Skip to content

Commit

Permalink
Merge pull request #193 from libp2p/move-sorting
Browse files Browse the repository at this point in the history
move AddrList to pstoremen, unexport it
  • Loading branch information
marten-seemann authored Dec 19, 2021
2 parents 2da8c8d + aedfd63 commit f9a486a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 56 deletions.
32 changes: 0 additions & 32 deletions p2p/host/peerstore/addr/sorting_test.go

This file was deleted.

16 changes: 6 additions & 10 deletions p2p/host/peerstore/pstoremem/addr_book.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"sync"
"time"

"github.com/libp2p/go-libp2p-peerstore/addr"

"github.com/libp2p/go-libp2p-core/peer"
pstore "github.com/libp2p/go-libp2p-core/peerstore"
"github.com/libp2p/go-libp2p-core/record"
Expand Down Expand Up @@ -486,18 +484,17 @@ func (mgr *AddrSubManager) AddrStream(ctx context.Context, p peer.ID, initial []
mgr.subs[p] = append(mgr.subs[p], sub)
mgr.mu.Unlock()

sort.Sort(addr.AddrList(initial))
sort.Sort(addrList(initial))

go func(buffer []ma.Multiaddr) {
defer close(out)

sent := make(map[string]bool, len(buffer))
var outch chan ma.Multiaddr

sent := make(map[string]struct{}, len(buffer))
for _, a := range buffer {
sent[string(a.Bytes())] = true
sent[string(a.Bytes())] = struct{}{}
}

var outch chan ma.Multiaddr
var next ma.Multiaddr
if len(buffer) > 0 {
next = buffer[0]
Expand All @@ -516,11 +513,11 @@ func (mgr *AddrSubManager) AddrStream(ctx context.Context, p peer.ID, initial []
next = nil
}
case naddr := <-sub.pubch:
if sent[string(naddr.Bytes())] {
if _, ok := sent[string(naddr.Bytes())]; ok {
continue
}
sent[string(naddr.Bytes())] = struct{}{}

sent[string(naddr.Bytes())] = true
if next == nil {
next = naddr
outch = out
Expand All @@ -532,7 +529,6 @@ func (mgr *AddrSubManager) AddrStream(ctx context.Context, p peer.ID, initial []
return
}
}

}(initial)

return out
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package addr
package pstoremem

import (
"bytes"
Expand All @@ -12,27 +12,20 @@ func isFDCostlyTransport(a ma.Multiaddr) bool {
return mafmt.TCP.Matches(a)
}

type AddrList []ma.Multiaddr
type addrList []ma.Multiaddr

func (al AddrList) Len() int {
return len(al)
}

func (al AddrList) Swap(i, j int) {
al[i], al[j] = al[j], al[i]
}
func (al addrList) Len() int { return len(al) }
func (al addrList) Swap(i, j int) { al[i], al[j] = al[j], al[i] }

func (al AddrList) Less(i, j int) bool {
func (al addrList) Less(i, j int) bool {
a := al[i]
b := al[j]

// dial localhost addresses next, they should fail immediately
lba := manet.IsIPLoopback(a)
lbb := manet.IsIPLoopback(b)
if lba {
if !lbb {
return true
}
if lba && !lbb {
return true
}

// dial utp and similar 'non-fd-consuming' addresses first
Expand Down
20 changes: 20 additions & 0 deletions p2p/host/peerstore/pstoremem/sorting_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package pstoremem

import (
"sort"
"testing"

ma "github.com/multiformats/go-multiaddr"
"github.com/stretchr/testify/require"
)

func TestAddressSorting(t *testing.T) {
u1 := ma.StringCast("/ip4/152.12.23.53/udp/1234/utp")
u2l := ma.StringCast("/ip4/127.0.0.1/udp/1234/utp")
local := ma.StringCast("/ip4/127.0.0.1/tcp/1234")
norm := ma.StringCast("/ip4/6.5.4.3/tcp/1234")

l := addrList{local, u1, u2l, norm}
sort.Sort(l)
require.Equal(t, l, addrList{u2l, u1, local, norm})
}

0 comments on commit f9a486a

Please sign in to comment.