client

package
v0.0.0-...-b7e272e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 10, 2020 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	XVersion           = "X-RPCX-Version"
	XMessageType       = "X-RPCX-MesssageType"
	XHeartbeat         = "X-RPCX-Heartbeat"
	XOneway            = "X-RPCX-Oneway"
	XMessageStatusType = "X-RPCX-MessageStatusType"
	XSerializeType     = "X-RPCX-SerializeType"
	XMessageID         = "X-RPCX-MessageID"
	XServicePath       = "X-RPCX-ServicePath"
	XServiceMethod     = "X-RPCX-ServiceMethod"
	XMeta              = "X-RPCX-Meta"
	XErrorMessage      = "X-RPCX-ErrorMessage"
)
View Source
const (
	// ReaderBuffsize is used for bufio reader.
	ReaderBuffsize = 16 * 1024
	// WriterBuffsize is used for bufio writer.
	WriterBuffsize = 16 * 1024
)

Variables

View Source
var (
	ErrBreakerOpen    = errors.New("breaker open")
	ErrBreakerTimeout = errors.New("breaker time out")
)
View Source
var (
	ErrShutdown         = errors.New("connection is shut down")
	ErrUnsupportedCodec = errors.New("unsupported codec")
)

ErrShutdown connection is closed.

View Source
var ConnFactories = map[string]ConnFactoryFn{
	"http": newDirectHTTPConn,
	"quic": newDirectQuicConn,
	"pipe": newDirectPipeConn,
	"unix": newDirectConn,
}
View Source
var DefaultOption = Option{
	Retries:        3,
	RPCPath:        share.DefaultRPCPath,
	ConnectTimeout: 10 * time.Second,
	SerializeType:  protocol.MsgPack,
	CompressType:   protocol.None,
	BackupLatency:  10 * time.Millisecond,
}

DefaultOption is a common option configuration for client.

Functions

This section is empty.

Types

type Breaker

type Breaker interface {
	Call(func() error, time.Duration) error
	Fail()
	Success()
	Ready() bool
}

Breaker is a CircuitBreaker interface.

var CircuitBreaker Breaker = circuit.NewRateBreaker(0.95, 100)

CircuitBreaker is a default circuit breaker (RateBreaker(0.95, 100)).

type Call

type Call struct {
	ServicePath   string            // The name of the service and method to call.
	ServiceMethod string            // The name of the service and method to call.
	Metadata      map[string]string //metadata
	ResMetadata   map[string]string
	Args          interface{} // The argument to the function (*struct).
	Reply         interface{} // The reply from the function (*struct).
	Error         error       // After completion, the error status.
	Done          chan *Call  // Strobes when call is complete.
	Raw           bool        // raw message or not
}

Call represents an active RPC.

type Client

type Client struct {
	Conn net.Conn

	Plugins PluginContainer

	ServerMessageChan chan<- *protocol.Message
	// contains filtered or unexported fields
}

Client represents a RPC client.

func NewClient

func NewClient(option Option) *Client

NewClient returns a new Client with the option.

func (*Client) Call

func (client *Client) Call(ctx context.Context, servicePath, serviceMethod string, args interface{}, reply interface{}) error

Call invokes the named function, waits for it to complete, and returns its error status.

func (*Client) Close

func (client *Client) Close() error

Close calls the underlying connection's Close method. If the connection is already shutting down, ErrShutdown is returned.

func (*Client) Connect

func (c *Client) Connect(network, address string) error

Connect connects the server via specified network.

func (*Client) Go

func (client *Client) Go(ctx context.Context, servicePath, serviceMethod string, args interface{}, reply interface{}, done chan *Call) *Call

Go invokes the function asynchronously. It returns the Call structure representing the invocation. The done channel will signal when the call is complete by returning the same Call object. If done is nil, Go will allocate a new channel. If non-nil, done must be buffered or Go will deliberately crash.

func (*Client) IsClosing

func (client *Client) IsClosing() bool

IsClosing client is closing or not.

func (*Client) IsShutdown

func (client *Client) IsShutdown() bool

IsShutdown client is shutdown or not.

func (*Client) RegisterServerMessageChan

func (client *Client) RegisterServerMessageChan(ch chan<- *protocol.Message)

RegisterServerMessageChan registers the channel that receives server requests.

func (*Client) SendRaw

func (client *Client) SendRaw(ctx context.Context, r *protocol.Message) (map[string]string, []byte, error)

SendRaw sends raw messages. You don't care args and replys.

func (*Client) UnregisterServerMessageChan

func (client *Client) UnregisterServerMessageChan()

UnregisterServerMessageChan removes ServerMessageChan.

type ClientAfterDecodePlugin

type ClientAfterDecodePlugin interface {
	ClientAfterDecode(*protocol.Message) error
}

ClientAfterDecodePlugin is invoked when the message is decoded.

type ClientBeforeEncodePlugin

type ClientBeforeEncodePlugin interface {
	ClientBeforeEncode(*protocol.Message) error
}

ClientBeforeEncodePlugin is invoked when the message is encoded and sent.

type ClientConnectedPlugin

type ClientConnectedPlugin interface {
	ClientConnected(net.Conn) (net.Conn, error)
}

ClientConnectedPlugin is invoked when the client has connected the server.

type ClientConnectionClosePlugin

type ClientConnectionClosePlugin interface {
	ClientConnectionClose(net.Conn) error
}

ClientConnectionClosePlugin is invoked when the connection is closing.

type ConnCreatedPlugin

type ConnCreatedPlugin interface {
	ConnCreated(net.Conn) (net.Conn, error)
}

ConnCreatedPlugin is invoked when the client connection has created.

type ConnFactoryFn

type ConnFactoryFn func(c *Client, network, address string) (net.Conn, error)

type ConsecCircuitBreaker

type ConsecCircuitBreaker struct {
	// contains filtered or unexported fields
}

ConsecCircuitBreaker is window sliding CircuitBreaker with failure threshold.

func NewConsecCircuitBreaker

func NewConsecCircuitBreaker(failureThreshold uint64, window time.Duration) *ConsecCircuitBreaker

NewConsecCircuitBreaker returns a new ConsecCircuitBreaker.

func (*ConsecCircuitBreaker) Call

func (cb *ConsecCircuitBreaker) Call(fn func() error, d time.Duration) error

Call Circuit function

func (*ConsecCircuitBreaker) Fail

func (cb *ConsecCircuitBreaker) Fail()

func (*ConsecCircuitBreaker) Ready

func (cb *ConsecCircuitBreaker) Ready() bool

func (*ConsecCircuitBreaker) Success

func (cb *ConsecCircuitBreaker) Success()

type Option

type Option struct {
	// Group is used to select the services in the same group. Services set group info in their meta.
	// If it is empty, clients will ignore group.
	Group string

	// Retries retries to send
	Retries int

	// TLSConfig for tcp and quic
	TLSConfig *tls.Config
	// kcp.BlockCrypt
	Block interface{}
	// RPCPath for http connection
	RPCPath string
	//ConnectTimeout sets timeout for dialing
	ConnectTimeout time.Duration
	// ReadTimeout sets readdeadline for underlying net.Conns
	ReadTimeout time.Duration
	// WriteTimeout sets writedeadline for underlying net.Conns
	WriteTimeout time.Duration

	// BackupLatency is used for Failbackup mode. rpcx will sends another request if the first response doesn't return in BackupLatency time.
	BackupLatency time.Duration

	// Breaker is used to config CircuitBreaker
	GenBreaker func() Breaker

	SerializeType protocol.SerializeType
	CompressType  protocol.CompressType

	Heartbeat         bool
	HeartbeatInterval time.Duration
}

Option contains all options for creating clients.

type Plugin

type Plugin interface {
}

Plugin is the client plugin interface.

type PluginContainer

type PluginContainer interface {
	Add(plugin Plugin)
	Remove(plugin Plugin)
	All() []Plugin

	DoConnCreated(net.Conn) (net.Conn, error)
	DoClientConnected(net.Conn) (net.Conn, error)
	DoClientConnectionClose(net.Conn) error

	DoPreCall(ctx context.Context, servicePath, serviceMethod string, args interface{}) error
	DoPostCall(ctx context.Context, servicePath, serviceMethod string, args interface{}, reply interface{}, err error) error

	DoClientBeforeEncode(*protocol.Message) error
	DoClientAfterDecode(*protocol.Message) error
}

PluginContainer represents a plugin container that defines all methods to manage plugins. And it also defines all extension points.

func NewPluginContainer

func NewPluginContainer() PluginContainer

type PostCallPlugin

type PostCallPlugin interface {
	DoPostCall(ctx context.Context, servicePath, serviceMethod string, args interface{}, reply interface{}, err error) error
}

PostCallPlugin is invoked after the client calls a server.

type PreCallPlugin

type PreCallPlugin interface {
	DoPreCall(ctx context.Context, servicePath, serviceMethod string, args interface{}) error
}

PreCallPlugin is invoked before the client calls a server.

type RPCClient

type RPCClient interface {
	Connect(network, address string) error
	Go(ctx context.Context, servicePath, serviceMethod string, args interface{}, reply interface{}, done chan *Call) *Call
	Call(ctx context.Context, servicePath, serviceMethod string, args interface{}, reply interface{}) error
	SendRaw(ctx context.Context, r *protocol.Message) (map[string]string, []byte, error)
	Close() error

	RegisterServerMessageChan(ch chan<- *protocol.Message)
	UnregisterServerMessageChan()

	IsClosing() bool
	IsShutdown() bool
}

RPCClient is interface that defines one client to call one server.

type ServiceError

type ServiceError string

ServiceError is an error from server.

func (ServiceError) Error

func (e ServiceError) Error() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL