grpc

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2026 License: MIT Imports: 53 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

go/grpc/server.go

Index

Constants

View Source
const (
	HeaderXForwardedProto = "X-Forwarded-Proto"
	HeaderXForwardedHost  = "X-Forwarded-Host"
	HeaderXOriginalURL    = "X-Original-Url"
	HeaderXRequestBody    = "X-Request-Body-Bin"
	HeaderXHTTPMethod     = "X-Http-Method"
)
View Source
const (
	HTTPHeaderLocation     = "Location"
	HTTPHeaderCacheControl = "Cache-Control"
)

///////////// FORWARD RESPONSE OPTIONS //////////////

View Source
const (
	// maximum message size for the server (20 MB)
	MaximumMessageSize = 20 * 1024 * 1024
)

Variables

This section is empty.

Functions

func CodeFromHTTPStatus

func CodeFromHTTPStatus(status int) codes.Code

CodeFromHTTPStatus converts an HTTP status code to the corresponding gRPC code. This follows the inverse mapping of HTTPStatusFromCode as defined in the gRPC spec. If the HTTP status code doesn't have a direct mapping, it returns codes.Unknown.

See: https://github.com/googleapis/googleapis/blob/master/google/rpc/code.proto

func ForwardCustomHeaders

func ForwardCustomHeaders(ctx context.Context) context.Context

func GetHTTPMethod

func GetHTTPMethod(ctx context.Context) string

func IsStandardHeader

func IsStandardHeader(key string) bool

IsStandardHeader returns true if the given key is a standard gRPC/HTTP2 header:

  • ":" HTTP/2 pseudo-headers (:authority, :method, :path, :scheme).
  • "grpc-" gRPC reserved headers (grpc-timeout, grpc-encoding, grpc-status, etc.).
  • "content-type" required by gRPC, always "application/grpc".
  • "user-agent" set automatically by gRPC clients.
  • "te" HTTP/2 transfer encoding, always "trailers".
  • "authorization" standard HTTP authentication header.

func SetHTTPHeader

func SetHTTPHeader(ctx context.Context, key, value string) error

func WithDNSBalancer

func WithDNSBalancer() grpc.DialOption

withDNSBalancer returns gRPC DialOption that does client-side load balancing based on DNS.

func WithRawCodec

func WithRawCodec() grpc.CallOption

Types

type Connection

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

Connection is a gRPC client.

func NewConnection

func NewConnection(opts *Opts, certsOpts *certs.Opts, prometheusOpts *prometheus.Opts) (*Connection, error)

NewConnection creates and returns a new gRPC client.

func (*Connection) Close

func (c *Connection) Close() error

func (*Connection) Connect

func (c *Connection) Connect(ctx context.Context) error

Connect dials the gRPC connection and returns it, as well as a health.ProbeFN, to encourage any client to use the probe fn as a health check.

func (*Connection) Get

func (c *Connection) Get() *grpc.ClientConn

func (*Connection) HealthCheckFn

func (c *Connection) HealthCheckFn(service string) health.Check

HealthCheck calls the `Check` method of the grpc server, specifying the service.

func (*Connection) WithLogger

func (c *Connection) WithLogger(logger *slog.Logger) *Connection

func (*Connection) WithMetadata

func (c *Connection) WithMetadata(key, value string) *Connection

WithMetadata adds a static metadata key-value pair to every unary and stream call.

func (*Connection) WithOptions

func (c *Connection) WithOptions(options ...grpc.DialOption) *Connection

WithOptions adds options to this gRPC client.

func (*Connection) WithStreamInterceptors

func (c *Connection) WithStreamInterceptors(interceptors ...grpc.StreamClientInterceptor) *Connection

WithStreamInterceptors adds interceptors to this gRPC client.

func (*Connection) WithUnaryInterceptors

func (c *Connection) WithUnaryInterceptors(interceptors ...grpc.UnaryClientInterceptor) *Connection

WithUnaryInterceptors adds interceptors to this gRPC client.

type Gateway

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

Gateway is a gRPC gateway.

func NewGateway

func NewGateway(opts *GatewayOpts, grpcOpts *Opts, certsOpts *certs.Opts, prometheusOpts *prometheus.Opts, registerHandlers []RegisterHandler) *Gateway

NewGateway creates and returns a new Gateway.

func (*Gateway) GracefulStop

func (g *Gateway) GracefulStop() error

GracefulStop gracefully stops the gateway server.

func (*Gateway) Serve

func (g *Gateway) Serve(ctx context.Context) error

Serve serves this gRPC gateway. Blocking call.

func (*Gateway) Stop

func (g *Gateway) Stop() error

Stop immediately stops the gateway server.

func (*Gateway) WithClientStreamInterceptors

func (g *Gateway) WithClientStreamInterceptors(interceptors ...grpc.StreamClientInterceptor) *Gateway

WithClientStreamInterceptors adds interceptors to this gateway's gRPC client..

func (*Gateway) WithClientUnaryInterceptors

func (g *Gateway) WithClientUnaryInterceptors(interceptors ...grpc.UnaryClientInterceptor) *Gateway

WithClientUnaryInterceptors adds interceptors to this gateway's gRPC client..

func (*Gateway) WithDialOptions

func (g *Gateway) WithDialOptions(options ...grpc.DialOption) *Gateway

WithDialOptions adds dial options to this gRPC gateway.

func (*Gateway) WithLogger

func (g *Gateway) WithLogger(logger *slog.Logger) *Gateway

func (*Gateway) WithOptions

func (g *Gateway) WithOptions(options ...runtime.ServeMuxOption) *Gateway

WithOptions adds options to this gRPC gateway.

type GatewayCookie

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

GatewayCookie namespaces methods for grpc-gateway cookies.

func (*GatewayCookie) GetHTTPCookie

func (gc *GatewayCookie) GetHTTPCookie(ctx context.Context, name string) (*grpcpb.HttpCookie, error)

GetHTTPCookie retrieves the http cookie with the given name from the context.

func (*GatewayCookie) GetHTTPCookies

func (*GatewayCookie) GetHTTPCookies(ctx context.Context) ([]*grpcpb.HttpCookie, error)

GetHTTPCookies retrieves any http cookies from a context.

func (*GatewayCookie) SetHTTPCookies

func (*GatewayCookie) SetHTTPCookies(ctx context.Context, httpCookies ...*grpcpb.HttpCookie) error

SetHTTPCookies is used by a server to send cookies through grpc headers, to the grpc gateway during an RPC call.

type GatewayOpts

type GatewayOpts struct {
	Port                   int      `long:"port" env:"PORT" description:"Port to serve the GRPC gateway on." default:"8080"`
	Host                   string   `long:"host" env:"HOST" description:"Host for a client to connect to."`
	GracefulStopTimeout    int      `` /* 127-byte string literal not displayed */
	AllowedIncomingHeaders []string `` /* 143-byte string literal not displayed */
	AllowedOutgoingHeaders []string `` /* 146-byte string literal not displayed */
}

GatewayOpts holds a gRPC gateway server opts.

type Opts

type Opts struct {
	Health              *health.GRPCOpts `group:"Health" namespace:"health" env-namespace:"HEALTH"`
	Port                int              `long:"port" env:"PORT" description:"Port to serve gRPC on." default:"9090"`
	Host                string           `long:"host" env:"HOST" description:"Host for a client to connect to."`
	DisableTLS          bool             `long:"disable-tls" env:"DISABLE_TLS" description:"Set to true in order to disable TLS for this service."`
	GracefulStopTimeout int              `` /* 127-byte string literal not displayed */
	SocketPath          string           `long:"socket-path" env:"SOCKET_PATH" description:"Unix socket path to use instead of TCP port"`
	EnableReflection    bool             `long:"enable-reflection" env:"ENABLE_REFLECTION" description:"enable the reflection api"`
}

Opts holds a gRPC server opts.

func ParseOpts

func ParseOpts(endpoint string) (*Opts, error)

func (*Opts) Endpoint

func (o *Opts) Endpoint() string

Returns the target endpoint.

type RegisterHandler

type RegisterHandler = func(ctx context.Context, mux *runtime.ServeMux, endpoint string, opts []grpc.DialOption) (err error)

RegisterHandler is syntactice sugar for a gRPC gateway handler.

type Server

type Server struct {
	Raw *grpc.Server
	// contains filtered or unexported fields
}

Server is a gRPC server.

func NewServer

func NewServer(opts *Opts, certsOpts *certs.Opts, prometheusOpts *prometheus.Opts, name string, register func(*Server)) *Server

NewServer creates and returns a new Server.

func (*Server) GetHealthServer

func (s *Server) GetHealthServer() *health.GRPCServer

func (*Server) GracefulStop

func (s *Server) GracefulStop() error

func (*Server) Serve

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

Serve instantiates the gRPC server and blocks forever.

func (*Server) Stop

func (s *Server) Stop() error

func (*Server) WithFileDescriptorSet

func (s *Server) WithFileDescriptorSet(bytes []byte) *Server

func (*Server) WithLogger

func (s *Server) WithLogger(logger *slog.Logger) *Server

func (*Server) WithOptions

func (s *Server) WithOptions(options ...grpc.ServerOption) *Server

WithOptions adds options to this gRPC server.

func (*Server) WithStreamInterceptors

func (s *Server) WithStreamInterceptors(interceptors ...grpc.StreamServerInterceptor) *Server

WithStreamInterceptors adds interceptors to this gRPC server. These interceptors are added *AFTER* the default pre interceptors and *BEFORE* the default post interceptors.

func (*Server) WithUnaryInterceptors

func (s *Server) WithUnaryInterceptors(interceptors ...grpc.UnaryServerInterceptor) *Server

WithUnaryInterceptors adds interceptors to this gRPC server. These interceptors are added *AFTER* the default pre interceptors and *BEFORE* the default post interceptors.

type ServerOptions

type ServerOptions struct {
	UnaryInterceptors  []grpc.UnaryServerInterceptor
	StreamInterceptors []grpc.StreamServerInterceptor
	GRPCOptions        []grpc.ServerOption
}

ServerOptions holds config for a server.

type WebCookie

type WebCookie struct{}

WebCookie namespaces methods for grpc-web cookies.

func (WebCookie) GetHTTPCookie

func (WebCookie) GetHTTPCookie(ctx context.Context, name string) *grpcpb.HttpCookie

GetHTTPCookie retrieves the http cookie with the given name from the context.

func (WebCookie) GetHTTPCookies

func (WebCookie) GetHTTPCookies(ctx context.Context) []*grpcpb.HttpCookie

GetHTTPCookies retrieves any http cookies from a context.

func (WebCookie) SetHTTPCookies

func (WebCookie) SetHTTPCookies(ctx context.Context, httpCookies ...*grpcpb.HttpCookie) error

SetHTTPCookies adds cookies to a grpc header with the `set-cookie`key. This is used to send cookies back to grpc-web clients.

Directories

Path Synopsis
Package grpcinproc provides utilities for working with gRPC streaming RPCs.
Package grpcinproc provides utilities for working with gRPC streaming RPCs.
go/grpc/middleware/errors.go
go/grpc/middleware/errors.go
Package mockserver provides a gRPC server that stands in for a third party in tests.
Package mockserver provides a gRPC server that stands in for a third party in tests.

Jump to

Keyboard shortcuts

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