Documentation
¶
Overview ¶
Package natsrpc is an rpc implemenation using nats as transport.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNCMaxReconnect is returned if nats.Conn has MaxReconnects >= 0. ErrNCMaxReconnect = errors.New("natsrpc.ServerConn nats.Conn should have MaxReconnects < 0") // ErrClosed is returned when ServerConn is closed ErrClosed = errors.New("natsrpc.ServerConn closed") )
var ( // DefaultSubjectPrefix is the default value of SCOptSubjectPrefix/CCOptSubjectPrefix. DefaultSubjectPrefix = "natsrpc" // DefaultGroup is the default value of SCOptGroup. DefaultGroup = "def" // DefaultClientTimeout is the default value of CCOptTimeout. DefaultClientTimeout time.Duration = 5 * time.Second )
Functions ¶
func NewPbJsonClient ¶
func NewPbJsonClient(cc *ClientConn) RPCClientFunc
NewPbJsonClient creates an rpc client using protobuf or json for encoding/decoding:
- If both input and output are proto.Message, then use protobuf.
- Otherwise use json.
func NewPbJsonServer ¶
func NewPbJsonServer(sc *ServerConn) RPCServerFunc
NewPbJsonServer creates an rpc server using protobuf or json for decoding/encoding.
Types ¶
type ClientConn ¶
type ClientConn struct {
// contains filtered or unexported fields
}
ClientConn wraps nats.Conn into 'client side rpc connection'.
func NewClientConn ¶
func NewClientConn(nc *nats.Conn, opts ...ClientConnOption) (*ClientConn, error)
NewClientConn creates a new ClientConn. `nc` must have MaxReconnect < 0 (e.g. never give up trying to reconnect).
type ClientConnOption ¶
type ClientConnOption func(*ClientConn) error
ClientConnOption is option in creating ClientConn.
func CCOptSubjectPrefix ¶
func CCOptSubjectPrefix(subjectPrefix string) ClientConnOption
CCOptSubjectPrefix sets subject prefix in nats namespace.
func CCOptTimeout ¶
func CCOptTimeout(t time.Duration) ClientConnOption
CCOptTimeout sets the default timeout when invoking rpc if there is no timeout set in context.
type ServerConn ¶
type ServerConn struct {
// contains filtered or unexported fields
}
ServerConn wraps nats.Conn into 'server side rpc connection'.
func NewServerConn ¶
func NewServerConn(nc *nats.Conn, opts ...ServerConnOption) (sc *ServerConn, err error)
NewServerConn creates a new ServerConn. `nc` must have MaxReconnect < 0 (e.g. never give up trying to reconnect).
func (*ServerConn) Close ¶
func (sc *ServerConn) Close() error
Close shutdowns the ServerConn: closes handler runner and unsubscribe from nats.
type ServerConnOption ¶
type ServerConnOption func(*ServerConn) error
ServerConnOption is option in creating ClientConn.
func SCOptContext ¶
func SCOptContext(ctx context.Context) ServerConnOption
SCOptContext sets base context used in handlers.
func SCOptGroup ¶
func SCOptGroup(group string) ServerConnOption
SCOptGroup sets nats subscription group name.
func SCOptLogger ¶
func SCOptLogger(logger logr.Logger) ServerConnOption
SCOptLogger sets logger for ServerConn.
func SCOptRunner ¶
func SCOptRunner(runner taskrunner.TaskRunner) ServerConnOption
SCOptRunner sets runner for handlers. Note that the runner will be closed in ServerConn.Close().
func SCOptSubjectPrefix ¶
func SCOptSubjectPrefix(subjectPrefix string) ServerConnOption
SCOptSubjectPrefix sets subject prefix in nats namespace.