CovenantSQL: Index | Files | Directories

package rpc

import ""

Package rpc provides a RPC implementation over the node-oriented connections.


Package Files

accept.go caller.go client.go codec.go doc.go pcaller.go pool.go serve.go server.go utils.go wrapper.go


var (
    Dial   = naconn.Dial
    DialEx = naconn.DialEx
    Accept = naconn.Accept

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.

func AcceptNAConn Uses

func AcceptNAConn(ctx context.Context, conn net.Conn) (net.Conn, error)

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 Uses

func AcceptRawConn(ctx context.Context, conn net.Conn) (net.Conn, error)

AcceptRawConn accepts raw connection without encryption or node-oriented mechanism.

Corresponding dialer is net.Dial.

func NewClient Uses

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 Uses

func ServeDirect(
    ctx context.Context, server *rpc.Server, stream io.ReadWriteCloser, remote *proto.RawNodeID,

ServeDirect serves data stream directly.

type AcceptConn Uses

type AcceptConn func(ctx context.Context, conn net.Conn) (net.Conn, error)

AcceptConn defines the function type which accepts a raw connetion as a specific type of connection.

func NewAcceptCryptoConnFunc Uses

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 Uses

type Caller struct {
    // contains filtered or unexported fields

Caller is a wrapper for session pooling and RPC calling.

func NewCaller Uses

func NewCaller() *Caller

NewCaller returns a new RPCCaller.

func NewCallerWithPool Uses

func NewCallerWithPool(pool NOClientPool) *Caller

NewCallerWithPool returns a new Caller with the pool.

func (*Caller) CallNode Uses

func (c *Caller) CallNode(node proto.NodeID, method string, args, reply interface{}) (err error)

CallNode calls node method.

func (*Caller) CallNodeWithContext Uses

func (c *Caller) CallNodeWithContext(
    ctx context.Context, node proto.NodeID, method string, args, reply interface{}) (err error,

CallNodeWithContext calls node method with context.

type Client Uses

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 Uses

func DialToNodeWithPool(pool NOClientPool, nodeID proto.NodeID, isAnonymous bool) (Client, error)

DialToNodeWithPool ties use connection in pool, if fails then connects to the node with nodeID.

type ClientPool Uses

type ClientPool struct {
    // contains filtered or unexported fields

ClientPool is the struct type of connection pool.

func (*ClientPool) Close Uses

func (p *ClientPool) Close() error

Close closes all FreeLists in the pool.

func (*ClientPool) Get Uses

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 Uses

func (p *ClientPool) GetEx(id proto.NodeID, isAnonymous bool) (cli Client, err error)

GetEx returns a client with an one-off connection if it's anonymous, otherwise returns existing freelist with Get.

func (*ClientPool) Len Uses

func (p *ClientPool) Len() (total int)

Len returns the connection count in the pool.

func (*ClientPool) Remove Uses

func (p *ClientPool) Remove(id proto.NodeID)

Remove the node freelist in the pool.

type LastErrSetter Uses

type LastErrSetter interface {

LastErrSetter defines the extend method to set client last error.

type NOClientPool Uses

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 Uses

type NodeAwareServerCodec struct {
    NodeID *proto.RawNodeID
    Ctx    context.Context

NodeAwareServerCodec wraps normal rpc.ServerCodec and inject node id during request process.

func NewNodeAwareServerCodec Uses

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 Uses

func (nc *NodeAwareServerCodec) ReadRequestBody(body interface{}) (err error)

ReadRequestBody override default rpc.ServerCodec behaviour and inject remote node id into request.

type PCaller Uses

type PCaller interface {
    Call(method string, request interface{}, reply interface{}) (err error)
    Target() string
    New() PCaller // returns new instance of current caller

PCaller defines generic interface shared with PersistentCaller and RawCaller.

type PersistentCaller Uses

type PersistentCaller struct {

    //TargetAddr string
    TargetID proto.NodeID
    // contains filtered or unexported fields

PersistentCaller is a wrapper for session pooling and RPC calling.

func NewPersistentCaller Uses

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 Uses

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 Uses

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 Uses

func (c *PersistentCaller) Close()

Close closes the stream and RPC client.

func (*PersistentCaller) New Uses

func (c *PersistentCaller) New() PCaller

New returns brand new persistent caller.

func (*PersistentCaller) ResetClient Uses

func (c *PersistentCaller) ResetClient() (err error)

ResetClient resets client.

func (*PersistentCaller) Target Uses

func (c *PersistentCaller) Target() string

Target returns the request target for logging purpose.

type ServeStream Uses

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 Uses

type Server struct {
    Listener net.Listener
    // contains filtered or unexported fields

Server is the RPC server struct.

func NewServer Uses

func NewServer() *Server

NewServer return a new Server.

func NewServerWithServeFunc Uses

func NewServerWithServeFunc(f ServeStream) *Server

NewServerWithServeFunc return a new Server.

func NewServerWithService Uses

func NewServerWithService(serviceMap ServiceMap) (server *Server, err error)

NewServerWithService returns a new Server and registers the Server.ServiceMap.

func (*Server) InitRPCServer Uses

func (s *Server) InitRPCServer(
    addr string,
    privateKeyPath string,
    masterKey []byte,
) (err error)

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 Uses

func (s *Server) RegisterService(name string, service interface{}) error

RegisterService registers service with a Service name, used by Client RPC.

func (*Server) Serve Uses

func (s *Server) Serve()

Serve start the Server main loop,.

func (*Server) SetListener Uses

func (s *Server) SetListener(l net.Listener)

SetListener set the service loop listener, used by func Serve main loop.

func (*Server) Stop Uses

func (s *Server) Stop()

Stop Server main loop.

func (*Server) WithAcceptConnFunc Uses

func (s *Server) WithAcceptConnFunc(f AcceptConn) *Server

WithAcceptConnFunc resets the AcceptConn function of server.

type ServiceMap Uses

type ServiceMap map[string]interface{}

ServiceMap maps service name to service instance.


muxPackage mux provides a RPC implementation with connection multiplexing.

Package rpc imports 18 packages (graph) and is imported by 10 packages. Updated 2019-08-12. Refresh now. Tools for package owners.