client

package
v0.0.0-...-506983e Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2021 License: Apache-2.0 Imports: 26 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
)
View Source
const (
	FileTransferBufferSize = 1024
)

Variables

View Source
var (
	ErrShutdown         = errors.New("connection is shut down")
	ErrUnsupportedCodec = errors.New("unsupported codec")
)

ErrShutdown connection is closed.

View Source
var (
	// ErrXClientShutdown xclient is shutdown.
	ErrXClientShutdown = errors.New("xClient is shut down")
	// ErrXClientNoServer selector can't found one server.
	ErrXClientNoServer = errors.New("can not found any server")
	// ErrServerUnavailable selected server is unavailable.
	ErrServerUnavailable = errors.New("selected server is unavilable")
)
View Source
var ConnFactories = map[string]ConnFactoryFn{
	"http": newDirectHTTPConn,
	"unix": newDirectConn,
}
View Source
var DefaultOption = Option{
	Retries:             3,
	RPCPath:             share.DefaultRPCPath,
	ConnectTimeout:      time.Second,
	SerializeType:       protocol.MsgPack,
	CompressType:        protocol.None,
	BackupLatency:       10 * time.Millisecond,
	MaxWaitForHeartbeat: 30 * time.Second,
	TCPKeepAlivePeriod:  time.Minute,
}

DefaultOption is a common option configuration for client.

Functions

func Hash

func Hash(key uint64, buckets int32) int32

Hash consistently chooses a hash bucket number in the range [0, numBuckets) for the given key. numBuckets must be >= 1.

func HashString

func HashString(s string) uint64

HashString get a hash value of a string

func JumpConsistentHash

func JumpConsistentHash(len int, options ...interface{}) int

JumpConsistentHash selects a server by serviceMethod and args

func RegisterCacheClientBuilder

func RegisterCacheClientBuilder(network string, builder CacheClientBuilder)

Types

type CacheClientBuilder

type CacheClientBuilder interface {
	SetCachedClient(client RPCClient, k, servicePath, serviceMethod string)
	FindCachedClient(k, servicePath, serviceMethod string) RPCClient
	DeleteCachedClient(client RPCClient, k, servicePath, serviceMethod string)
	GenerateClient(k, servicePath, serviceMethod string) (client RPCClient, err error)
}

CacheClientBuilder defines builder interface to generate RPCCient.

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) GetConn

func (c *Client) GetConn() net.Conn

GetConn returns the underlying conn.

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) RemoteAddr

func (c *Client) RemoteAddr() string

RemoteAddr returns the remote address.

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 ConsistentAddrStrFunction

type ConsistentAddrStrFunction func(options ...interface{}) string

ConsistentFunction define a hash function Return service address, like "tcp@127.0.0.1:8970"

type DNSDiscovery

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

DNSDiscovery is based on DNS a record. You must set port and network info when you create the DNSDiscovery.

func (*DNSDiscovery) Clone

func (d *DNSDiscovery) Clone(servicePath string) (ServiceDiscovery, error)

Clone clones this ServiceDiscovery with new servicePath.

func (*DNSDiscovery) Close

func (d *DNSDiscovery) Close()

func (*DNSDiscovery) GetServices

func (d *DNSDiscovery) GetServices() []*KVPair

GetServices returns the static server

func (*DNSDiscovery) RemoveWatcher

func (d *DNSDiscovery) RemoveWatcher(ch chan []*KVPair)

func (*DNSDiscovery) SetFilter

func (d *DNSDiscovery) SetFilter(filter ServiceDiscoveryFilter)

SetFilter sets the filer.

func (*DNSDiscovery) WatchService

func (d *DNSDiscovery) WatchService() chan []*KVPair

WatchService returns a nil chan.

type FailMode

type FailMode int

FailMode decides how clients action when clients fail to invoke services

const (
	// Failover selects another server automaticaly
	Failover FailMode = iota
	// Failfast returns error immediately
	Failfast
	// Failtry use current client again
	Failtry
	// Failbackup select another server if the first server doesn't respon in specified time and use the fast response.
	Failbackup
)

type HashServiceAndArgs

type HashServiceAndArgs func(len int, options ...interface{}) int

HashServiceAndArgs define a hash function

type KVPair

type KVPair struct {
	Key   string
	Value string
}

KVPair contains a key and a string.

type MultipleServersDiscovery

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

MultipleServersDiscovery is a multiple servers service discovery. It always returns the current servers and uses can change servers dynamically.

func (*MultipleServersDiscovery) Clone

func (d *MultipleServersDiscovery) Clone(servicePath string) (ServiceDiscovery, error)

Clone clones this ServiceDiscovery with new servicePath.

func (*MultipleServersDiscovery) Close

func (d *MultipleServersDiscovery) Close()

func (*MultipleServersDiscovery) GetServices

func (d *MultipleServersDiscovery) GetServices() []*KVPair

GetServices returns the configured server

func (*MultipleServersDiscovery) RemoveWatcher

func (d *MultipleServersDiscovery) RemoveWatcher(ch chan []*KVPair)

func (*MultipleServersDiscovery) SetFilter

func (d *MultipleServersDiscovery) SetFilter(filter ServiceDiscoveryFilter)

SetFilter sets the filer.

func (*MultipleServersDiscovery) Update

func (d *MultipleServersDiscovery) Update(pairs []*KVPair)

Update is used to update servers at runtime.

func (*MultipleServersDiscovery) WatchService

func (d *MultipleServersDiscovery) WatchService() chan []*KVPair

WatchService returns a nil chan.

type OneClient

type OneClient struct {
	Plugins PluginContainer
	// contains filtered or unexported fields
}

OneClient wraps servicesPath and XClients. Users can use a shared oneclient to access multiple services.

func NewBidirectionalOneClient

func NewBidirectionalOneClient(failMode FailMode, selectMode SelectMode, discovery ServiceDiscovery, option Option, serverMessageChan chan<- *protocol.Message) *OneClient

NewBidirectionalOneClient creates a new xclient that can receive notifications from servers.

func NewOneClient

func NewOneClient(failMode FailMode, selectMode SelectMode, discovery ServiceDiscovery, option Option) *OneClient

NewOneClient creates a OneClient that supports service discovery and service governance.

func (*OneClient) Auth

func (c *OneClient) Auth(auth string)

Auth sets s token for Authentication.

func (*OneClient) Broadcast

func (c *OneClient) Broadcast(ctx context.Context, servicePath string, serviceMethod string, args interface{}, reply interface{}) error

Broadcast sends requests to all servers and Success only when all servers return OK. FailMode and SelectMode are meanless for this method. Please set timeout to avoid hanging.

func (*OneClient) Call

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

Call invokes the named function, waits for it to complete, and returns its error status. It handles errors base on FailMode.

func (*OneClient) Close

func (c *OneClient) Close() error

Close closes all xclients and its underlying connnections to services.

func (*OneClient) DownloadFile

func (c *OneClient) DownloadFile(ctx context.Context, requestFileName string, saveTo io.Writer, meta map[string]string) error

func (*OneClient) Fork

func (c *OneClient) Fork(ctx context.Context, servicePath string, serviceMethod string, args interface{}, reply interface{}) error

Fork sends requests to all servers and Success once one server returns OK. FailMode and SelectMode are meanless for this method.

func (*OneClient) GetPlugins

func (c *OneClient) GetPlugins() PluginContainer

func (*OneClient) Go

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

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. It does not use FailMode.

func (*OneClient) SendFile

func (c *OneClient) SendFile(ctx context.Context, fileName string, rateInBytesPerSecond int64, meta map[string]string) error

func (*OneClient) SendRaw

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

func (*OneClient) SetPlugins

func (c *OneClient) SetPlugins(plugins PluginContainer)

SetPlugins sets client's plugins.

func (*OneClient) SetSelector

func (c *OneClient) SetSelector(servicePath string, s Selector)

SetSelector sets customized selector by users.

func (*OneClient) Stream

func (c *OneClient) Stream(ctx context.Context, meta map[string]string) (net.Conn, error)

type OneClientPool

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

OneClientPool is a oneclient pool with fixed size. It uses roundrobin algorithm to call its xclients. All oneclients share the same configurations such as ServiceDiscovery and serverMessageChan.

func NewBidirectionalOneClientPool

func NewBidirectionalOneClientPool(count int, failMode FailMode, selectMode SelectMode, discovery ServiceDiscovery, option Option, serverMessageChan chan<- *protocol.Message) *OneClientPool

NewBidirectionalOneClientPool creates a BidirectionalOneClient pool with fixed size.

func NewOneClientPool

func NewOneClientPool(count int, failMode FailMode, selectMode SelectMode, discovery ServiceDiscovery, option Option) *OneClientPool

NewOneClientPool creates a fixed size OneClient pool.

func (*OneClientPool) Auth

func (c *OneClientPool) Auth(auth string)

Auth sets s token for Authentication.

func (OneClientPool) Close

func (p OneClientPool) Close()

Close this pool. Please make sure it won't be used any more.

func (*OneClientPool) Get

func (p *OneClientPool) Get() *OneClient

Get returns a OneClient. It does not remove this OneClient from its cache so you don't need to put it back. Don't close this OneClient because maybe other goroutines are using this OneClient.

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 max idle time for underlying net.Conns
	IdleTimeout 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

	SerializeType protocol.SerializeType
	CompressType  protocol.CompressType

	// send heartbeat message to service and check responses
	Heartbeat bool
	// interval for heartbeat
	HeartbeatInterval   time.Duration
	MaxWaitForHeartbeat time.Duration

	// TCPKeepAlive, if it is zero we don't set keepalive
	TCPKeepAlivePeriod time.Duration
}

Option contains all options for creating clients.

type Peer2PeerDiscovery

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

Peer2PeerDiscovery is a peer-to-peer service discovery. It always returns the static server.

func (*Peer2PeerDiscovery) Clone

func (d *Peer2PeerDiscovery) Clone(servicePath string) (ServiceDiscovery, error)

Clone clones this ServiceDiscovery with new servicePath.

func (*Peer2PeerDiscovery) Close

func (d *Peer2PeerDiscovery) Close()

func (*Peer2PeerDiscovery) GetServices

func (d *Peer2PeerDiscovery) GetServices() []*KVPair

GetServices returns the static server

func (*Peer2PeerDiscovery) RemoveWatcher

func (d *Peer2PeerDiscovery) RemoveWatcher(ch chan []*KVPair)

func (*Peer2PeerDiscovery) SetFilter

func (d *Peer2PeerDiscovery) SetFilter(filter ServiceDiscoveryFilter)

SetFilter sets the filer.

func (*Peer2PeerDiscovery) WatchService

func (d *Peer2PeerDiscovery) WatchService() chan []*KVPair

WatchService returns a nil chan.

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

	DoWrapSelect(SelectFunc) SelectFunc
}

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 {
	PostCall(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 {
	PreCall(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
	RemoteAddr() string

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

	IsClosing() bool
	IsShutdown() bool

	GetConn() net.Conn
}

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

type Receipt

type Receipt struct {
	Address string
	Reply   interface{}
	Error   error
}

Receipt represents the result of the service returned.

type SelectFunc

type SelectFunc func(ctx context.Context, servicePath, serviceMethod string, args interface{}) string

type SelectMode

type SelectMode int

SelectMode defines the algorithm of selecting a services from candidates.

const (
	// RandomSelect is selecting randomly
	RandomSelect SelectMode = iota
	// RoundRobin is selecting by round robin
	RoundRobin
	// WeightedRoundRobin is selecting by weighted round robin
	WeightedRoundRobin
	// WeightedICMP is selecting by weighted Ping time
	WeightedICMP
	// ConsistentHash is selecting by hashing
	ConsistentHash

	// SelectByUser is selecting by implementation of users
	SelectByUser = 1000
)

type SelectNodePlugin

type SelectNodePlugin interface {
	WrapSelect(SelectFunc) SelectFunc
}

SelectNodePlugin can interrupt selecting of xclient and add customized logics such as skipping some nodes.

type Selector

type Selector interface {
	Select(ctx context.Context, servicePath, serviceMethod string, args interface{}) string // SelectFunc
	UpdateServer(servers map[string]string)
}

Selector defines selector that selects one service from candidates.

type ServiceDiscovery

type ServiceDiscovery interface {
	GetServices() []*KVPair
	WatchService() chan []*KVPair
	RemoveWatcher(ch chan []*KVPair)
	Clone(servicePath string) (ServiceDiscovery, error)
	SetFilter(ServiceDiscoveryFilter)
	Close()
}

ServiceDiscovery defines ServiceDiscovery of zookeeper, etcd and consul

func NewDNSDiscovery

func NewDNSDiscovery(domain string, network string, port int, d time.Duration) (ServiceDiscovery, error)

NewPeer2PeerDiscovery returns a new Peer2PeerDiscovery.

func NewMultipleServersDiscovery

func NewMultipleServersDiscovery(pairs []*KVPair) (ServiceDiscovery, error)

NewMultipleServersDiscovery returns a new MultipleServersDiscovery.

func NewPeer2PeerDiscovery

func NewPeer2PeerDiscovery(server, metadata string) (ServiceDiscovery, error)

NewPeer2PeerDiscovery returns a new Peer2PeerDiscovery.

type ServiceDiscoveryFilter

type ServiceDiscoveryFilter func(kvp *KVPair) bool

ServiceDiscoveryFilter can be used to filter services with customized logics. Servers can register its services but clients can use the customized filter to select some services. It returns true if ServiceDiscovery wants to use this service, otherwise it returns false.

type ServiceError

type ServiceError string

ServiceError is an error from server.

func (ServiceError) Error

func (e ServiceError) Error() string

type Weighted

type Weighted struct {
	Server          string
	Weight          int
	CurrentWeight   int
	EffectiveWeight int
}

Weighted is a wrapped server with weight

type XClient

type XClient interface {
	SetPlugins(plugins PluginContainer)
	GetPlugins() PluginContainer
	SetSelector(s Selector)
	Auth(auth string)

	Go(ctx context.Context, serviceMethod string, args interface{}, reply interface{}, done chan *Call) (*Call, error)
	Call(ctx context.Context, serviceMethod string, args interface{}, reply interface{}) error
	Broadcast(ctx context.Context, serviceMethod string, args interface{}, reply interface{}) error
	Fork(ctx context.Context, serviceMethod string, args interface{}, reply interface{}) error
	Inform(ctx context.Context, serviceMethod string, args interface{}, reply interface{}) ([]Receipt, error)
	SendRaw(ctx context.Context, r *protocol.Message) (map[string]string, []byte, error)
	SendFile(ctx context.Context, fileName string, rateInBytesPerSecond int64, meta map[string]string) error
	DownloadFile(ctx context.Context, requestFileName string, saveTo io.Writer, meta map[string]string) error
	Stream(ctx context.Context, meta map[string]string) (net.Conn, error)
	Close() error
}

XClient is an interface that used by client with service discovery and service governance. One XClient is used only for one service. You should create multiple XClient for multiple services.

func NewBidirectionalXClient

func NewBidirectionalXClient(servicePath string, failMode FailMode, selectMode SelectMode, discovery ServiceDiscovery, option Option, serverMessageChan chan<- *protocol.Message) XClient

NewBidirectionalXClient creates a new xclient that can receive notifications from servers.

func NewXClient

func NewXClient(servicePath string, failMode FailMode, selectMode SelectMode, discovery ServiceDiscovery, option Option) XClient

NewXClient creates a XClient that supports service discovery and service governance.

type XClientPool

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

XClientPool is a xclient pool with fixed size. It uses roundrobin algorithm to call its xclients. All xclients share the same configurations such as ServiceDiscovery and serverMessageChan.

func NewBidirectionalXClientPool

func NewBidirectionalXClientPool(count int, servicePath string, failMode FailMode, selectMode SelectMode, discovery ServiceDiscovery, option Option, serverMessageChan chan<- *protocol.Message) *XClientPool

NewBidirectionalXClientPool creates a BidirectionalXClient pool with fixed size.

func NewXClientPool

func NewXClientPool(count int, servicePath string, failMode FailMode, selectMode SelectMode, discovery ServiceDiscovery, option Option) *XClientPool

NewXClientPool creates a fixed size XClient pool.

func (*XClientPool) Auth

func (c *XClientPool) Auth(auth string)

Auth sets s token for Authentication.

func (*XClientPool) Close

func (p *XClientPool) Close()

Close this pool. Please make sure it won't be used any more.

func (*XClientPool) Get

func (p *XClientPool) Get() XClient

Get returns a xclient. It does not remove this xclient from its cache so you don't need to put it back. Don't close this xclient because maybe other goroutines are using this xclient.

Jump to

Keyboard shortcuts

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