Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(rpc): graceful shutdown for RPC server #463

Merged
merged 1 commit into from
Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,15 @@ func main() {

var g errgroup.Group

// Run httpd server
g.Go(func() error {
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
g.Go(func() error {
Expand Down
39 changes: 23 additions & 16 deletions rpc/server.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package rpc

import (
"net"
"context"
"net/http"
"sync"

"github.com/appleboy/gorush/gorush"
"github.com/appleboy/gorush/rpc/proto"
"golang.org/x/sync/errgroup"

"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/reflection"
Expand Down Expand Up @@ -98,28 +99,34 @@ func (s *Server) Send(ctx context.Context, in *proto.NotificationRequest) (*prot
}

// RunGRPCServer run gorush grpc server
func RunGRPCServer() error {
func RunGRPCServer(ctx context.Context) error {
if !gorush.PushConf.GRPC.Enabled {
gorush.LogAccess.Info("gRPC server is disabled.")
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()
srv := NewServer()
proto.RegisterGorushServer(s, srv)
proto.RegisterHealthServer(s, srv)
rpcSrv := NewServer()
proto.RegisterGorushServer(s, rpcSrv)
proto.RegisterHealthServer(s, rpcSrv)
// Register reflection service on gRPC server.
reflection.Register(s)
gorush.LogAccess.Debug("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)
return err
gorush.LogAccess.Info("gRPC server is running on " + gorush.PushConf.GRPC.Port + " port.")

srv := &http.Server{
Addr: ":" + gorush.PushConf.GRPC.Port,
Handler: s,
}

return nil
var g errgroup.Group
g.Go(func() error {
select {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S1000: should use a simple channel send/receive instead of select with a single case (from gosimple)

case <-ctx.Done():
return srv.Shutdown(ctx)
}
})
g.Go(func() error {
return srv.ListenAndServe()
})
return g.Wait()
}