mnet: add MListener.CloseOnStop, to allow for http.Server.Shutdown (and other entities) closing the listener instead

This commit is contained in:
Brian Picciano 2019-01-24 19:19:57 -05:00
parent 5f864c44f2
commit a0a531cdd2

View File

@ -16,6 +16,10 @@ import (
type MListener struct { type MListener struct {
net.Listener net.Listener
log *mlog.Logger log *mlog.Logger
// If set to true before mrun's stop event is run, the stop event will not
// cause the MListener to be closed.
NoCloseOnStop bool
} }
// MListen returns an MListener which will be initialized when the start event // MListen returns an MListener which will be initialized when the start event
@ -48,6 +52,9 @@ func MListen(ctx mctx.Context, network, defaultAddr string) *MListener {
// TODO track connections and wait for them to complete before shutting // TODO track connections and wait for them to complete before shutting
// down? // down?
mrun.OnStop(ctx, func(mctx.Context) error { mrun.OnStop(ctx, func(mctx.Context) error {
if l.NoCloseOnStop {
return nil
}
l.log.Info("stopping listener") l.log.Info("stopping listener")
return l.Close() return l.Close()
}) })