grpcserver

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package grpcserver bootstraps a gRPC server with the same cross-cutting concerns as the REST stack — panic recovery, trace-id injection, structured access logging, Prometheus metrics, and errs->status mapping — and exposes it as a worker.Runnable so it can be supervised alongside the HTTP server and background workers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Addr is the listen address, e.g. ":9090".
	Addr string
	// ShutdownTimeout bounds graceful shutdown before a hard stop (default 10s).
	ShutdownTimeout time.Duration
	// EnableReflection turns on server reflection (handy for grpcurl in dev).
	EnableReflection bool
	// MetricsNamespace prefixes the gRPC metric names (default "grpc").
	MetricsNamespace string

	// MaxRecvMsgSize / MaxSendMsgSize override the 4 MiB defaults (bytes).
	MaxRecvMsgSize int
	MaxSendMsgSize int
	// NumStreamWorkers bounds the pool of goroutines serving streams; 0 spawns
	// one goroutine per stream (the gRPC default).
	NumStreamWorkers uint32
	// SharedWriteBuffer reuses transport write buffers across RPCs to cut
	// allocations under load.
	SharedWriteBuffer bool
	// Keepalive tunes connection liveness; zero values fall back to gRPC defaults.
	Keepalive KeepaliveConfig
}

Config configures the gRPC server.

type KeepaliveConfig

type KeepaliveConfig struct {
	MaxConnectionIdle   time.Duration
	MaxConnectionAge    time.Duration
	Time                time.Duration
	Timeout             time.Duration
	MinTime             time.Duration
	PermitWithoutStream bool
}

KeepaliveConfig mirrors the gRPC keepalive server parameters and enforcement policy. Zero values are left at gRPC defaults.

type Option

type Option func(*options)

Option customizes the server.

func WithMetrics

func WithMetrics(reg prometheus.Registerer) Option

WithMetrics registers gRPC collectors on reg (e.g. metrics.Metrics.Registry).

func WithTracer

func WithTracer(t trace.Tracer) Option

WithTracer sets the tracer used for trace-id injection. Defaults to a no-op.

func WithUnaryInterceptors

func WithUnaryInterceptors(in ...grpc.UnaryServerInterceptor) Option

WithUnaryInterceptors appends application interceptors, applied after the built-in ones (closest to the handler).

type Server

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

Server wraps a *grpc.Server and implements worker.Runnable.

func New

func New(log *logger.Logger, cfg Config, opts ...Option) *Server

New builds a Server. Register services via ServiceRegistrar before Start.

func (*Server) Name

func (s *Server) Name() string

Name identifies the server in the supervisor and logs.

func (*Server) Serve

func (s *Server) Serve(lis net.Listener) error

Serve serves on the provided listener until Stop. It is useful for tests (e.g. a bufconn listener) and for callers that manage their own listener.

func (*Server) ServiceRegistrar

func (s *Server) ServiceRegistrar() grpc.ServiceRegistrar

ServiceRegistrar returns the registrar used to register gRPC services, e.g. widgetv1.RegisterWidgetServiceServer(s.ServiceRegistrar(), handler).

func (*Server) Start

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

Start binds a TCP listener on the configured address and serves until Stop.

func (*Server) Stop

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

Stop gracefully drains in-flight RPCs, falling back to a hard stop if the shutdown timeout elapses.

Jump to

Keyboard shortcuts

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