Skip to content

Commit

Permalink
fixes #252 Add logging to ease debugging connection reattempts
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Krieger committed Nov 27, 2016
1 parent 74b6487 commit 49c2090
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
9 changes: 9 additions & 0 deletions core.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ type socket struct {

// Port hook -- called when a port is added or removed
porthook PortHook

// Debugger -- buffer to collect connection reattempt messages
debugger logger
}

func (sock *socket) addPipe(tranpipe Pipe, d *dialer, l *listener) *pipe {
Expand Down Expand Up @@ -560,6 +563,10 @@ func (sock *socket) SetPortHook(newhook PortHook) PortHook {
return oldhook
}

func (sock *socket) Debug() string {
return sock.debugger.String()
}

type dialer struct {
d PipeDialer
sock *socket
Expand Down Expand Up @@ -614,6 +621,7 @@ func (d *dialer) dialer() {
for {
p, err := d.d.Dial()
if err == nil {
d.sock.debugger.Logf("connected to %s", d.addr)
// reset retry time
rtime = d.sock.reconntime
d.sock.Lock()
Expand All @@ -632,6 +640,7 @@ func (d *dialer) dialer() {
}

// we're redialing here
d.sock.debugger.Logf("failed to dial %s, retrying: %v", d.addr, err)
select {
case <-d.closeq: // dialer closed
return
Expand Down
38 changes: 38 additions & 0 deletions log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package mangos

import (
"bytes"
"fmt"
"sync"
)

type logger struct {
sync.Mutex
buf bytes.Buffer
}

func (l *logger) Log(a ...interface{}) {
l.Lock()
defer l.Unlock()
l.buf.WriteString(fmt.Sprint(a...))
l.buf.WriteByte('\n')
}

func (l *logger) Logf(format string, a ...interface{}) {
l.Lock()
defer l.Unlock()
l.buf.WriteString(fmt.Sprintf(format, a...))
l.buf.WriteByte('\n')
}

func (l *logger) String() string {
l.Lock()
defer l.Unlock()
return l.buf.String()
}

func (l *logger) Clear() {
l.Lock()
defer l.Unlock()
l.buf.Reset()
}
3 changes: 3 additions & 0 deletions socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,7 @@ type Socket interface {
// added or removed from this socket (connect/disconnect). The previous
// hook is returned (nil if none.)
SetPortHook(PortHook) PortHook

// Debug returns a string containing
Debug() string
}

0 comments on commit 49c2090

Please sign in to comment.