Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use deferred initialization of the asnStore #3

Merged
merged 2 commits into from
Aug 25, 2020
Merged
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
42 changes: 33 additions & 9 deletions asn.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,10 @@ import (
"github.com/libp2p/go-cidranger"
)

var Store *asnStore
var Store *indirectAsnStore

func init() {
s, err := NewAsnStore()
if err != nil {
panic(err)
}
Store = s
Store = newIndirectAsnStore()
}

type networkWithAsn struct {
Expand Down Expand Up @@ -53,9 +49,7 @@ func (a *asnStore) AsnForIPv6(ip net.IP) (string, error) {
return n.asn, nil
}

// NewAsnStore returns a `asnStore` that can be queried for the Autonomous System Numbers
// for a given IP address or a multiaddress which contains an IP address.
func NewAsnStore() (*asnStore, error) {
func newAsnStore() (*asnStore, error) {
cr := cidranger.NewPCTrieRanger()

for k, v := range ipv6CidrToAsnMap {
Expand All @@ -71,3 +65,33 @@ func NewAsnStore() (*asnStore, error) {

return &asnStore{cr}, nil
}

type indirectAsnStore struct {
store *asnStore
doneLoading chan struct{}
}

// AsnForIPv6 returns the AS number for the given IPv6 address.
// If no mapping exists for the given IP, this function will
// return an empty ASN and a nil error.
Comment on lines +75 to +76
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a default cidr prefix for unknown mappings?

Copy link
Contributor Author

@aschmahmann aschmahmann Aug 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you elaborate a little more? I'm not understanding the question

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if 2 ips in the same /64 are looked up in a space that isn't in the mapping, will they get the same empty ASN, or different ones?
when we make a new ASN in response to a query, we should maybe save it in the mapping with some default prefix so that other IPs close to it cluster with it.

Copy link
Contributor Author

@aschmahmann aschmahmann Aug 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will they get the same empty ASN, or different ones?

We return "" and when this is used in kbucket we just reject ASNs named "".

Note, this is why we're going to have to change your test in go-ipfs to use an ipv6 associated with a real ASN https://github.com/ipfs/go-ipfs/blob/fe9f49aeb71c753b92ed41922c493ff3bf08ef56/test/integration/wan_lan_dht_test.go#L54

func (a *indirectAsnStore) AsnForIPv6(ip net.IP) (string, error) {
<-a.doneLoading
return a.store.AsnForIPv6(ip)
}

func newIndirectAsnStore() *indirectAsnStore {
a := &indirectAsnStore{
doneLoading: make(chan struct{}),
}

go func() {
defer close(a.doneLoading)
store, err := newAsnStore()
if err != nil {
panic(err)
}
a.store = store
}()

return a
}