Skip to content

Commit

Permalink
added grpc server wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
ssd04 committed May 2, 2024
1 parent 09ec534 commit 6e06b53
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 16 deletions.
5 changes: 4 additions & 1 deletion factory/publisherFactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/multiversx/mx-chain-ws-connector-firehose-go/config"
"github.com/multiversx/mx-chain-ws-connector-firehose-go/process"
"github.com/multiversx/mx-chain-ws-connector-firehose-go/server"
"google.golang.org/grpc"
)

// CreatePublisher will return the required Publisher implementation based on whether the hyperOutportBlock are
Expand All @@ -23,7 +24,9 @@ func CreatePublisher(
return nil, fmt.Errorf("failed to create grpc blocks handler: %w", err)
}

s, err := server.New(cfg.GRPC, handler)
grpcServer := grpc.NewServer()

s, err := server.NewGRPCServerWrapper(grpcServer, cfg.GRPC, handler)
if err != nil {
return nil, fmt.Errorf("failed to create grpc server: %w", err)
}
Expand Down
38 changes: 23 additions & 15 deletions server/grpcServer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"net"

logger "github.com/multiversx/mx-chain-logger-go"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"

"github.com/multiversx/mx-chain-ws-connector-firehose-go/config"
Expand All @@ -19,34 +18,42 @@ var (
log = logger.GetOrCreate("server")
)

type grpcServer struct {
server *grpc.Server
type grpcServer interface {
reflection.GRPCServer
Serve(lis net.Listener) error
GracefulStop()
}

type grpcServerWrapper struct {
server grpcServer
config config.GRPCConfig

cancelFunc context.CancelFunc
}

// New instantiates the underlying grpc server handling rpc requests.
func New(config config.GRPCConfig, blocksHandler process.GRPCBlocksHandler) (*grpcServer, error) {
s := grpc.NewServer()

// NewGRPCServerWrapper instantiates the underlying grpc server handling rpc requests.
func NewGRPCServerWrapper(
grpcServer grpcServer,
config config.GRPCConfig,
blocksHandler process.GRPCBlocksHandler,
) (*grpcServerWrapper, error) {
ctx, cancelFunc := context.WithCancel(context.Background())

Check failure on line 40 in server/grpcServer.go

View workflow job for this annotation

GitHub Actions / golangci linter

lostcancel: the cancelFunc function is not used on all paths (possible context leak) (govet)
service, err := hyperOutportBlock.NewService(ctx, blocksHandler)
if err != nil {
return nil, fmt.Errorf("failed to create service: %w", err)

Check failure on line 43 in server/grpcServer.go

View workflow job for this annotation

GitHub Actions / golangci linter

lostcancel: this return statement may be reached without using the cancelFunc var defined on line 40 (govet)
}
data.RegisterBlockStreamServer(s, service)
reflection.Register(s)
data.RegisterBlockStreamServer(grpcServer, service)
reflection.Register(grpcServer)

return &grpcServer{
server: s,
return &grpcServerWrapper{
server: grpcServer,
config: config,
cancelFunc: cancelFunc,
}, nil
}

// Start will start the grpc server on the configured URL.
func (s *grpcServer) Start() {
func (s *grpcServerWrapper) Start() {
go func() {
err := s.run()
if err != nil {
Expand All @@ -55,7 +62,7 @@ func (s *grpcServer) Start() {
}()
}

func (s *grpcServer) run() error {
func (s *grpcServerWrapper) run() error {
lis, err := net.Listen("tcp", s.config.URL)
if err != nil {
return fmt.Errorf("failed to listen: %v", err)
Expand All @@ -69,14 +76,15 @@ func (s *grpcServer) run() error {
}

// Close will gracefully stop the grpc server.
func (s *grpcServer) Close() {
func (s *grpcServerWrapper) Close() {
if s.cancelFunc != nil {
s.cancelFunc()
}

s.server.GracefulStop()
}

// IsInterfaceNil checks if the underlying server is nil.
func (s *grpcServer) IsInterfaceNil() bool {
func (s *grpcServerWrapper) IsInterfaceNil() bool {
return s == nil
}
45 changes: 45 additions & 0 deletions server/grpcServer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package server_test

import (
"net"
"testing"

"github.com/multiversx/mx-chain-ws-connector-firehose-go/config"
"github.com/multiversx/mx-chain-ws-connector-firehose-go/server"
"github.com/multiversx/mx-chain-ws-connector-firehose-go/testscommon"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
)

type GRPCServerStub struct{}

func (g *GRPCServerStub) RegisterService(desc *grpc.ServiceDesc, impl any) {
}

func (g *GRPCServerStub) GetServiceInfo() map[string]grpc.ServiceInfo {
return make(map[string]grpc.ServiceInfo)
}

func (g *GRPCServerStub) Serve(lis net.Listener) error {
return nil
}

func (g *GRPCServerStub) GracefulStop() {
}

func TestNewGRPCServerWrapper(t *testing.T) {
t.Parallel()

gsv, err := server.NewGRPCServerWrapper(
&GRPCServerStub{},
config.GRPCConfig{
URL: "localhost:8081",
},
&testscommon.GRPCBlocksHandlerStub{},
)
require.Nil(t, err)
require.False(t, gsv.IsInterfaceNil())

gsv.Start()
gsv.Close()
}

0 comments on commit 6e06b53

Please sign in to comment.