-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from ipfs/feat/addr-stream
Add peerstore method to subscribe to new addresses
- Loading branch information
Showing
5 changed files
with
396 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package addr | ||
|
||
import ( | ||
"bytes" | ||
|
||
ma "github.com/jbenet/go-multiaddr" | ||
manet "github.com/jbenet/go-multiaddr-net" | ||
mafmt "github.com/whyrusleeping/mafmt" | ||
) | ||
|
||
func isFDCostlyTransport(a ma.Multiaddr) bool { | ||
return mafmt.TCP.Matches(a) | ||
} | ||
|
||
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) 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 | ||
} | ||
} | ||
|
||
// dial utp and similar 'non-fd-consuming' addresses first | ||
fda := isFDCostlyTransport(a) | ||
fdb := isFDCostlyTransport(b) | ||
if !fda { | ||
if fdb { | ||
return true | ||
} | ||
|
||
// if neither consume fd's, assume equal ordering | ||
return false | ||
} | ||
|
||
// if 'b' doesnt take a file descriptor | ||
if !fdb { | ||
return false | ||
} | ||
|
||
// if 'b' is loopback and both take file descriptors | ||
if lbb { | ||
return false | ||
} | ||
|
||
// for the rest, just sort by bytes | ||
return bytes.Compare(a.Bytes(), b.Bytes()) > 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package addr | ||
|
||
import ( | ||
"sort" | ||
"testing" | ||
) | ||
|
||
func TestAddressSorting(t *testing.T) { | ||
u1 := newAddrOrFatal(t, "/ip4/152.12.23.53/udp/1234/utp") | ||
u2l := newAddrOrFatal(t, "/ip4/127.0.0.1/udp/1234/utp") | ||
local := newAddrOrFatal(t, "/ip4/127.0.0.1/tcp/1234") | ||
norm := newAddrOrFatal(t, "/ip4/6.5.4.3/tcp/1234") | ||
|
||
l := AddrList{local, u1, u2l, norm} | ||
sort.Sort(l) | ||
|
||
if !l[0].Equal(u2l) { | ||
t.Fatal("expected utp local addr to be sorted first: ", l[0]) | ||
} | ||
|
||
if !l[1].Equal(u1) { | ||
t.Fatal("expected utp addr to be sorted second") | ||
} | ||
|
||
if !l[2].Equal(local) { | ||
t.Fatal("expected tcp localhost addr thid") | ||
} | ||
|
||
if !l[3].Equal(norm) { | ||
t.Fatal("expected normal addr last") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.