44 "errors"
55 "fmt"
66 "io"
7- "io/ioutil"
87 "net"
98 "sort"
109 "sync"
@@ -20,8 +19,7 @@ const (
2019 baseProtocolLength = uint64 (16 )
2120 baseProtocolMaxMsgSize = 10 * 1024 * 1024
2221
23- pingInterval = 15 * time .Second
24- disconnectGracePeriod = 2 * time .Second
22+ pingInterval = 15 * time .Second
2523)
2624
2725const (
@@ -129,39 +127,27 @@ func (p *Peer) run() DiscReason {
129127 case err := <- readErr :
130128 if r , ok := err .(DiscReason ); ok {
131129 reason = r
132- break
130+ } else {
131+ // Note: We rely on protocols to abort if there is a write
132+ // error. It might be more robust to handle them here as well.
133+ p .DebugDetailf ("Read error: %v\n " , err )
134+ reason = DiscNetworkError
133135 }
134- // Note: We rely on protocols to abort if there is a write
135- // error. It might be more robust to handle them here as well.
136- p .DebugDetailf ("Read error: %v\n " , err )
137- p .conn .Close ()
138- reason = DiscNetworkError
139136 case err := <- p .protoErr :
140137 reason = discReasonForError (err )
141138 case reason = <- p .disc :
142139 }
143140
144141 close (p .closed )
142+ p .politeDisconnect (reason )
145143 p .wg .Wait ()
146- if reason != DiscNetworkError {
147- p .politeDisconnect (reason )
148- }
149144 p .Debugf ("Disconnected: %v\n " , reason )
150145 return reason
151146}
152147
153148func (p * Peer ) politeDisconnect (reason DiscReason ) {
154- done := make (chan struct {})
155- go func () {
149+ if reason != DiscNetworkError {
156150 SendItems (p .rw , discMsg , uint (reason ))
157- // Wait for the other side to close the connection.
158- // Discard any data that they send until then.
159- io .Copy (ioutil .Discard , p .conn )
160- close (done )
161- }()
162- select {
163- case <- done :
164- case <- time .After (disconnectGracePeriod ):
165151 }
166152 p .conn .Close ()
167153}
0 commit comments