Documentation ¶
Overview ¶
Package grpc provides a gRPC entrypoint for go-orb.
Index ¶
- Constants
- Variables
- func WithDefaults(options ...Option) server.Option
- func WithEntrypoint(options ...Option) server.Option
- type Config
- type Option
- func Listener(listener net.Listener) Option
- func WithAddress(addr string) Option
- func WithGRPCOptions(opts ...grpc.ServerOption) Option
- func WithGRPCReflection(reflection bool) Option
- func WithHealthService(health bool) Option
- func WithInsecure(insecure bool) Option
- func WithLogLevel(level slog.Level) Option
- func WithLogPlugin(plugin string) Option
- func WithName(name string) Option
- func WithRegistration(name string, registration server.RegistrationFunc) Option
- func WithStreamInterceptor(name string, interceptor grpc.StreamServerInterceptor, selector ...string) Option
- func WithTLS(config *tls.Config) Option
- func WithTimeout(timeout time.Duration) Option
- func WithUnaryInterceptor(name string, interceptor grpc.UnaryServerInterceptor, selector ...string) Option
- type ServerGRPC
- func (s *ServerGRPC) Address() string
- func (s *ServerGRPC) Config() Config
- func (s *ServerGRPC) EntrypointID() string
- func (s *ServerGRPC) Name() string
- func (s *ServerGRPC) Register(register server.RegistrationFunc)
- func (s *ServerGRPC) Start() error
- func (s *ServerGRPC) Stop(ctx context.Context) error
- func (s *ServerGRPC) String() string
- func (s *ServerGRPC) Transport() string
- func (s *ServerGRPC) Type() string
Constants ¶
const ( // DefaultAddress to listen on. By default a random port will be selected // with a preferably private network interface. Otherwise a public interface. DefaultAddress = ":0" // DefaultInsecure is set to false to make sure the network traffic is entrypted. DefaultInsecure = false // DefaultgRPCReflection enables reflection by default. DefaultgRPCReflection = true // DefaultHealthService enables the health service by default. DefaultHealthService = true // DefaultTimeout is set to 5s. DefaultTimeout = time.Second * 5 )
const Plugin = "grpc"
Plugin name.
Variables ¶
var ( // UnaryInterceptors is a plugin container for unary interceptors middleware. UnaryInterceptors = container.NewMap[string, grpc.UnaryServerInterceptor]() // StreamInterceptors is a plugin container for streaming interceptors middleware. StreamInterceptors = container.NewMap[string, grpc.StreamServerInterceptor]() )
var (
ErrInvalidConfigType = errors.New("http server: invalid config type provided, not of type http.Config")
)
Errors.
Functions ¶
func WithDefaults ¶
WithDefaults sets default options to use on the creation of new gRPC entrypoints.
func WithEntrypoint ¶
WithEntrypoint adds a gRPC entrypoint with the provided options.
Types ¶
type Config ¶
type Config struct { // Name is the entrypoint name. // // The default name is 'grpc-<random uuid>' Name string `json:"name" yaml:"name"` // Address to listen on. // // If no IP is provided, an interface will be selected automatically. Private // interfaces are preferred, if none are found a public interface will be used. // // If no port is provided, a random port will be selected. To listen on a // specific interface, but with a random port, you can use '<IP>:0'. Address string // Insecure will start the gRPC server without TLS. // If set to false, and no TLS certifiate is provided, a self-signed // certificate will be generated. // // WARNING: don't use this in production, unless you really know what you are // doing. this will result in unencrypted traffic. Really, it is even advised // against using this in testing environments. Insecure bool // TLS config, if none is provided a self-signed certificates will be generated. // // You can load a tls config from yaml/json with the following options: // // “`yaml // rootCAFiles: // - xxx // clientCAFiles: // - xxx // clientAuth: "none" | "request" | "require" | "verify" | "require+verify" // certificates: // - certFile: xxx // keyFile: xxx // “` TLS *mtls.Config `json:"tls,omitempty" yaml:"tls,omitempty"` // HandlerRegistrations are all handler registration functions that will be // registered to the server upon startup. // // You can statically add handlers by using the fuctional server options. // Optionally, you can dynamically add handlers by registering them to the // Handlers global, and setting them explicitly in the config. HandlerRegistrations server.HandlerRegistrations `json:"handlers" yaml:"handlers"` // UnaryInterceptors are middlware for unary gRPC calls. These are all // request handlers that don't use streaming. // // Optionally selectors can be provided to the middleware to limit which // requests the middleware is called on. UnaryInterceptors matcher.Matcher[grpc.UnaryServerInterceptor] `json:"middleware" yaml:"middleware"` // StreamInterceptors are middlware for streaming gRPC calls. // // Optionally selectors can be provided to the middleware to limit which // requests the middleware is called on. StreamInterceptors matcher.Matcher[grpc.StreamServerInterceptor] `json:"streamMiddleware" yaml:"streamMiddleware"` // GRPCOptions are options provided by the grpc package, and will be directly // passed ot the gRPC server. GRPCOptions []grpc.ServerOption `json:"-" yaml:"-"` // HealthService dictates whether the gRPC health check protocol should be // implemented. This is an implementation provided by the grpc package. // Defaults to true. // // This is useful for healthprobes, such as in Kubernetes (>=1.24). HealthService bool `json:"health" yaml:"health"` // Reflection dictates whether the server should implementent gRPC // reflection. This is used by e.g. the gRPC proxy. Defaults to true. Reflection bool `json:"reflection" yaml:"reflection"` // Timeout adds a timeout to the request context on the request handler. // // The handler still needs to respect the context timeout for this to have // any effect. Timeout time.Duration `json:"timeout" yaml:"timeout"` // Listener is a custom listener. If none provided one will be created with // the address and TLS config. Listener net.Listener `json:"-" yaml:"-"` // Logger allows you to dynamically change the log level and plugin for a // specific entrypoint. Logger struct { Level slog.Level `json:"level,omitempty" yaml:"level,omitempty"` // TODO(davincible): change with custom level Plugin string `json:"plugin,omitempty" yaml:"plugin,omitempty"` } `json:"logger" yaml:"logger"` }
Config provides options to the gRPC entrypoint.
func NewConfig ¶
func NewConfig() *Config
NewConfig will create a new default config for the entrypoint.
func (*Config) ApplyOptions ¶
ApplyOptions applies a set of options.
func (Config) Copy ¶
func (c Config) Copy() server.EntrypointConfig
Copy creates a copy of the config.
func (Config) GetAddress ¶
GetAddress returns the entrypoint address.
type Option ¶
type Option func(o *Config)
Option is a functional option to provide custom values to the config.
func WithAddress ¶
WithAddress sets the address to listen on.
If no IP is provided, an interface will be selected automatically. Private interfaces are preferred, if none are found a public interface will be used. To listen on all interfaces explicitly set '0.0.0.0:<port>'.
If no port is provided, a random port will be selected. To listen on a specific interface, but with a random port, you can use '<IP>:0'.
func WithGRPCOptions ¶
func WithGRPCOptions(opts ...grpc.ServerOption) Option
WithGRPCOptions with grpc options.
func WithGRPCReflection ¶
WithGRPCReflection dictates whether the server should implementent gRPC reflection. This is used by e.g. the gRPC proxy. Defaults to true.
func WithHealthService ¶
WithHealthService dictates whether the gRPC health check protocol should be implemented. This is an implementation provided by the grpc package. Defaults to true.
This is useful for healthprobes, such as in Kubernetes (>=1.24).
func WithInsecure ¶
WithInsecure will create the entrypoint without using TLS.
WARNING: don't use this in production, unless you really know what you are doing. this will result in unencrypted traffic. Really, it is even advised against using this in testing environments.
func WithLogLevel ¶
WithLogLevel changes the log level from the inherited logger.
func WithLogPlugin ¶
WithLogPlugin changes the log level from the inherited logger.
func WithName ¶
WithName sets the entrypoint name. The default name is in the format of 'grpc-<uuid>'. Setting a custom name allows you to dynamically reference the entrypoint in the file config, and makes it easier to attribute the logs.
func WithRegistration ¶
func WithRegistration(name string, registration server.RegistrationFunc) Option
WithRegistration adds a named registration function to the config. The name set here allows you to dynamically add this handler to entrypoints through a config.
Registration functions are used to register handlers to a server.
func WithStreamInterceptor ¶
func WithStreamInterceptor(name string, interceptor grpc.StreamServerInterceptor, selector ...string) Option
WithStreamInterceptor sets a middleware for streaming gRPC calls.
Optionally, a selctor regex can be provided to limit the scope on which the middleware should be called.
Selector example:
- /* > special case, will be replaced with '.*'
- .*
- /myPkg.myService/*
- /myPkg.myService/Echo
- /myPkg.myService/Echo[1-9]
- Echo$
func WithTimeout ¶
WithTimeout sets the request context timeout for requests.
The handler still needs to respect the context timeout for this to have any effect.
func WithUnaryInterceptor ¶
func WithUnaryInterceptor(name string, interceptor grpc.UnaryServerInterceptor, selector ...string) Option
WithUnaryInterceptor sets a middleware for unary (simple non-streaming) calls.
Optionally, a selctor regex can be provided to limit the scope on which the middleware should be called.
Selector example:
- /* > special case, will be replaced with '.*'
- .*
- /myPkg.myService/*
- /myPkg.myService/Echo
- /myPkg.myService/Echo[1-9]
- Echo$
type ServerGRPC ¶
type ServerGRPC struct {
// contains filtered or unexported fields
}
ServerGRPC is an entrypoint with a gRPC server.
func ProvideServerGRPC ¶
func ProvideServerGRPC( _ types.ServiceName, logger log.Logger, reg registry.Type, cfg Config, opts ...Option, ) (*ServerGRPC, error)
ProvideServerGRPC creates a gRPC server by options.
func (*ServerGRPC) Address ¶
func (s *ServerGRPC) Address() string
Address returns the address the entypoint listens on, for example: 127.0.0.1:8000 .
func (*ServerGRPC) Config ¶
func (s *ServerGRPC) Config() Config
Config returns the server config.
Note that this a copy and you cannot mutate it. Some values such as arrays are pointers, but mutating them either results in undefined behavior, or no change, as they are already processed.
func (*ServerGRPC) EntrypointID ¶
func (s *ServerGRPC) EntrypointID() string
EntrypointID returns the id (uuid) of this entrypoint in the registry.
func (*ServerGRPC) Register ¶
func (s *ServerGRPC) Register(register server.RegistrationFunc)
Register executes a registration function on the entrypoint.
func (*ServerGRPC) Stop ¶
func (s *ServerGRPC) Stop(ctx context.Context) error
Stop stop the gRPC server.
func (*ServerGRPC) String ¶
func (s *ServerGRPC) String() string
String returns the entrypoint type; http.
func (*ServerGRPC) Transport ¶
func (s *ServerGRPC) Transport() string
Transport returns the client transport to use.