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

gracefully shutdown grpc #30

Merged
merged 1 commit into from
Dec 8, 2021
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
30 changes: 22 additions & 8 deletions pkg/api/runtime/grpc/grpc_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,7 @@ func (srv *grpcApi) Refresh() error {
}

func (srv *grpcApi) Serve(ctx context.Context) error {
srv.server = &rusiServerImpl{
refreshChannels: []chan bool{},
mainCtx: ctx,
publishHandler: srv.publishHandler,
subscribeHandler: srv.subscribeHandler,
}
srv.server = newRusiServer(ctx, srv.publishHandler, srv.subscribeHandler)
grpcServer := grpc.NewServer(srv.serverOptions...)
v1.RegisterRusiServer(grpcServer, srv.server)

Expand All @@ -66,17 +61,33 @@ func (srv *grpcApi) Serve(ctx context.Context) error {
}
}()

return grpcServer.Serve(lis)
err = grpcServer.Serve(lis)
//wait for unsubscribe
srv.server.subsWaitGroup.Wait()
return err
}

type rusiServerImpl struct {
mu sync.RWMutex
mainCtx context.Context
subsWaitGroup *sync.WaitGroup
refreshChannels []chan bool
publishHandler messaging.PublishRequestHandler
subscribeHandler messaging.SubscribeRequestHandler
}

func newRusiServer(ctx context.Context,
publishHandler messaging.PublishRequestHandler,
subscribeHandler messaging.SubscribeRequestHandler) *rusiServerImpl {
return &rusiServerImpl{
refreshChannels: []chan bool{},
mainCtx: ctx,
subsWaitGroup: &sync.WaitGroup{},
publishHandler: publishHandler,
subscribeHandler: subscribeHandler,
}
}

func (srv *rusiServerImpl) Refresh() error {
srv.mu.RLock()
defer srv.mu.RUnlock()
Expand Down Expand Up @@ -110,6 +121,9 @@ func (srv *rusiServerImpl) removeRefreshChan(refreshChan chan bool) {

// Subscribe creates a subscription
func (srv *rusiServerImpl) Subscribe(stream v1.Rusi_SubscribeServer) error {
srv.subsWaitGroup.Add(1)
defer srv.subsWaitGroup.Done()

//block until subscriptionRequest is received
r, err := stream.Recv()
if err != nil {
Expand Down Expand Up @@ -228,7 +242,7 @@ func startAckReceiverForStream(subAckMap map[string]*subAck, mu *sync.RWMutex, s
for {
select {
case <-stream.Context().Done():
klog.V(4).ErrorS(stream.Context().Err(), "stopping ack stream watcher")
//klog.V(4).ErrorS(stream.Context().Err(), "stopping ack stream watcher")
return
default:
r, err := stream.Recv() //blocks
Expand Down
7 changes: 1 addition & 6 deletions pkg/api/runtime/grpc/grpc_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,12 +476,7 @@ var lis *bufconn.Listener

func startServer(t *testing.T, ctx context.Context, publishHandler messaging.PublishRequestHandler,
subscribeHandler messaging.SubscribeRequestHandler) *rusiServerImpl {
server := &rusiServerImpl{
mainCtx: ctx,
refreshChannels: []chan bool{},
publishHandler: publishHandler,
subscribeHandler: subscribeHandler,
}
server := newRusiServer(ctx, publishHandler, subscribeHandler)
lis = bufconn.Listen(bufSize)
grpcServer := grpc.NewServer()
v1.RegisterRusiServer(grpcServer, server)
Expand Down