Abstract gRPC server process for nacelle.
This library supplies an abstract gRPC server process whose behavior can be configured by implementing a ServerInitializer
interface. For a more full-featured gRPC server framework built on nacelle, see scarf.
You can see an additional example of a gRPC process in the example repository, specifically the server initializer.
A gRPC process is created by supplying an initializer, described below, that controls its behavior.
server := grpcbase.NewServer(NewServerInitializer(), options...)
A server initializer is a struct with an Init
method that takes a config object and an grpc.Server as parameters. This method may return an error value, which signals a fatal error to the process that runs it. This method provides an extension point to register services to the server instance before the process accepts connections.
The following example registers a gRPC service to the server that will atomically increment a request counter and return it in a payload defined in the proto
package that also contains the service definition.
type Initializer struct {}
func (i *Initializer) Init(config nacelle.Config, server *http.Server) error {
proto.RegisterRequestCounterServiceServer(server, &RequestCounterService{})
return nil
}
type RequestCounterService {
requests uint
}
func (kvs *RequestCounterService) Get(ctx context.Context, r *proto.Request) (*proto.Response, error) {
value := atomic.AddUint32(&i.requests)
return &proto.Response{count: value}, nil
}
A simple server initializer stuct that does not need additional methods, state, or dependency instances injected via a service container can use the server initializer function wrapper instead.
ServerInitializerFunc(func(config nacelle.Config, server *grpc.Server) error {
proto.RegisterRequestCounterServiceServer(server, &RequestCounterService{})
return nil
})
The following options can be supplied to the server constructor to tune its behavior.
- WithTagModifiers
- WithTagModifiers registers the tag modifiers to be used when loading process configuration (see below). This can be used to change default hosts and ports, or prefix all target environment variables in the case where more than one gRPC server is registered per application (e.g. health server and application server, data plane and control plane server).
- WithServerOptions
- WithServerOptions registers options to be supplied directly to the gRPC server constructor.
The default process behavior can be configured by the following environment variables.
Environment Variable | Default | Description |
---|---|---|
GRPC_HOST | 0.0.0.0 | The host on which to accept connections. |
GRPC_PORT | 5000 | The port on which to accept connections. |