diff --git a/asn.go b/asn.go index 9ed8412..cfbe127 100644 --- a/asn.go +++ b/asn.go @@ -11,7 +11,7 @@ import ( var Store *indirectAsnStore func init() { - Store = &indirectAsnStore{} + Store = newIndirectAsnStore() } type networkWithAsn struct { @@ -81,19 +81,30 @@ func newAsnStore() (*asnStore, error) { 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. func (a *indirectAsnStore) AsnForIPv6(ip net.IP) (string, error) { - if a.store == nil { + <-a.doneLoading + return a.store.AsnForIPv6(ip) +} + +func newIndirectAsnStore() *indirectAsnStore { + a := &indirectAsnStore{ + doneLoading: make(chan struct{}), + } + + go func() { store, err := newAsnStore() if err != nil { panic(err) } a.store = store - } + close(a.doneLoading) + }() - return a.store.AsnForIPv6(ip) + return a }