Skip to content

Commit

Permalink
Improve closing
Browse files Browse the repository at this point in the history
  • Loading branch information
gagliardetto committed Aug 19, 2023
1 parent c6bca83 commit eadbac4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
4 changes: 2 additions & 2 deletions cmd-rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func newCmd_rpc() *cli.Command {
klog.Infof("Found %d directories; will start watching them for changes ...", len(dirs))
spew.Dump(dirs)

ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(c.Context)
defer cancel()

err = onFileChanged(ctx, dirs, func(event fsnotify.Event) {
Expand Down Expand Up @@ -234,7 +234,7 @@ func newCmd_rpc() *cli.Command {
}
}

return multi.ListenAndServe(listenOn, listenerConfig)
return multi.ListenAndServe(c.Context, listenOn, listenerConfig)
},
}
}
Expand Down
18 changes: 16 additions & 2 deletions multiepoch.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func LoadProxyConfig(configFilepath string) (*ProxyConfig, error) {
}

// ListeAndServe starts listening on the configured address and serves the RPC API.
func (m *MultiEpoch) ListenAndServe(listenOn string, lsConf *ListenerConfig) error {
func (m *MultiEpoch) ListenAndServe(ctx context.Context, listenOn string, lsConf *ListenerConfig) error {
handler := newMultiEpochHandler(m, lsConf)
handler = fasthttp.CompressHandler(handler)

Expand Down Expand Up @@ -206,7 +206,21 @@ func (m *MultiEpoch) ListenAndServe(listenOn string, lsConf *ListenerConfig) err
}

klog.Infof("RPC server listening on %s", listenOn)
return fasthttp.ListenAndServe(listenOn, handler)

s := &fasthttp.Server{
Handler: handler,
MaxRequestBodySize: 1024 * 1024,
}
go func() {
// listen for context cancellation
<-ctx.Done()
klog.Info("RPC server shutting down...")
defer klog.Info("RPC server shut down")
if err := s.ShutdownWithContext(ctx); err != nil {
klog.Errorf("Error while shutting down RPC server: %s", err)
}
}()
return s.ListenAndServe(listenOn)
}

func randomRequestID() string {
Expand Down

0 comments on commit eadbac4

Please sign in to comment.