Commit 38b74d19 authored by Adin Schmahmann's avatar Adin Schmahmann

feat: use deferred initialization of the asnStore

parent 1b967eb4
...@@ -8,14 +8,10 @@ import ( ...@@ -8,14 +8,10 @@ import (
"github.com/libp2p/go-cidranger" "github.com/libp2p/go-cidranger"
) )
var Store *asnStore var Store *indirectAsnStore
func init() { func init() {
s, err := NewAsnStore() Store = &indirectAsnStore{}
if err != nil {
panic(err)
}
Store = s
} }
type networkWithAsn struct { type networkWithAsn struct {
...@@ -56,6 +52,17 @@ func (a *asnStore) AsnForIPv6(ip net.IP) (string, error) { ...@@ -56,6 +52,17 @@ func (a *asnStore) AsnForIPv6(ip net.IP) (string, error) {
// NewAsnStore returns a `asnStore` that can be queried for the Autonomous System Numbers // 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. // for a given IP address or a multiaddress which contains an IP address.
func NewAsnStore() (*asnStore, error) { func NewAsnStore() (*asnStore, error) {
if Store.store == nil {
store, err := newAsnStore()
if err != nil {
return nil, err
}
Store.store = store
}
return Store.store, nil
}
func newAsnStore() (*asnStore, error) {
cr := cidranger.NewPCTrieRanger() cr := cidranger.NewPCTrieRanger()
for k, v := range ipv6CidrToAsnMap { for k, v := range ipv6CidrToAsnMap {
...@@ -71,3 +78,22 @@ func NewAsnStore() (*asnStore, error) { ...@@ -71,3 +78,22 @@ func NewAsnStore() (*asnStore, error) {
return &asnStore{cr}, nil return &asnStore{cr}, nil
} }
type indirectAsnStore struct {
store *asnStore
}
// 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.
func (a *indirectAsnStore) AsnForIPv6(ip net.IP) (string, error) {
if a.store == nil {
store, err := newAsnStore()
if err != nil {
panic(err)
}
a.store = store
}
return a.store.AsnForIPv6(ip)
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment