* solve https://github.com/appleboy/gorush/issues/476

* add logging to start gRPC server (tcp listener)
change grpc.Server.Stop -> grpc.Server.GracefulStop
add grpc server_test
remove async functions from rpc/server.go

* add logging to err in rpc/server.go
This commit is contained in:
Slava Romanov
2020-03-10 16:09:03 +03:00
committed by GitHub
parent eb136d137a
commit d06e4a2337
2 changed files with 60 additions and 19 deletions

View File

@@ -2,13 +2,11 @@ package rpc
import (
"context"
"net/http"
"net"
"sync"
"time"
"github.com/appleboy/gorush/gorush"
"github.com/appleboy/gorush/rpc/proto"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
@@ -110,27 +108,24 @@ func RunGRPCServer(ctx context.Context) error {
rpcSrv := NewServer()
proto.RegisterGorushServer(s, rpcSrv)
proto.RegisterHealthServer(s, rpcSrv)
// Register reflection service on gRPC server.
reflection.Register(s)
gorush.LogAccess.Info("gRPC server is running on " + gorush.PushConf.GRPC.Port + " port.")
srv := &http.Server{
Addr: ":" + gorush.PushConf.GRPC.Port,
Handler: s,
lis, err := net.Listen("tcp", ":"+gorush.PushConf.GRPC.Port)
if err != nil {
gorush.LogError.Fatalln(err)
return err
}
var g errgroup.Group
g.Go(func() error {
gorush.LogAccess.Info("gRPC server is running on " + gorush.PushConf.GRPC.Port + " port.")
go func() {
select {
case <-ctx.Done():
timeout := time.Duration(gorush.PushConf.Core.ShutdownTimeout) * time.Second
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
return srv.Shutdown(ctx)
s.GracefulStop() // graceful shutdown
}
})
g.Go(func() error {
return srv.ListenAndServe()
})
return g.Wait()
}()
if err = s.Serve(lis); err != nil {
gorush.LogError.Fatalln(err)
}
return err
}