Commit a379376c authored by Kirill Smelkov's avatar Kirill Smelkov

X Switch to tracing after event happenned

so there is nice happens-before relation when we receive an event and
start something else after it, not a race.
parent eb5eda25
......@@ -61,9 +61,9 @@ type MyTracer struct {
*xtesting.SyncTracer
}
func (t *MyTracer) TraceNetDial(ev *xnet.TraceDial) { t.Trace1(ev) }
func (t *MyTracer) TraceNetListen(ev *xnet.TraceListen) { t.Trace1(ev) }
func (t *MyTracer) TraceNetTx(ev *xnet.TraceTx) { t.Trace1(ev) }
func (t *MyTracer) TraceNetConnect(ev *xnet.TraceConnect) { t.Trace1(ev) }
func (t *MyTracer) TraceNetListen(ev *xnet.TraceListen) { t.Trace1(ev) }
func (t *MyTracer) TraceNetTx(ev *xnet.TraceTx) { t.Trace1(ev) }
/*
......@@ -127,11 +127,16 @@ func TestMasterStorage(t *testing.T) {
return a
}
// shortcut for net tx events
// shortcut for net tx event
nettx := func(src, dst, pkt string) *xnet.TraceTx {
return &xnet.TraceTx{Src: xaddr(src), Dst: xaddr(dst), Pkt: []byte(pkt)}
}
// shortcut for net connect event
netconnect := func(src, dst, dialed string) *xnet.TraceConnect {
return &xnet.TraceConnect{Src: xaddr(src), Dst: xaddr(dst), Dialed: dialed}
}
Mhost := xnet.NetTrace(net.Host("m"), tracer)
Shost := xnet.NetTrace(net.Host("s"), tracer)
......@@ -165,8 +170,7 @@ func TestMasterStorage(t *testing.T) {
// expect:
//tc.ExpectNetListen("1")
tc.Expect(&xnet.TraceListen{Laddr: ":0"})
tc.Expect(&xnet.TraceDial{Dst: "m:0"})
//tc.ExpectNetDial("0")
tc.Expect(netconnect("s:1", "m:1", "m:0"))
tc.ExpectPar(
......
......@@ -26,32 +26,34 @@ import (
// NetTrace wraps underlying networker with IO tracing layer
//
// Tracing is done via calling trace func right before corresponding packet
// is sent for Tx to underlying network. No synchronization for notification is
// performed - if one is required tracing func must implement such
// synchronization itself.
// Tracing is done via calling trace func right after corresponding networking
// event happenned. No synchronization for notification is performed - if one
// is required tracing func must implement such synchronization itself.
//
// only Tx events are traced:
// - because Write, contrary to Read, never writes partial data on non-error
// - because in case of pipenet tracing writes only is enough to get whole network exchange picture
// only initiation events are traced:
//
// XXX Dial/Listen are also traced
// 1. Tx only (no Rx):
// - because Write, contrary to Read, never writes partial data on non-error
// - because in case of pipenet tracing writes only is enough to get whole network exchange picture
//
// 2. Dial only (no Accept)
// XXX text
func NetTrace(inner Networker, tracer Tracer) Networker {
return &netTrace{inner, tracer}
}
// Tracer is the interface that needs to be implemented by network trace receivers
type Tracer interface {
TraceNetDial(*TraceDial)
TraceNetConnect(*TraceConnect)
TraceNetListen(*TraceListen)
TraceNetTx(*TraceTx)
}
// TraceDial is event corresponding to network dialing
type TraceDial struct {
// TraceConnect is event corresponding to network connection
type TraceConnect struct {
// XXX also put networker?
// XXX also put Src here somehow ?
Dst string
Src, Dst net.Addr
Dialed string
}
// TraceListen is event corresponding to network listening
......@@ -79,23 +81,23 @@ func (nt *netTrace) Network() string {
}
func (nt *netTrace) Dial(ctx context.Context, addr string) (net.Conn, error) {
nt.tracer.TraceNetDial(&TraceDial{Dst: addr})
// XXX +TraceNetDialPost ?
c, err := nt.inner.Dial(ctx, addr)
if err != nil {
return nil, err
}
nt.tracer.TraceNetConnect(&TraceConnect{Src: c.LocalAddr(), Dst: c.RemoteAddr(), Dialed: addr})
return &traceConn{nt, c}, nil
// XXX +TraceNetDialPost ?
}
func (nt *netTrace) Listen(laddr string) (net.Listener, error) {
nt.tracer.TraceNetListen(&TraceListen{Laddr: laddr})
// XXX +TraceNetListenPre ?
l, err := nt.inner.Listen(laddr)
if err != nil {
return nil, err
}
nt.tracer.TraceNetListen(&TraceListen{Laddr: laddr})
return &netTraceListener{nt, l}, nil
// XXX +TraceNetListenPost ?
}
// netTraceListener wraps net.Listener to wrap accepted connections with traceConn
......@@ -119,7 +121,10 @@ type traceConn struct {
}
func (tc *traceConn) Write(b []byte) (int, error) {
tc.nt.tracer.TraceNetTx(&TraceTx{Src: tc.LocalAddr(), Dst: tc.RemoteAddr(), Pkt: b})
return tc.Conn.Write(b)
// XXX +TraceNetTxPost ?
// XXX +TraceNetTxPre ?
n, err := tc.Conn.Write(b)
if err == nil {
tc.nt.tracer.TraceNetTx(&TraceTx{Src: tc.LocalAddr(), Dst: tc.RemoteAddr(), Pkt: b})
}
return n, err
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment