Documentation
¶
Overview ¶
Package gorpc provides a small Go-to-Go RPC transport for internal services.
It is intentionally not a protobuf, gRPC, Connect, or IDL replacement. Both sides share normal Go request and response types, and the wire protocol uses length-prefixed MessagePack frames over a single full-duplex connection.
Index ¶
- Constants
- Variables
- func Call[Req, Resp any](ctx context.Context, client *Client, service, method string, req Req) (Resp, error)
- func MustRegister[Req, Resp any](s *Server, service, method string, fn HandlerFunc[Req, Resp])
- func Register[Req, Resp any](s *Server, service, method string, fn HandlerFunc[Req, Resp]) error
- type Client
- type ClientOptions
- type Codec
- type Frame
- type FrameType
- type HandlerFunc
- type MessagePackCodec
- type RemoteError
- type Server
- type ServerOptions
Constants ¶
const ( ErrorCodeCanceled = "canceled" ErrorCodeDeadlineExceeded = "deadline_exceeded" ErrorCodeInternal = "internal" ErrorCodeInvalidRequest = "invalid_request" ErrorCodeNotFound = "not_found" )
Remote error codes used by the built-in server and helpers.
const CodecMessagePack = "msgpack"
CodecMessagePack is the v1 MessagePack codec name used during handshake.
const DefaultHandshakeTimeout = 5 * time.Second
DefaultHandshakeTimeout is the default timeout for the initial protocol handshake.
const DefaultMaxFrameSize int64 = 16 * 1024 * 1024
DefaultMaxFrameSize is the default maximum encoded frame size.
const ProtocolVersion uint16 = 1
ProtocolVersion is the current GoRPC wire protocol version.
Variables ¶
var ( ErrClosed = errors.New("gorpc: closed") ErrDuplicateRoute = errors.New("gorpc: duplicate route") ErrInvalidRoute = errors.New("gorpc: invalid route") )
Common GoRPC errors.
var ( ErrFrameTooLarge = errors.New("gorpc: frame too large") ErrProtocol = errors.New("gorpc: protocol error") )
Frame read/write errors.
Functions ¶
func Call ¶
func Call[Req, Resp any](ctx context.Context, client *Client, service, method string, req Req) (Resp, error)
Call performs a typed unary request/response call.
func MustRegister ¶
func MustRegister[Req, Resp any](s *Server, service, method string, fn HandlerFunc[Req, Resp])
MustRegister is Register that panics on error.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a single full-duplex connection to a GoRPC server.
func (*Client) RemoteService ¶
RemoteService returns the service name reported by the server handshake.
type ClientOptions ¶
type ClientOptions struct {
ClientName string
ExpectedServiceName string
Codec Codec
MaxFrameSize int64
HandshakeTimeout time.Duration
Logger *slog.Logger
Dialer *net.Dialer
}
ClientOptions configures Dial.
type Codec ¶
type Codec interface {
Name() string
Marshal(v any) ([]byte, error)
Unmarshal(data []byte, v any) error
}
Codec marshals frame envelopes and method payloads.
type Frame ¶
type Frame struct {
Version uint16 `msgpack:"version"`
Type FrameType `msgpack:"type"`
RequestID uint64 `msgpack:"request_id,omitempty"`
Service string `msgpack:"service,omitempty"`
Method string `msgpack:"method,omitempty"`
DeadlineUnixNano int64 `msgpack:"deadline_unix_nano,omitempty"`
Payload []byte `msgpack:"payload,omitempty"`
}
Frame is the v1 wire envelope. It is MessagePack-encoded and written with a 4-byte big-endian length prefix.
type FrameType ¶
type FrameType uint8
FrameType identifies the kind of message carried by a frame.
type HandlerFunc ¶
HandlerFunc is the typed function shape used by registered unary methods.
type MessagePackCodec ¶
type MessagePackCodec struct{}
MessagePackCodec is the default v1 codec.
func (MessagePackCodec) Marshal ¶
func (MessagePackCodec) Marshal(v any) ([]byte, error)
Marshal encodes v as MessagePack.
func (MessagePackCodec) Name ¶
func (MessagePackCodec) Name() string
Name returns the handshake name for MessagePackCodec.
type RemoteError ¶
type RemoteError struct {
Code string `msgpack:"code" json:"code"`
Message string `msgpack:"message" json:"message"`
Details map[string]any `msgpack:"details,omitempty" json:"details,omitempty"`
}
RemoteError is sent in FrameError payloads and returned by callers when the server handled the request but rejected or failed it.
func NewRemoteError ¶
func NewRemoteError(code, message string, details map[string]any) *RemoteError
NewRemoteError creates a structured error suitable for returning from a handler.
func (*RemoteError) Error ¶
func (e *RemoteError) Error() string
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server accepts GoRPC connections and dispatches registered methods.
func NewServer ¶
func NewServer(opts ServerOptions) *Server
NewServer creates a Server with default codec and limits where options are unset.
func (*Server) ListenAndServe ¶
ListenAndServe listens on network/address and serves GoRPC connections.