Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
p2p
go-p2p-asn-util
Commits
d8ddb2eb
Commit
d8ddb2eb
authored
Aug 25, 2020
by
Adin Schmahmann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: use background goroutine for indirectAsnStore initialization
parent
38b74d19
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
15 additions
and
17 deletions
+15
-17
asn.go
asn.go
+15
-17
No files found.
asn.go
View file @
d8ddb2eb
...
...
@@ -11,7 +11,7 @@ import (
var
Store
*
indirectAsnStore
func
init
()
{
Store
=
&
i
ndirectAsnStore
{}
Store
=
newI
ndirectAsnStore
()
}
type
networkWithAsn
struct
{
...
...
@@ -49,19 +49,6 @@ 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
)
{
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
()
...
...
@@ -81,19 +68,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
()
{
defer
close
(
a
.
doneLoading
)
store
,
err
:=
newAsnStore
()
if
err
!=
nil
{
panic
(
err
)
}
a
.
store
=
store
}
}
()
return
a
.
store
.
AsnForIPv6
(
ip
)
return
a
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment