Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alesr/grpcworker
Go gRPC worker compatible with Voi SVC https://github.com/voi-oss/svc
https://github.com/alesr/grpcworker
Last synced: about 2 months ago
JSON representation
Go gRPC worker compatible with Voi SVC https://github.com/voi-oss/svc
- Host: GitHub
- URL: https://github.com/alesr/grpcworker
- Owner: alesr
- Created: 2021-04-04T16:29:53.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2023-10-25T22:29:21.000Z (about 1 year ago)
- Last Synced: 2024-06-20T14:24:48.578Z (7 months ago)
- Language: Go
- Size: 6.79 MB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# gRPCWorker
The grpcworker package provides a worker implementation for running a gRPC server with unary and stream interceptors. It is designed to be flexible and customizable through the use of options.
## Usage
```go
package mainimport (
"fmt"
"net""github.com/voi-oss/svc"
"go.uber.org/zap"
"google.golang.org/grpc""github.com/alesr/grpcworker"
)type MyApp struct{}
// Implement methods of your gRPC service interface
func main() {
// Create your application instance
app := MyApp{}// Create a gRPC service descriptor
serviceDesc := grpc.ServiceDesc{
ServiceName: "service-name",
HandlerType: (*MyApp)(nil),
}// Create your unary interceptor
yourUnaryInterceptor := func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
// Do something before handling the request
resp, _ := handler(ctx, req)
// Do something after handling the request
return resp, nil
}// Create your stream interceptor
yourStreamInterceptor := func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
// Do something before handling the request
_ := handler(srv, ss)
// Do something after handling the request
return nil
}logger, _ := zap.NewDevelopment()
lis, _ := net.Listen("tcp", ":50051")// Create a new worker with optional interceptors
worker := grpcworker.New(logger, lis, &app, &serviceDesc,
grpcworker.WithUnaryInterceptor(yourUnaryInterceptor),
grpcworker.WithStreamInterceptor(yourStreamInterceptor),
)// Initialize the worker
if err := worker.Init(logger); err != nil {
logger.Fatal("failed to initialize worker", zap.Error(err))
}// Start the gRPC server
if err := worker.Run(); err != nil {
logger.Fatal("failed to start server", zap.Error(err))
}// Terminate the server gracefully
if err := worker.Terminate(); err != nil {
logger.Fatal("failed to terminate server", zap.Error(err))
}
}
```