server

package
v0.0.0-...-599cc13 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package server provides RPC server functionality for multiplexed connections.

Index

Constants

View Source
const (
	ProtocolMajor = 1
	ProtocolMinor = 0
)

Protocol version constants.

Variables

View Source
var (
	ErrClosed             = errors.New("server closed")
	ErrSessionClosed      = errors.New("session closed")
	ErrMessageTooLarge    = errors.New("message size exceeds limit")
	ErrTooManyConnections = errors.New("too many connections")
)

Common errors.

View Source
var ErrHandlerExists = errors.New("handler already registered")

ErrHandlerExists is returned when trying to register a handler that already exists.

Functions

This section is empty.

Types

type BiDirHandler

type BiDirHandler struct {
	HandleFunc func(ctx context.Context, stream *BiDirStream) error
}

BiDirHandler handles bidirectional streaming RPCs.

func (BiDirHandler) Type

func (h BiDirHandler) Type() msgs.RPCType

Type returns the RPC type this handler handles.

type BiDirStream

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

BiDirStream provides bidirectional communication for handlers.

func (*BiDirStream) Context

func (s *BiDirStream) Context() context.Context

Context returns the context for this stream.

func (*BiDirStream) Err

func (s *BiDirStream) Err() error

Err returns any error that occurred during the stream.

func (*BiDirStream) Recv

func (s *BiDirStream) Recv() iter.Seq[[]byte]

Recv returns an iterator over received payloads. When context is cancelled, it drains and yields any buffered messages before returning.

func (*BiDirStream) Send

func (s *BiDirStream) Send(payload []byte) error

Send sends a payload to the client.

func (*BiDirStream) SetTrailer

func (s *BiDirStream) SetTrailer(md metadata.MD)

SetTrailer sets metadata to be sent with the Close message. This can be called multiple times; subsequent calls will merge metadata.

func (*BiDirStream) Trailer

func (s *BiDirStream) Trailer() metadata.MD

Trailer returns the trailer metadata that was set.

type Handler

type Handler interface {
	Type() msgs.RPCType
}

Handler is the interface implemented by all RPC type handlers.

type HandlerInfo

type HandlerInfo struct {
	Package string
	Service string
	Call    string
	Type    msgs.RPCType
}

HandlerInfo contains information about a registered handler.

type Option

type Option func(*Server)

Option configures a Server.

func WithCompression

func WithCompression(alg msgs.Compression) Option

WithCompression sets the default compression algorithm for server responses. Use msgs.CmpNone to disable compression (default).

func WithMaxConcurrentRPCs

func WithMaxConcurrentRPCs(max int) Option

WithMaxConcurrentRPCs sets the maximum number of concurrent RPC handlers. This uses a limited worker pool to restrict concurrency. Default is 0 (no limit, uses the context's pool directly).

func WithMaxConnections

func WithMaxConnections(max int) Option

WithMaxConnections sets the maximum number of concurrent connections. New connections are rejected with ErrTooManyConnections when at limit. Default is 0 (no limit).

func WithMaxRecvMsgSize

func WithMaxRecvMsgSize(size int) Option

WithMaxRecvMsgSize sets the maximum size for received messages. Messages larger than this will be rejected with ErrMessageTooLarge. Default is 4 MiB.

func WithMaxSendMsgSize

func WithMaxSendMsgSize(size int) Option

WithMaxSendMsgSize sets the maximum size for sent messages. Messages larger than this will cause the send to fail with ErrMessageTooLarge. Default is 4 MiB.

func WithPacking

func WithPacking(enabled bool) Option

WithPacking enables Cap'n Proto-style message packing for connections. When enabled, the server will agree to packing if requested by the client. Packing can significantly reduce message size by eliminating zero bytes.

func WithStreamInterceptor

func WithStreamInterceptor(interceptors ...interceptor.StreamServerInterceptor) Option

WithStreamInterceptor adds stream interceptors to the server. Multiple calls chain the interceptors; they execute in the order provided.

func WithUnaryInterceptor

func WithUnaryInterceptor(interceptors ...interceptor.UnaryServerInterceptor) Option

WithUnaryInterceptor adds unary interceptors to the server. Multiple calls chain the interceptors; they execute in the order provided.

type RecvHandler

type RecvHandler struct {
	HandleFunc func(ctx context.Context, stream *SendStream) error
}

RecvHandler handles server-send streaming RPCs (server sends, client receives).

func (RecvHandler) Type

func (h RecvHandler) Type() msgs.RPCType

Type returns the RPC type this handler handles.

type RecvStream

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

RecvStream allows the server to receive messages from the client. Used by SendHandler (client sends, server receives).

func (*RecvStream) Context

func (s *RecvStream) Context() context.Context

Context returns the context for this stream.

func (*RecvStream) Err

func (s *RecvStream) Err() error

Err returns any error that occurred during the stream.

func (*RecvStream) Recv

func (s *RecvStream) Recv() iter.Seq[[]byte]

Recv returns an iterator over received payloads. When context is cancelled, it drains and yields any buffered messages before returning.

func (*RecvStream) SetTrailer

func (s *RecvStream) SetTrailer(md metadata.MD)

SetTrailer sets metadata to be sent with the Close message. This can be called multiple times; subsequent calls will merge metadata.

func (*RecvStream) Trailer

func (s *RecvStream) Trailer() metadata.MD

Trailer returns the trailer metadata that was set.

type Registry

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

Registry manages handler registration for RPC services.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates a new handler registry.

func (*Registry) Handlers

func (r *Registry) Handlers() iter.Seq[HandlerInfo]

Handlers returns an iterator over all registered handlers.

func (*Registry) Lookup

func (r *Registry) Lookup(pkg, service, call string) (Handler, bool)

Lookup finds a handler for the given package/service/call.

func (*Registry) LookupByDescr

func (r *Registry) LookupByDescr(descr msgs.Descr) (Handler, bool)

LookupByDescr finds a handler using a Descr message.

func (*Registry) Register

func (r *Registry) Register(ctx context.Context, pkg, service, call string, h Handler) error

Register registers a handler for a specific package/service/call combination.

type SendHandler

type SendHandler struct {
	HandleFunc func(ctx context.Context, stream *RecvStream) error
}

SendHandler handles client-send streaming RPCs (client sends, server receives).

func (SendHandler) Type

func (h SendHandler) Type() msgs.RPCType

Type returns the RPC type this handler handles.

type SendStream

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

SendStream allows the server to send messages to the client. Used by RecvHandler (server sends, client receives).

func (*SendStream) Context

func (s *SendStream) Context() context.Context

Context returns the context for this stream.

func (*SendStream) Send

func (s *SendStream) Send(payload []byte) error

Send sends a payload to the client.

func (*SendStream) SetTrailer

func (s *SendStream) SetTrailer(md metadata.MD)

SetTrailer sets metadata to be sent with the Close message. This can be called multiple times; subsequent calls will merge metadata.

func (*SendStream) Trailer

func (s *SendStream) Trailer() metadata.MD

Trailer returns the trailer metadata that was set.

type Server

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

Server handles RPC connections and dispatches to registered handlers.

func New

func New(opts ...Option) *Server

New creates a new RPC server.

func (*Server) IsDraining

func (s *Server) IsDraining() bool

IsDraining returns true if the server is shutting down (no new connections accepted).

func (*Server) Register

func (s *Server) Register(ctx context.Context, pkg, service, call string, handler Handler) error

Register registers a handler for a specific package/service/call combination.

func (*Server) Registry

func (s *Server) Registry() *Registry

Registry returns the server's handler registry. This is useful for reflection and introspection of registered services.

func (*Server) Serve

func (s *Server) Serve(ctx context.Context, transport io.ReadWriteCloser) error

Serve handles a single connection, spawning session goroutines via context.Pool(ctx). This blocks until the connection is closed or an error occurs.

func (*Server) Shutdown

func (s *Server) Shutdown(ctx context.Context) error

Shutdown gracefully shuts down the server. It stops accepting new connections, sends GoAway to all active connections, waits for in-flight RPCs to complete, then closes all connections. The context controls how long to wait; if cancelled, remaining connections are forcefully closed.

type ServerConn

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

ServerConn handles a single client connection.

func (*ServerConn) Close

func (c *ServerConn) Close() error

Close closes the connection immediately without waiting for handlers.

func (*ServerConn) GracefulClose

func (c *ServerConn) GracefulClose(ctx context.Context) error

GracefulClose gracefully closes the connection, waiting for in-flight handlers to complete. The context controls how long to wait; if it's cancelled or times out, remaining handlers are forcefully terminated.

Returns nil if all handlers completed gracefully, or an error if the context was cancelled before all handlers finished.

func (*ServerConn) IsDraining

func (c *ServerConn) IsDraining() bool

IsDraining returns true if the connection is draining (no new sessions allowed).

type SyncHandler

type SyncHandler struct {
	HandleFunc func(ctx context.Context, req []byte, md []msgs.Metadata) ([]byte, error)
}

SyncHandler handles synchronous request/response RPCs.

func (SyncHandler) Type

func (h SyncHandler) Type() msgs.RPCType

Type returns the RPC type this handler handles.

Jump to

Keyboard shortcuts

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