chore(rpc): graceful shutdown for RPC server (#463)
graceful shutdown for grpc service.
This commit is contained in:
parent
bcd0e70252
commit
2113dfc84e
8
main.go
8
main.go
|
@ -271,11 +271,15 @@ func main() {
|
||||||
|
|
||||||
var g errgroup.Group
|
var g errgroup.Group
|
||||||
|
|
||||||
|
// Run httpd server
|
||||||
g.Go(func() error {
|
g.Go(func() error {
|
||||||
return gorush.RunHTTPServer(ctx)
|
return gorush.RunHTTPServer(ctx)
|
||||||
}) // Run httpd server
|
})
|
||||||
|
|
||||||
g.Go(rpc.RunGRPCServer) // Run gRPC internal server
|
// Run gRPC internal server
|
||||||
|
g.Go(func() error {
|
||||||
|
return rpc.RunGRPCServer(ctx)
|
||||||
|
})
|
||||||
|
|
||||||
// check job completely
|
// check job completely
|
||||||
g.Go(func() error {
|
g.Go(func() error {
|
||||||
|
|
|
@ -1,13 +1,14 @@
|
||||||
package rpc
|
package rpc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net"
|
"context"
|
||||||
|
"net/http"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/appleboy/gorush/gorush"
|
"github.com/appleboy/gorush/gorush"
|
||||||
"github.com/appleboy/gorush/rpc/proto"
|
"github.com/appleboy/gorush/rpc/proto"
|
||||||
|
"golang.org/x/sync/errgroup"
|
||||||
|
|
||||||
"golang.org/x/net/context"
|
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
"google.golang.org/grpc/codes"
|
"google.golang.org/grpc/codes"
|
||||||
"google.golang.org/grpc/reflection"
|
"google.golang.org/grpc/reflection"
|
||||||
|
@ -98,28 +99,34 @@ func (s *Server) Send(ctx context.Context, in *proto.NotificationRequest) (*prot
|
||||||
}
|
}
|
||||||
|
|
||||||
// RunGRPCServer run gorush grpc server
|
// RunGRPCServer run gorush grpc server
|
||||||
func RunGRPCServer() error {
|
func RunGRPCServer(ctx context.Context) error {
|
||||||
if !gorush.PushConf.GRPC.Enabled {
|
if !gorush.PushConf.GRPC.Enabled {
|
||||||
gorush.LogAccess.Info("gRPC server is disabled.")
|
gorush.LogAccess.Info("gRPC server is disabled.")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
lis, err := net.Listen("tcp", ":"+gorush.PushConf.GRPC.Port)
|
|
||||||
if err != nil {
|
|
||||||
gorush.LogError.Errorf("failed to listen: %v", err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
s := grpc.NewServer()
|
s := grpc.NewServer()
|
||||||
srv := NewServer()
|
rpcSrv := NewServer()
|
||||||
proto.RegisterGorushServer(s, srv)
|
proto.RegisterGorushServer(s, rpcSrv)
|
||||||
proto.RegisterHealthServer(s, srv)
|
proto.RegisterHealthServer(s, rpcSrv)
|
||||||
// Register reflection service on gRPC server.
|
// Register reflection service on gRPC server.
|
||||||
reflection.Register(s)
|
reflection.Register(s)
|
||||||
gorush.LogAccess.Debug("gRPC server is running on " + gorush.PushConf.GRPC.Port + " port.")
|
gorush.LogAccess.Info("gRPC server is running on " + gorush.PushConf.GRPC.Port + " port.")
|
||||||
if err := s.Serve(lis); err != nil {
|
|
||||||
gorush.LogError.Errorf("failed to serve: %v", err)
|
srv := &http.Server{
|
||||||
return err
|
Addr: ":" + gorush.PushConf.GRPC.Port,
|
||||||
|
Handler: s,
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
var g errgroup.Group
|
||||||
|
g.Go(func() error {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return srv.Shutdown(ctx)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
g.Go(func() error {
|
||||||
|
return srv.ListenAndServe()
|
||||||
|
})
|
||||||
|
return g.Wait()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue