mnet: ListenerOnStart -> MListen, and have it close on mrun.Stop
This commit is contained in:
parent
0ef6997f40
commit
0fb0cb92a0
13
mnet/mnet.go
13
mnet/mnet.go
@ -11,12 +11,13 @@ import (
|
||||
"github.com/mediocregopher/mediocre-go-lib/mrun"
|
||||
)
|
||||
|
||||
// ListenerOnStart returns a Listener which will be initialized when the start
|
||||
// event is triggered on ctx (see mrun.Start).
|
||||
// MListen returns a Listener which will be initialized when the start event is
|
||||
// triggered on ctx (see mrun.Start), and closed when the stop event is
|
||||
// triggered on ctx (see mrun.Stop).
|
||||
//
|
||||
// network defaults to "tcp" if empty. defaultAddr defaults to ":0" if empty,
|
||||
// and will be configurable via mcfg.
|
||||
func ListenerOnStart(ctx mctx.Context, network, defaultAddr string) net.Listener {
|
||||
func MListen(ctx mctx.Context, network, defaultAddr string) net.Listener {
|
||||
if network == "" {
|
||||
network = "tcp"
|
||||
}
|
||||
@ -35,6 +36,12 @@ func ListenerOnStart(ctx mctx.Context, network, defaultAddr string) net.Listener
|
||||
return nil
|
||||
})
|
||||
|
||||
// TODO track connections and wait for them to complete before shutting
|
||||
// down?
|
||||
mrun.OnStop(ctx, func(mctx.Context) error {
|
||||
return l.Close()
|
||||
})
|
||||
|
||||
return &l
|
||||
}
|
||||
|
||||
|
@ -37,9 +37,9 @@ func TestIsReservedIP(t *T) {
|
||||
))
|
||||
}
|
||||
|
||||
func TestListen(t *T) {
|
||||
func TestMListen(t *T) {
|
||||
ctx := mctx.New()
|
||||
l := ListenerOnStart(ctx, "", "")
|
||||
l := MListen(ctx, "", "")
|
||||
if err := mcfg.Populate(ctx, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if err := mrun.Start(ctx); err != nil {
|
||||
@ -65,6 +65,7 @@ func TestListen(t *T) {
|
||||
t.Fatalf("read %q from conn", b)
|
||||
}
|
||||
|
||||
conn.Close()
|
||||
l.Close()
|
||||
if err := mrun.Stop(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user