Documentation
¶
Overview ¶
Package rpc provides a RPC implementation over the node-oriented connections.
Index ¶
- Variables
- func AcceptNAConn(ctx context.Context, conn net.Conn) (net.Conn, error)
- func AcceptRawConn(ctx context.Context, conn net.Conn) (net.Conn, error)
- func NewClient(stream io.ReadWriteCloser) (client *rpc.Client)
- func ServeDirect(ctx context.Context, server *rpc.Server, stream io.ReadWriteCloser, ...)
- type AcceptConn
- type Caller
- type Client
- type ClientPool
- type LastErrSetter
- type NOClientPool
- type NodeAwareServerCodec
- type PCaller
- type PersistentCaller
- type ServeStream
- type Server
- func (s *Server) InitRPCServer(addr string, privateKeyPath string, masterKey []byte) (err error)
- func (s *Server) RegisterService(name string, service interface{}) error
- func (s *Server) Serve()
- func (s *Server) SetListener(l net.Listener)
- func (s *Server) Stop()
- func (s *Server) WithAcceptConnFunc(f AcceptConn) *Server
- type ServiceMap
Constants ¶
This section is empty.
Variables ¶
The following variables define a method set to Dial/Accept node-oriented connections for this RPC package.
TODO(leventeliu): allow to config other node-oriented connection dialer/accepter.
Functions ¶
func AcceptNAConn ¶
AcceptNAConn accepts connection as a naconn.NAConn.
Default accept function of RPC server, and also the only accept function for the connections from a NAConnPool.
Corresponding dialer is naconn.Dial/naconn.DialEx.
func AcceptRawConn ¶
AcceptRawConn accepts raw connection without encryption or node-oriented mechanism.
Corresponding dialer is net.Dial.
func NewClient ¶
func NewClient(stream io.ReadWriteCloser) (client *rpc.Client)
NewClient returns a new Client with stream.
NOTE(leventeliu): ownership of stream is passed through:
io.Closer -> rpc.ClientCodec -> *rpc.Client
Closing the *rpc.Client will cause io.Closer invoked.
func ServeDirect ¶
func ServeDirect( ctx context.Context, server *rpc.Server, stream io.ReadWriteCloser, remote *proto.RawNodeID, )
ServeDirect serves data stream directly.
Types ¶
type AcceptConn ¶
AcceptConn defines the function type which accepts a raw connetion as a specific type of connection.
func NewAcceptCryptoConnFunc ¶
func NewAcceptCryptoConnFunc(handler etls.CipherHandler) AcceptConn
NewAcceptCryptoConnFunc returns a AcceptConn function which accepts raw connection and uses the cipher handler to handle it as etls.CryptoConn.
Corresponding dialer is crypto/etls.Dial.
type Caller ¶
type Caller struct {
// contains filtered or unexported fields
}
Caller is a wrapper for session pooling and RPC calling.
func NewCallerWithPool ¶
func NewCallerWithPool(pool NOClientPool) *Caller
NewCallerWithPool returns a new Caller with the pool.
type Client ¶
type Client interface { Call(serviceMethod string, args interface{}, reply interface{}) error Go(serviceMethod string, args interface{}, reply interface{}, done chan *rpc.Call) *rpc.Call Close() error }
Client defines the RPC client interface.
func DialToNodeWithPool ¶
DialToNodeWithPool ties use connection in pool, if fails then connects to the node with nodeID.
type ClientPool ¶
type ClientPool struct {
// contains filtered or unexported fields
}
ClientPool is the struct type of connection pool.
func (*ClientPool) Close ¶
func (p *ClientPool) Close() error
Close closes all FreeLists in the pool.
func (*ClientPool) Get ¶
func (p *ClientPool) Get(id proto.NodeID) (cli Client, err error)
Get returns existing freelist to the node, if not exist try best to create one.
func (*ClientPool) GetEx ¶
GetEx returns a client with an one-off connection if it's anonymous, otherwise returns existing freelist with Get.
func (*ClientPool) Len ¶
func (p *ClientPool) Len() (total int)
Len returns the connection count in the pool.
func (*ClientPool) Remove ¶
func (p *ClientPool) Remove(id proto.NodeID)
Remove the node freelist in the pool.
type LastErrSetter ¶
type LastErrSetter interface {
SetLastErr(error)
}
LastErrSetter defines the extend method to set client last error.
type NOClientPool ¶
type NOClientPool interface { Get(remote proto.NodeID) (Client, error) GetEx(remote proto.NodeID, isAnonymous bool) (Client, error) Close() error }
NOClientPool defines the node-oriented client pool interface.
type NodeAwareServerCodec ¶
NodeAwareServerCodec wraps normal rpc.ServerCodec and inject node id during request process.
func NewNodeAwareServerCodec ¶
func NewNodeAwareServerCodec(ctx context.Context, codec rpc.ServerCodec, nodeID *proto.RawNodeID) *NodeAwareServerCodec
NewNodeAwareServerCodec returns new NodeAwareServerCodec with normal rpc.ServerCode and proto.RawNodeID.
func (*NodeAwareServerCodec) ReadRequestBody ¶
func (nc *NodeAwareServerCodec) ReadRequestBody(body interface{}) (err error)
ReadRequestBody override default rpc.ServerCodec behaviour and inject remote node id into request.
type PCaller ¶
type PCaller interface { Call(method string, request interface{}, reply interface{}) (err error) Close() Target() string New() PCaller // returns new instance of current caller }
PCaller defines generic interface shared with PersistentCaller and RawCaller.
type PersistentCaller ¶
type PersistentCaller struct { //TargetAddr string TargetID proto.NodeID sync.Mutex // contains filtered or unexported fields }
PersistentCaller is a wrapper for session pooling and RPC calling.
func NewPersistentCaller ¶
func NewPersistentCaller(target proto.NodeID) *PersistentCaller
NewPersistentCaller returns a persistent RPCCaller.
IMPORTANT: If a PersistentCaller is firstly used by a DHT.Ping, which is an anonymous ETLS connection. It should not be used by any other RPC except DHT.Ping.
func NewPersistentCallerWithPool ¶
func NewPersistentCallerWithPool(pool NOClientPool, target proto.NodeID) *PersistentCaller
NewPersistentCallerWithPool returns a persistent RPCCaller.
IMPORTANT: If a PersistentCaller is firstly used by a DHT.Ping, which is an anonymous ETLS connection. It should not be used by any other RPC except DHT.Ping.
func (*PersistentCaller) Call ¶
func (c *PersistentCaller) Call(method string, args interface{}, reply interface{}) (err error)
Call invokes the named function, waits for it to complete, and returns its error status.
func (*PersistentCaller) Close ¶
func (c *PersistentCaller) Close()
Close closes the stream and RPC client.
func (*PersistentCaller) New ¶
func (c *PersistentCaller) New() PCaller
New returns brand new persistent caller.
func (*PersistentCaller) ResetClient ¶
func (c *PersistentCaller) ResetClient() (err error)
ResetClient resets client.
func (*PersistentCaller) Target ¶
func (c *PersistentCaller) Target() string
Target returns the request target for logging purpose.
type ServeStream ¶
type ServeStream func( ctx context.Context, server *rpc.Server, stream io.ReadWriteCloser, remote *proto.RawNodeID, )
ServeStream defines the data stream serving function type which serves RPC at the given io.ReadWriteCloser.
type Server ¶
Server is the RPC server struct.
func NewServerWithServeFunc ¶
func NewServerWithServeFunc(f ServeStream) *Server
NewServerWithServeFunc return a new Server.
func NewServerWithService ¶
func NewServerWithService(serviceMap ServiceMap) (server *Server, err error)
NewServerWithService returns a new Server and registers the Server.ServiceMap.
func (*Server) InitRPCServer ¶
InitRPCServer load the private key, init the crypto transfer layer and register RPC services. IF ANY ERROR returned, please raise a FATAL.
func (*Server) RegisterService ¶
RegisterService registers service with a Service name, used by Client RPC.
func (*Server) SetListener ¶
SetListener set the service loop listener, used by func Serve main loop.
func (*Server) WithAcceptConnFunc ¶
func (s *Server) WithAcceptConnFunc(f AcceptConn) *Server
WithAcceptConnFunc resets the AcceptConn function of server.
type ServiceMap ¶
type ServiceMap map[string]interface{}
ServiceMap maps service name to service instance.