mnet: export MListener and add some debug logging

This commit is contained in:
Brian Picciano 2019-01-24 16:54:38 -05:00
parent 405120513f
commit bd4d7fc9e3
2 changed files with 32 additions and 7 deletions

View File

@ -7,31 +7,33 @@ import (
"github.com/mediocregopher/mediocre-go-lib/mcfg" "github.com/mediocregopher/mediocre-go-lib/mcfg"
"github.com/mediocregopher/mediocre-go-lib/mctx" "github.com/mediocregopher/mediocre-go-lib/mctx"
"github.com/mediocregopher/mediocre-go-lib/merr"
"github.com/mediocregopher/mediocre-go-lib/mlog" "github.com/mediocregopher/mediocre-go-lib/mlog"
"github.com/mediocregopher/mediocre-go-lib/mrun" "github.com/mediocregopher/mediocre-go-lib/mrun"
) )
type listener struct { // MListener is returned by MListen and simply wraps a net.Listener.
type MListener struct {
net.Listener net.Listener
log *mlog.Logger log *mlog.Logger
} }
// MListen returns a Listener which will be initialized when the start event is // MListen returns an MListener which will be initialized when the start event
// triggered on ctx (see mrun.Start), and closed when the stop event is // is triggered on ctx (see mrun.Start), and closed when the stop event is
// triggered on ctx (see mrun.Stop). // triggered on ctx (see mrun.Stop).
// //
// network defaults to "tcp" if empty. defaultAddr defaults to ":0" if empty, // network defaults to "tcp" if empty. defaultAddr defaults to ":0" if empty,
// and will be configurable via mcfg. // and will be configurable via mcfg.
func MListen(ctx mctx.Context, network, defaultAddr string) net.Listener { func MListen(ctx mctx.Context, network, defaultAddr string) *MListener {
if network == "" { if network == "" {
network = "tcp" network = "tcp"
} }
if defaultAddr == "" { if defaultAddr == "" {
defaultAddr = ":0" defaultAddr = ":0"
} }
addr := mcfg.String(ctx, "addr", defaultAddr, network+" address to listen on in format [host]:port. If port is 0 then a random one will be chosen") addr := mcfg.String(ctx, "listen-addr", defaultAddr, network+" address to listen on in format [host]:port. If port is 0 then a random one will be chosen")
l := new(listener) l := new(MListener)
l.log = mlog.From(ctx).WithKV(l) l.log = mlog.From(ctx).WithKV(l)
mrun.OnStart(ctx, func(mctx.Context) error { mrun.OnStart(ctx, func(mctx.Context) error {
@ -53,7 +55,28 @@ func MListen(ctx mctx.Context, network, defaultAddr string) net.Listener {
return l return l
} }
func (l *listener) KV() map[string]interface{} { // Accept wraps a call to Accept on the underlying net.Listener, providing debug
// logging.
func (l *MListener) Accept() (net.Conn, error) {
conn, err := l.Listener.Accept()
if err != nil {
return conn, err
}
l.log.Debug("connection accepted", mlog.KV{"remoteAddr": conn.RemoteAddr()})
return conn, nil
}
// Close wraps a call to Close on the underlying net.Listener, providing debug
// logging.
func (l *MListener) Close() error {
l.log.Debug("listener closing")
err := l.Listener.Close()
l.log.Debug("listener closed", merr.KV(err))
return err
}
// KV implements the mlog.KVer interface.
func (l *MListener) KV() map[string]interface{} {
return map[string]interface{}{"addr": l.Addr().String()} return map[string]interface{}{"addr": l.Addr().String()}
} }

View File

@ -8,6 +8,7 @@ import (
"github.com/mediocregopher/mediocre-go-lib/mcfg" "github.com/mediocregopher/mediocre-go-lib/mcfg"
"github.com/mediocregopher/mediocre-go-lib/mctx" "github.com/mediocregopher/mediocre-go-lib/mctx"
"github.com/mediocregopher/mediocre-go-lib/mlog"
"github.com/mediocregopher/mediocre-go-lib/mrun" "github.com/mediocregopher/mediocre-go-lib/mrun"
"github.com/mediocregopher/mediocre-go-lib/mtest/massert" "github.com/mediocregopher/mediocre-go-lib/mtest/massert"
) )
@ -39,6 +40,7 @@ func TestIsReservedIP(t *T) {
func TestMListen(t *T) { func TestMListen(t *T) {
ctx := mctx.ChildOf(mctx.New(), "test") ctx := mctx.ChildOf(mctx.New(), "test")
mlog.CtxSet(ctx, mlog.From(ctx).WithMaxLevel(mlog.DebugLevel))
l := MListen(ctx, "", "") l := MListen(ctx, "", "")
if err := mcfg.Populate(ctx, nil); err != nil { if err := mcfg.Populate(ctx, nil); err != nil {