]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Don't accept unless connections are wanted
authorMatt Joiner <anacrolix@gmail.com>
Wed, 18 Mar 2015 07:36:27 +0000 (18:36 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Wed, 18 Mar 2015 07:36:27 +0000 (18:36 +1100)
client.go

index 341cb5ec20b8eaf140e1479bde51d5678c05a690..6d3ec782859c26dfec8f47df1167a493880e4de1 100644 (file)
--- a/client.go
+++ b/client.go
@@ -624,8 +624,22 @@ func (cl *Client) ipBlockRange(ip net.IP) (r *iplist.Range) {
        return
 }
 
+func (cl *Client) waitAccept() {
+       cl.mu.Lock()
+       defer cl.mu.Unlock()
+       for {
+               for _, t := range cl.torrents {
+                       if cl.wantConns(t) {
+                               return
+                       }
+               }
+               cl.event.Wait()
+       }
+}
+
 func (cl *Client) acceptConnections(l net.Listener, utp bool) {
        for {
+               cl.waitAccept()
                // We accept all connections immediately, because we don't know what
                // torrent they're for.
                conn, err := l.Accept()
@@ -1668,6 +1682,7 @@ func (me *Client) connectionLoop(t *torrent, c *connection) error {
 }
 
 func (me *Client) dropConnection(torrent *torrent, conn *connection) {
+       me.event.Broadcast()
        for r := range conn.Requests {
                me.connDeleteRequest(torrent, conn, r)
        }