aboutsummaryrefslogtreecommitdiffstats
path: root/p2p/peer.go
diff options
context:
space:
mode:
authorFelix Lange <fjl@twurst.com>2017-02-24 16:58:04 +0800
committerFelix Lange <fjl@twurst.com>2017-02-28 17:20:29 +0800
commit96ae35e2ac8c360781407d7294081aabdcbb3652 (patch)
tree378cc7aae99127a4e85a721e5cd319ced87e103a /p2p/peer.go
parent35e8308bf76df11c3c1e8d11fac782814583ea5c (diff)
downloadgo-tangerine-96ae35e2ac8c360781407d7294081aabdcbb3652.tar.gz
go-tangerine-96ae35e2ac8c360781407d7294081aabdcbb3652.tar.zst
go-tangerine-96ae35e2ac8c360781407d7294081aabdcbb3652.zip
p2p, p2p/discover, p2p/nat: rework logging using context keys
Diffstat (limited to 'p2p/peer.go')
-rw-r--r--p2p/peer.go43
1 files changed, 21 insertions, 22 deletions
diff --git a/p2p/peer.go b/p2p/peer.go
index 5d09927a5..a9c20189a 100644
--- a/p2p/peer.go
+++ b/p2p/peer.go
@@ -17,7 +17,6 @@
package p2p
import (
- "errors"
"fmt"
"io"
"net"
@@ -25,6 +24,7 @@ import (
"sync"
"time"
+ "github.com/ethereum/go-ethereum/common/mclock"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/p2p/discover"
"github.com/ethereum/go-ethereum/rlp"
@@ -64,6 +64,8 @@ type protoHandshake struct {
type Peer struct {
rw *conn
running map[string]*protoRW
+ log log.Logger
+ created mclock.AbsTime
wg sync.WaitGroup
protoErr chan error
@@ -125,20 +127,25 @@ func newPeer(conn *conn, protocols []Protocol) *Peer {
p := &Peer{
rw: conn,
running: protomap,
+ created: mclock.Now(),
disc: make(chan DiscReason),
protoErr: make(chan error, len(protomap)+1), // protocols + pingLoop
closed: make(chan struct{}),
+ log: log.New("id", conn.id, "conn", conn.flags),
}
return p
}
-func (p *Peer) run() DiscReason {
+func (p *Peer) Log() log.Logger {
+ return p.log
+}
+
+func (p *Peer) run() (remoteRequested bool, err error) {
var (
writeStart = make(chan struct{}, 1)
writeErr = make(chan error, 1)
readErr = make(chan error, 1)
- reason DiscReason
- requested bool
+ reason DiscReason // sent to the peer
)
p.wg.Add(2)
go p.readLoop(readErr)
@@ -152,31 +159,26 @@ func (p *Peer) run() DiscReason {
loop:
for {
select {
- case err := <-writeErr:
+ case err = <-writeErr:
// A write finished. Allow the next write to start if
// there was no error.
if err != nil {
- log.Trace(fmt.Sprintf("%v: write error: %v", p, err))
reason = DiscNetworkError
break loop
}
writeStart <- struct{}{}
- case err := <-readErr:
+ case err = <-readErr:
if r, ok := err.(DiscReason); ok {
- log.Debug(fmt.Sprintf("%v: remote requested disconnect: %v", p, r))
- requested = true
+ remoteRequested = true
reason = r
} else {
- log.Trace(fmt.Sprintf("%v: read error: %v", p, err))
reason = DiscNetworkError
}
break loop
- case err := <-p.protoErr:
+ case err = <-p.protoErr:
reason = discReasonForError(err)
- log.Debug(fmt.Sprintf("%v: protocol error: %v (%v)", p, err, reason))
break loop
- case reason = <-p.disc:
- log.Debug(fmt.Sprintf("%v: locally requested disconnect: %v", p, reason))
+ case err = <-p.disc:
break loop
}
}
@@ -184,10 +186,7 @@ loop:
close(p.closed)
p.rw.close(reason)
p.wg.Wait()
- if requested {
- reason = DiscRequested
- }
- return reason
+ return remoteRequested, err
}
func (p *Peer) pingLoop() {
@@ -297,14 +296,14 @@ func (p *Peer) startProtocols(writeStart <-chan struct{}, writeErr chan<- error)
proto.closed = p.closed
proto.wstart = writeStart
proto.werr = writeErr
- log.Trace(fmt.Sprintf("%v: Starting protocol %s/%d", p, proto.Name, proto.Version))
+ p.log.Trace(fmt.Sprintf("Starting protocol %s/%d", proto.Name, proto.Version))
go func() {
err := proto.Run(p, proto)
if err == nil {
- log.Trace(fmt.Sprintf("%v: Protocol %s/%d returned", p, proto.Name, proto.Version))
- err = errors.New("protocol returned")
+ p.log.Trace(fmt.Sprintf("Protocol %s/%d returned", proto.Name, proto.Version))
+ err = errProtocolReturned
} else if err != io.EOF {
- log.Trace(fmt.Sprintf("%v: Protocol %s/%d error: %v", p, proto.Name, proto.Version, err))
+ p.log.Trace(fmt.Sprintf("Protocol %s/%d failed", proto.Name, proto.Version), "err", err)
}
p.protoErr <- err
p.wg.Done()