Documentation ¶
Overview ¶
Package rpcz allows exposing and accessing methods of a service over network.
This package supports communication over TCP, Unix sockets, and with TLS on top of it. The default and recommended encoding is Protobuf, with JSON available as an alternative. No user-defined encodings (aka codecs) are supported, nor it is planned. It is as an alternative to rpc from the standard library and gRPC. The server allows to register and expose one or more services. A service must be a pointer to a value of an exported type. Each method meeting the following criteria will be registered and can be called remotely: - A method must be exported. - A method has three arguments. - The first argument is context.Context. - The second and third arguments are both pointers and exported. - A method has only one return parameter of the type error. The following line illustrates a valid method's signature: func (s *Service) SomeMethod(ctx context.Context, req *SomeMethodRequest, resp *SomeMethodResponse) error The request and response types must be marshallable to Protobuf, i.e. implement the proto.Message interface. The first argument of a service's method is passed in the server's context. This context is cancelled when the server is requested to shutdown. The server is not concerned with specifying timeouts for service's methods. It's the service's responsibility to implement timeouts should they be needed. The primary use of the parent context is to be notified when shutdown has been requested to gracefully finish any ongoing operations. At the moment, the context does not include any data, but it might be exteneded at a later point with useful information such as a request trace identificator. Reminder: contexts MUST NOT be used for dependency injection. The second argument represents a request to the method of a service. The third argument is passed in a pointer to a value to which the method writes the response. If a method returns an error, it's sent over the wire as a string, and when the client returns it as ServiceError.
Index ¶
- Variables
- type Client
- func NewClient(saddr string) (*Client, error)
- func NewClientTLS(saddr string, cfg *tls.Config) (*Client, error)
- func NewClientTLSOptions(saddr string, cfg *tls.Config, opts *ClientOptions) (*Client, error)
- func NewClientWithConn(opts *ClientOptions, nc net.Conn) (*Client, error)
- func NewClientWithOptions(saddr string, opts *ClientOptions) (*Client, error)
- type ClientOptions
- type Encoding
- type Result
- type Server
- func NewServer(laddr string) (*Server, error)
- func NewServerTLS(laddr string, cfg *tls.Config) (*Server, error)
- func NewServerTLSOptions(laddr string, cfg *tls.Config, opts *ServerOptions) (*Server, error)
- func NewServerWithListener(opts *ServerOptions, ln net.Listener) (*Server, error)
- func NewServerWithOptions(laddr string, opts *ServerOptions) (*Server, error)
- type ServerOptions
- type ServiceError
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidEncoding = errors.New("rpcz: invalid encoding") ErrInvalidServerOptions = errors.New("rpcz: invalid server options: must not be nil") ErrInvalidAddr = errors.New("rpcz: invalid server address") ErrInvalidListener = errors.New("rpcz: invalid server listener") ErrSrvStarted = errors.New("rpcz: server already started") ErrSrvClosed = errors.New("rpcz: server already shutdown") ErrInvalidClientOptions = errors.New("rpcz: invalid client options: must not be nil") ErrShutdown = errors.New("rpcz: client is shutdown") ErrClosed = errors.New("rpcz: client is closed") )
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client makes requests to remote methods on services registered on a remote server.
func NewClient ¶
NewClient returns a client connected to saddr with Protobuf encoding and default options.
func NewClientTLS ¶
NewClientTLS returns a client connected to saddr in TLS mode.
func NewClientTLSOptions ¶
NewClientTLSOptions creates a client connected to saddr over TLS with opts.
func NewClientWithConn ¶
func NewClientWithConn(opts *ClientOptions, nc net.Conn) (*Client, error)
NewClientWithConn creates a client set up with opts connected to nc.
func NewClientWithOptions ¶
func NewClientWithOptions(saddr string, opts *ClientOptions) (*Client, error)
NewClientWithOptions returns a client connected to saddr and set up with opts.
func (*Client) Close ¶
Close closes the connection and shuts down the client.
Any unfinished requests are aborted.
type ClientOptions ¶
ClientOptions allows to setup Client.
An empty value of ClientOptions uses safe defaults: - Protobuf encoding - 8192 bytes per connection for a read buffer - 8192 bytes per connection for a write buffer.
func (*ClientOptions) Copy ¶
func (o *ClientOptions) Copy() *ClientOptions
Copy returns a full copy of o.
type Encoding ¶
type Encoding int8
Encoding specifies which encoding is supported by a concrete server, client, and transport.
type Result ¶
type Result struct {
// contains filtered or unexported fields
}
Result represents a result of a request.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server handles requests from clients by calling methods on registered services.
func NewServer ¶
NewServer returns a server listening on laddr with Protobuf encoding and default options.
func NewServerTLS ¶
NewServerTLS returns a server listening on laddr in TLS mode.
func NewServerTLSOptions ¶
NewServerTLSOptions creates a server listening on laddr in TLS mode with opts.
func NewServerWithListener ¶
func NewServerWithListener(opts *ServerOptions, ln net.Listener) (*Server, error)
NewServerWithListener returns a server set up with opts listening on ln.
func NewServerWithOptions ¶
func NewServerWithOptions(laddr string, opts *ServerOptions) (*Server, error)
NewServerWithOptions returns a server listening on laddr and set up with opts.
func (*Server) Register ¶
Register registers the given svc.
The svc is registered if it's exported and at least one method satisfies the following conditions:
- a method is exported
- a method has three arguments
- the first argument is context.Context
- the second and third arguments are both pointers to exported values that implement proto.Message
- a method has only one return parameter of the type error.
New services can be added while the server is runnning.
func (*Server) Shutdown ¶
Shutdown gracefully shuts down the server. A successful call returns no error.
It iteratively tries to close new and idle connections until no such left, unless/until interrupted by cancellation of ctx. Subsequent calls return ErrSrvClosed.
type ServerOptions ¶
ServerOptions allows to setup Server.
An empty value of ServerOptions uses safe defaults: - Protobuf encoding - 8192 bytes per connection for a read buffer - 8192 bytes per connection for a write buffer.
func (*ServerOptions) Copy ¶
func (o *ServerOptions) Copy() *ServerOptions
Copy returns a full copy of o.
type ServiceError ¶
type ServiceError string
ServiceError represents an error returned by a remote method.
func (ServiceError) Error ¶
func (e ServiceError) Error() string
Error returns the string representation of e.