Documentation ¶
Overview ¶
Package rpc provides an RPC service for non-specific []byte in and []byte out data. Note that this package is usually used to build higher-level rpc packages and not directly.
A simple client works like:
client, err := New(pathToSocket) if err != nil { // Do something } ctx, cancel := context.WitheTimeout(5 * time.Second) resp := Response{} retry: // req and resp are []byte{} if err := client.Call(ctx, "method", req, &resp); err != nil { if Retryable(err) { // Okay, so you probably should do this in a for loop, but I wanted to use // a label for the hell of it. goto retry } // Do something here, cause you have a non-retryable error. }
Note: The server only returns errors to clients when something goes wrong. This makes it predictable on the server side when the error is retryable. When the service has an error, I recommend returning the expected response, which should have a dict containing the error code and the error message. This allows your clients to decide if they should retry a request.
Index ¶
- Constants
- func CredFromCtx(ctx context.Context) uds.Cred
- func Errorf(code ErrType, s string, i ...interface{}) error
- func Retryable(err error) bool
- type Client
- type ErrBadData
- type ErrDeadlineExceeded
- type ErrMethodNotFound
- type ErrServer
- type ErrType
- type ErrUnknown
- type Option
- type RequestHandler
- type Server
Constants ¶
const ( // ETUnknown means to use the ErrUnknown type. ETUnknown = 0 // ETMethodNotFound means to use the ErrMethodNotFound type. ETMethodNotFound = 1 // ETDeadlineExceeded means to use the ErrDeadlineExceeded type. ETDeadlineExceeded = 2 // ETBadData means to use the ErrBadData type. ETBadData = 3 // ETServer means to use the ErrServer type. ETServer = 4 )
Variables ¶
This section is empty.
Functions ¶
func CredFromCtx ¶
CredFromCtx will extract the Cred from the Context object.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client provides an RPC client with []byte in and []byte out.
func New ¶
func New(socketAddr string, uid, gid int, fileModes []os.FileMode, options ...Option) (*Client, error)
New is the constructor for Client. rwc must be a *uds.Client or *uds.Conn.
type ErrBadData ¶
type ErrBadData struct {
// contains filtered or unexported fields
}
ErrBadData indicates that client sent bad data according to the server.
func (ErrBadData) Error ¶
func (e ErrBadData) Error() string
type ErrDeadlineExceeded ¶
type ErrDeadlineExceeded struct {
// contains filtered or unexported fields
}
ErrDeadlineExceeded indicates call deadline was exceeded.
func (ErrDeadlineExceeded) Error ¶
func (e ErrDeadlineExceeded) Error() string
type ErrMethodNotFound ¶
type ErrMethodNotFound struct {
// contains filtered or unexported fields
}
ErrMethodNotFound indicates that the calling method wasn't found on the server.
func (ErrMethodNotFound) Error ¶
func (e ErrMethodNotFound) Error() string
type ErrServer ¶
type ErrServer struct {
// contains filtered or unexported fields
}
ErrServer indicates the server sent back an error message.
type ErrUnknown ¶
type ErrUnknown struct {
// contains filtered or unexported fields
}
ErrUnknown indicates the error type is unknown.
func (ErrUnknown) Error ¶
func (e ErrUnknown) Error() string
type Option ¶
type Option func(c *Client)
Option is an optional argument to New.
func MaxSize ¶
MaxSize is the maximum size a read message is allowed to be. If a message is larger than this, Next() will fail and the underlying connection will be closed.
func SharedPool ¶
SharedPool allows the use of a shared pool of buffers between Client instead of a pool per client. This is useful when clients are short lived and have similar message sizes. Client will panic if the pool does not return a *[]byte object.
type RequestHandler ¶
RequestHandler will receive a Context object with a Deadline set and you can retrieve the calling process creds with CredFromCtx. An error returned is a ErrServer that can not be retried. Generally service errors should be in the resp, not as a returned error. See the note in the package intro.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server provides an json RPC server.
func (*Server) RegisterMethod ¶
func (s *Server) RegisterMethod(method string, handler RequestHandler)
RegisterMethod registers an RPC method with the server.
func (*Server) Stop ¶
Stop stops the server, which will stop listening for new connections. This should slowly kill off existing calls. Stop will return when all calls have completed or the context deadline is reached(or cancelled). A nil error indicates that all jobs were completed. Note: a Server cannot be reused.