File tree Expand file tree Collapse file tree 2 files changed +34
-5
lines changed
Expand file tree Collapse file tree 2 files changed +34
-5
lines changed Original file line number Diff line number Diff line change 66package metrics
77
88import (
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+
1525type 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
2435var 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+ }
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments