Skip to content

Commit 0133744

Browse files
committed
use worker pool for geolookup
1 parent a5d34b8 commit 0133744

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

tracker/metrics/metrics.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,30 @@
66
package metrics
77

88
import (
9+
"net"
10+
"sync/atomic"
11+
912
"github.com/getlantern/geo"
1013
"go.opentelemetry.io/otel"
1114
"go.opentelemetry.io/otel/metric"
1215
"go.opentelemetry.io/otel/metric/noop"
1316
)
1417

18+
const countryLookupWorkers = 4
19+
20+
type countryLookupRequest struct {
21+
ip net.IP
22+
country *atomic.Value
23+
}
24+
1525
type metricsManager struct {
1626
meter metric.Meter
1727
ProxyIO metric.Int64Counter
1828
conns metric.Int64UpDownCounter
1929
duration metric.Int64Histogram
2030

21-
countryLookup geo.CountryLookup
31+
countryLookup geo.CountryLookup
32+
countryLookupC chan countryLookupRequest
2233
}
2334

2435
var metrics = &metricsManager{
@@ -47,5 +58,18 @@ func SetupMetricsManager(countryLookup geo.CountryLookup) {
4758
if countryLookup != nil {
4859
metrics.countryLookup = countryLookup
4960
}
61+
if _, ok := countryLookup.(geo.NoLookup); !ok {
62+
metrics.countryLookupC = make(chan countryLookupRequest, 256)
63+
for range countryLookupWorkers {
64+
go countryLookupWorker(metrics.countryLookupC, metrics.countryLookup)
65+
}
66+
}
67+
5068
metrics.meter = meter
5169
}
70+
71+
func countryLookupWorker(ch <-chan countryLookupRequest, lookup geo.CountryLookup) {
72+
for req := range ch {
73+
req.country.Store(lookup.CountryCode(req.ip))
74+
}
75+
}

tracker/metrics/tracker.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,14 @@ func metadataToAttributes(metadata adapter.InboundContext) *attributes {
115115
},
116116
}
117117
attrs.country.Store(ccNa)
118-
go func() {
119-
fromCountry := metrics.countryLookup.CountryCode(metadata.Source.IPAddr().IP)
120-
attrs.country.Store(fromCountry)
121-
}()
118+
if metrics.countryLookupC != nil {
119+
select {
120+
case metrics.countryLookupC <- countryLookupRequest{
121+
ip: metadata.Source.IPAddr().IP,
122+
country: &attrs.country,
123+
}:
124+
default:
125+
}
126+
}
122127
return attrs
123128
}

0 commit comments

Comments
 (0)