mrpc

package module
v0.0.0-...-ac92542 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 5, 2019 License: MIT Imports: 6 Imported by: 0

README

mrpc-go

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Error

func Error(code ErrCode, text string) error

Error returns an error with the given code and error text. If code is OK, nil will be returned.

func Errorf

func Errorf(code ErrCode, format string, args ...interface{}) error

Errorf returns an error with the given code and the formatted error text. If code is OK, nil will be returned.

func ResponseError

func ResponseError(resp Response) error

ResponseError returns the error for the given response.

Types

type CallInfo

type CallInfo struct {
	Service interface{}
	Method  string
	Body    []byte
}

CallInfo holds details about a method call on the server side.

type Caller

type Caller interface {
	Call(ctx context.Context, req Request) (Response, error)
}

Caller defines an interface for calling remote functions.

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is mrpc client to call service methods. A client is transport independent.

func NewClient

func NewClient(rw io.ReadWriter) *Client

NewClient creates a new mrpc client. When calling a method with Call, the request will be written to the writing part and the response will be read from the reading part of rw.

func (*Client) Call

func (c *Client) Call(ctx context.Context, req Request) (Response, error)

Call calls a remote method by writing the request to the client's writer and reading the response from the client's reader.

type ErrCode

type ErrCode int

ErrCode is an enumeration of supported error codes.

const (
	OK              ErrCode = 0
	Unknown         ErrCode = 1
	Timeout         ErrCode = 2
	NotFound        ErrCode = 3
	AlreadyExists   ErrCode = 4
	InvalidArgument ErrCode = 5
	Unauthorized    ErrCode = 6
	Forbidden       ErrCode = 7
	Internal        ErrCode = 8
	Unavailable     ErrCode = 9
)

Enumerators for ErrCode.

func ErrorCode

func ErrorCode(err error) ErrCode

ErrorCode determines the error code of the given error.

func (*ErrCode) DecodeMsgpack

func (o *ErrCode) DecodeMsgpack(r *msgpack.Reader) error

DecodeMsgpack implements the Decoder interface for ErrCode.

func (ErrCode) EncodeMsgpack

func (o ErrCode) EncodeMsgpack(w *msgpack.Writer) error

EncodeMsgpack implements the Encoder interface for ErrCode.

type Handler

type Handler func(ctx context.Context, svc interface{}, body []byte) ([]byte, error)

Handler is a function type for mrpc handlers. A handler is called for each request and basically defines a service's method.

type MethodSpec

type MethodSpec struct {
	ID      int
	Handler Handler
}

MethodSpec holds the data for a single method. A method has a unique id within the defined service and a handler which completes all incoming requests.

type Registry

type Registry interface {
	Register(svc ServiceSpec)
}

Registry defines an interface for registering a service specification.

type Request

type Request struct {
	Service string
	Method  int
	Headers RequestHeaders
	Body    []byte
}

Request holds the data for a single mrpc request. The target service is specified by its name and must be registered at the corresponding mrpc server. A service contains methods, each identified by an ordinal. With this ordinal the method of the targeted service is specified.

func (*Request) DecodeMsgpack

func (o *Request) DecodeMsgpack(r *msgpack.Reader) error

DecodeMsgpack implements the Decoder interface for Request.

func (*Request) EncodeMsgpack

func (o *Request) EncodeMsgpack(w *msgpack.Writer) (err error)

EncodeMsgpack implements the Encoder interface for Request.

type RequestHeaders

type RequestHeaders struct {
	Timeout uint64
}

RequestHeaders holds all supported header data for a mrpc request.

func (*RequestHeaders) DecodeMsgpack

func (o *RequestHeaders) DecodeMsgpack(r *msgpack.Reader) error

DecodeMsgpack implements the Decoder interface for RequestHeaders.

func (*RequestHeaders) EncodeMsgpack

func (o *RequestHeaders) EncodeMsgpack(w *msgpack.Writer) (err error)

EncodeMsgpack implements the Encoder interface for RequestHeaders.

type Response

type Response struct {
	ErrorCode ErrCode
	ErrorText string
	Body      []byte
}

Response holds the data for an mrpc response. If ErrCode is not OK, an error with the specified error text will be reported to the client. In this case the return value will be ignored. In case of a successful call, the return value will be reported to the client.

func ErrorResponse

func ErrorResponse(err error) Response

ErrorResponse returns a response which indicates the given error.

func ErrorResponsef

func ErrorResponsef(code ErrCode, format string, args ...interface{}) Response

func (*Response) DecodeMsgpack

func (o *Response) DecodeMsgpack(r *msgpack.Reader) error

DecodeMsgpack implements the Decoder interface for Response.

func (*Response) EncodeMsgpack

func (o *Response) EncodeMsgpack(w *msgpack.Writer) (err error)

EncodeMsgpack implements the Encoder interface for Response.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server is a mrpc server, where services can be registered. A server is transport independent and the network layer has to be implemented separately.

func NewServer

func NewServer(o ...ServerOption) (*Server, error)

NewServer creates a new mrpc server with the given options.

func (*Server) Execute

func (s *Server) Execute(ctx context.Context, req Request) Response

Execute executes a single request and calls the corresponding method. If the requested service or method was not registered, an error response will be returned.

func (*Server) Register

func (s *Server) Register(svc ServiceSpec)

Register registers a service to the server. If an invalid service specification is passed or a service with the same id was already registered, the function will panic.

func (*Server) ServeMRPC

func (s *Server) ServeMRPC(ctx context.Context, r io.Reader, w io.Writer) error

ServeMRPC serves a request read from r and writes the response back to w. This function only returns an error, if the encoding of the response fails. All other errors are encoded in the response and written to w. The reader should provide a single request only.

type ServerInterceptor

type ServerInterceptor func(ctx context.Context, call CallInfo, h Handler) ([]byte, error)

ServerInterceptor defines a function type for intercepting a request on the server side. The interceptor is responsible to call h to complete the method call.

type ServerOption

type ServerOption func(*serverOptions) error

ServerOption represents an option which can be used to configure an mrpc server.

func WithServerInterceptor

func WithServerInterceptor(interceptor ServerInterceptor) ServerOption

WithServerInterceptor adds an interceptor for method calls on the server side. It is possible to add multiple interceptors. In thas case they are executed in the order they are provided.

type ServiceSpec

type ServiceSpec struct {
	Name    string
	Service interface{}
	Methods []MethodSpec
}

ServiceSpec holds the data for a service. A service has a unique name and holds the specification for all containing methods.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL