Skip to content
This repository has been archived by the owner on Aug 19, 2022. It is now read-only.

move AddrList to pstoremen, unexport it #193

Merged
merged 1 commit into from
Dec 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 0 additions & 32 deletions addr/sorting_test.go

This file was deleted.

16 changes: 6 additions & 10 deletions 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
21 changes: 7 additions & 14 deletions addr/sorting.go → pstoremem/sorting.go
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 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})
}