Nacelle

Go service framework
/

Abstract gRPC server

The github.com/go-nacelle/grpcbase package provides an abstract gRPC server process.


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.

A gRPC process is created by supplying an initializer, described below, that controls its behavior.

1
server := grpcbase.NewServer(NewServerInitializer(), options...)

A server initializer is a struct with an Init method that takes a context 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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
type Initializer struct {}

func (i *Initializer) Init(ctx context.Context, 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.

1
2
3
4
_ = ServerInitializerFunc(func(ctx context.Context, server *grpc.Server) error {
    proto.RegisterRequestCounterServiceServer(server, &RequestCounterService{})
    return nil
})

You can see an additional example of a gRPC process in the example repository, specifically the server initializer.