Use missinggo.Event for connection closing event
This commit is contained in:
parent
eb838a894c
commit
3ed628356b
|
@ -1391,10 +1391,8 @@ func (me *Client) connectionLoop(t *torrent, c *connection) error {
|
|||
receivedMessageTypes.Add(strconv.FormatInt(int64(msg.Type), 10), 1)
|
||||
me.mu.Lock()
|
||||
c.lastMessageReceived = time.Now()
|
||||
select {
|
||||
case <-c.closing:
|
||||
if c.closed.IsSet() {
|
||||
return nil
|
||||
default:
|
||||
}
|
||||
if err != nil {
|
||||
if me.stopped() || err == io.EOF {
|
||||
|
@ -1691,10 +1689,8 @@ func (t *torrent) needData() bool {
|
|||
}
|
||||
|
||||
func (cl *Client) usefulConn(t *torrent, c *connection) bool {
|
||||
select {
|
||||
case <-c.closing:
|
||||
if c.closed.IsSet() {
|
||||
return false
|
||||
default:
|
||||
}
|
||||
if !t.haveInfo() {
|
||||
return c.supportsExtension("ut_metadata")
|
||||
|
|
|
@ -10,9 +10,9 @@ import (
|
|||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/anacrolix/missinggo"
|
||||
"github.com/anacrolix/torrent/bencode"
|
||||
pp "github.com/anacrolix/torrent/peer_protocol"
|
||||
)
|
||||
|
@ -36,8 +36,7 @@ type connection struct {
|
|||
encrypted bool
|
||||
Discovery peerSource
|
||||
uTP bool
|
||||
closing chan struct{}
|
||||
mu sync.Mutex // Only for closing.
|
||||
closed missinggo.Event
|
||||
post chan pp.Message
|
||||
writeCh chan []byte
|
||||
|
||||
|
@ -84,7 +83,6 @@ func newConnection() (c *connection) {
|
|||
PeerChoked: true,
|
||||
PeerMaxRequests: 250,
|
||||
|
||||
closing: make(chan struct{}),
|
||||
writeCh: make(chan []byte),
|
||||
post: make(chan pp.Message),
|
||||
}
|
||||
|
@ -235,14 +233,7 @@ func (cn *connection) WriteStatus(w io.Writer, t *torrent) {
|
|||
}
|
||||
|
||||
func (c *connection) Close() {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
select {
|
||||
case <-c.closing:
|
||||
return
|
||||
default:
|
||||
}
|
||||
close(c.closing)
|
||||
c.closed.Set()
|
||||
// TODO: This call blocks sometimes, why?
|
||||
go c.conn.Close()
|
||||
}
|
||||
|
@ -260,7 +251,7 @@ func (c *connection) PeerHasPiece(piece int) bool {
|
|||
func (c *connection) Post(msg pp.Message) {
|
||||
select {
|
||||
case c.post <- msg:
|
||||
case <-c.closing:
|
||||
case <-c.closed.C():
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -422,7 +413,7 @@ func (conn *connection) writer() {
|
|||
conn.Close()
|
||||
return
|
||||
}
|
||||
case <-conn.closing:
|
||||
case <-conn.closed.C():
|
||||
return
|
||||
}
|
||||
} else {
|
||||
|
@ -439,7 +430,7 @@ func (conn *connection) writer() {
|
|||
conn.Close()
|
||||
return
|
||||
}
|
||||
case <-conn.closing:
|
||||
case <-conn.closed.C():
|
||||
return
|
||||
default:
|
||||
connectionWriterFlush.Add(1)
|
||||
|
@ -505,7 +496,7 @@ func (conn *connection) writeOptimizer(keepAliveDelay time.Duration) {
|
|||
if pending.Len() == 0 {
|
||||
timer.Reset(keepAliveDelay)
|
||||
}
|
||||
case <-conn.closing:
|
||||
case <-conn.closed.C():
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
|
@ -146,9 +146,7 @@ func (t *torrent) worstConns(cl *Client) (wcs *worstConns) {
|
|||
cl: cl,
|
||||
}
|
||||
for _, c := range t.Conns {
|
||||
select {
|
||||
case <-c.closing:
|
||||
default:
|
||||
if !c.closed.IsSet() {
|
||||
wcs.c = append(wcs.c, c)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue