]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Add more worseConn comparisons
authorMatt Joiner <anacrolix@gmail.com>
Mon, 9 Jul 2018 23:50:39 +0000 (09:50 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Mon, 9 Jul 2018 23:50:39 +0000 (09:50 +1000)
Should fix #264.

connection.go
worst_conns.go

index 41eb47bd9160f89cdccd9314402f2f44481a771b..8239f84541bda4a9fd4a4a83930dfd2c96344081 100644 (file)
@@ -1553,3 +1553,15 @@ func (c *connection) setTorrent(t *Torrent) {
        c.t = t
        t.reconcileHandshakeStats(c)
 }
+
+func (c *connection) peerPriority() peerPriority {
+       return bep40PriorityIgnoreError(c.remoteIpPort(), c.t.cl.publicAddr(c.remoteIp()))
+}
+
+func (c *connection) remoteIp() net.IP {
+       return missinggo.AddrIP(c.remoteAddr())
+}
+
+func (c *connection) remoteIpPort() ipPort {
+       return ipPort{missinggo.AddrIP(c.remoteAddr()), uint16(missinggo.AddrPort(c.remoteAddr()))}
+}
index 51a23f12b125c32e66964c348d655673aa2ad209..ad87d1b38f9613791a22eb06016a02f4d93f11f5 100644 (file)
@@ -1,6 +1,10 @@
 package torrent
 
-import "container/heap"
+import (
+       "container/heap"
+       "fmt"
+       "unsafe"
+)
 
 func worseConn(l, r *connection) bool {
        var ml multiLess
@@ -11,7 +15,13 @@ func worseConn(l, r *connection) bool {
        ml.StrictNext(
                l.completedHandshake.Equal(r.completedHandshake),
                l.completedHandshake.Before(r.completedHandshake))
-       return ml.Final()
+       ml.StrictNext(l.peerPriority() == r.peerPriority(), l.peerPriority() < r.peerPriority())
+       ml.StrictNext(l == r, uintptr(unsafe.Pointer(l)) < uintptr(unsafe.Pointer(r)))
+       less, ok := ml.FinalOk()
+       if !ok {
+               panic(fmt.Sprintf("cannot differentiate %#v and %#v", l, r))
+       }
+       return less
 }
 
 type worseConnSlice struct {