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

fix: fdcostly should take only the prefix into account #5

Merged
4 commits merged into from
Mar 13, 2019
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
12 changes: 10 additions & 2 deletions filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package addrutil

import (
ma "github.com/multiformats/go-multiaddr"
mafmt "github.com/whyrusleeping/mafmt"
)

// SubtractFilter returns a filter func that filters all of the given addresses
Expand All @@ -20,7 +19,16 @@ func SubtractFilter(addrs ...ma.Multiaddr) func(ma.Multiaddr) bool {
// IsFDCostlyTransport returns true for transports that require a new file
// descriptor per connection created
func IsFDCostlyTransport(a ma.Multiaddr) bool {
return mafmt.TCP.Matches(a)
res := false

ma.ForEach(a, func(c ma.Component) bool {
if c.Protocol().Code == ma.P_TCP {
res = true
return false
}
return true
})
return res
}

// FilterNeg returns a negated version of the passed in filter
Expand Down
50 changes: 49 additions & 1 deletion filter_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package addrutil

import (
ma "github.com/multiformats/go-multiaddr"
"testing"

ma "github.com/multiformats/go-multiaddr"
)

func TestSubtractAndNegFilter(t *testing.T) {
Expand Down Expand Up @@ -35,3 +36,50 @@ func TestIsFDCostlyTransport(t *testing.T) {
t.Errorf("Expected address %s to not need a new file descriptor per new connection", udpMa.String())
}
}

func TestIsFDCostly(t *testing.T) {
good := []ma.Multiaddr{
newMultiaddr(t, "/ip4/127.0.0.1/tcp/1234"),
newMultiaddr(t, "/ip4/0.0.0.0/tcp/1234"),
newMultiaddr(t, "/ip6/::1/tcp/1234"),
newMultiaddr(t, "/ip6/::/tcp/1234"),
newMultiaddr(t, "/ip6/fe80::1/tcp/1234"),
newMultiaddr(t, "/ip6/fe80::/tcp/1234"),
newMultiaddr(t, "/ip6/fe80::/tcp/1234/http"),
newMultiaddr(t, "/ip4/127.0.0.1/tcp/1234/http"),
}

bad := []ma.Multiaddr{
newMultiaddr(t, "/ip4/127.0.0.1/udp/1234"),
newMultiaddr(t, "/ip4/0.0.0.0/udp/1234/utp"),
newMultiaddr(t, "/ip6/::1/udp/1234"),
newMultiaddr(t, "/ip6/::/udp/1234"),
}

for _, a := range bad {
if IsFDCostlyTransport(a) {
t.Errorf("addr %s should not be fd costly", a)
}
}

for _, a := range good {
if !IsFDCostlyTransport(a) {
t.Errorf("addr %s should be fd costly", a)
}
}
}

func TestIsFdCostlyMalformed(t *testing.T) {
bad := []ma.Multiaddr{
newMultiaddr(t, "/ip4/127.0.0.1/"),
newMultiaddr(t, "/ip4/0.0.0.0/"),
newMultiaddr(t, "/ip6/::1/"),
newMultiaddr(t, "/ip6/::/"),
}

for _, a := range bad {
if IsFDCostlyTransport(a) {
t.Errorf("addr %s should not be fd costly", a)
}
}
}
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ require (
github.com/ipfs/go-log v0.0.1
github.com/multiformats/go-multiaddr v0.0.1
github.com/multiformats/go-multiaddr-net v0.0.1
github.com/whyrusleeping/mafmt v1.2.8
)
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/whyrusleeping/go-logging v0.0.0-20170515211332-0457bb6b88fc h1:9lDbC6Rz4bwmou+oE6Dt4Cb2BGMur5eR/GYptkKUVHo=
github.com/whyrusleeping/go-logging v0.0.0-20170515211332-0457bb6b88fc/go.mod h1:bopw91TMyo8J3tvftk8xmU2kPmlrt4nScJQZU2hE5EM=
github.com/whyrusleeping/mafmt v1.2.8 h1:TCghSl5kkwEE0j+sU/gudyhVMRlpBin8fMBBHg59EbA=
github.com/whyrusleeping/mafmt v1.2.8/go.mod h1:faQJFPbLSxzD9xpA02ttW/tS9vZykNvXwGvqIpk20FA=
golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67 h1:ng3VDlRp5/DHpSWl02R4rM9I+8M2rhmsuLwAMmkLQWE=
golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/net v0.0.0-20190227160552-c95aed5357e7 h1:C2F/nMkR/9sfUTpvR3QrjBuTdvMUC/cFajkphs1YLQo=
Expand Down