p2p: log remote reason when disconnect is requested

The returned reason is currently not used except for the log
message. This change makes the log messages a bit more useful.
The handshake code also returns the remote reason.
This commit is contained in:
Felix Lange 2015-05-14 03:04:04 +02:00
parent 7efeb4bd96
commit 691cb90284
2 changed files with 10 additions and 8 deletions

View File

@ -131,10 +131,11 @@ func (p *Peer) run() DiscReason {
case err := <-p.protoErr: case err := <-p.protoErr:
reason = discReasonForError(err) reason = discReasonForError(err)
case reason = <-p.disc: case reason = <-p.disc:
p.politeDisconnect(reason)
reason = DiscRequested
} }
close(p.closed) close(p.closed)
p.politeDisconnect(reason)
p.wg.Wait() p.wg.Wait()
glog.V(logger.Debug).Infof("%v: Disconnected: %v\n", p, reason) glog.V(logger.Debug).Infof("%v: Disconnected: %v\n", p, reason)
return reason return reason
@ -191,7 +192,7 @@ func (p *Peer) handle(msg Msg) error {
// check errors because, the connection will be closed after it. // check errors because, the connection will be closed after it.
rlp.Decode(msg.Payload, &reason) rlp.Decode(msg.Payload, &reason)
glog.V(logger.Debug).Infof("%v: Disconnect Requested: %v\n", p, reason[0]) glog.V(logger.Debug).Infof("%v: Disconnect Requested: %v\n", p, reason[0])
return DiscRequested return reason[0]
case msg.Code < baseProtocolLength: case msg.Code < baseProtocolLength:
// ignore other base protocol messages // ignore other base protocol messages
return msg.Discard() return msg.Discard()

View File

@ -172,12 +172,13 @@ func TestPeerDisconnect(t *testing.T) {
if err := SendItems(rw, discMsg, DiscQuitting); err != nil { if err := SendItems(rw, discMsg, DiscQuitting); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if err := ExpectMsg(rw, discMsg, []interface{}{DiscRequested}); err != nil { select {
t.Error(err) case reason := <-disc:
} if reason != DiscQuitting {
closer() t.Errorf("run returned wrong reason: got %v, want %v", reason, DiscRequested)
if reason := <-disc; reason != DiscRequested { }
t.Errorf("run returned wrong reason: got %v, want %v", reason, DiscRequested) case <-time.After(500 * time.Millisecond):
t.Error("peer did not return")
} }
} }