Skip to content

Commit da13074

Browse files
committed
Merge pull request abh#9 from dgryski/mutex-for-region-by-addr
Protect calls to GeoIP calls with our mutex
2 parents ce8dff3 + 13f8ee8 commit da13074

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

geoip.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ import (
2424

2525
type GeoIP struct {
2626
db *C.GeoIP
27+
28+
// We don't use GeoIP's thread-safe API calls, which means there is a
29+
// single global netmask variable that gets clobbered in the main
30+
// lookup routine. Any calls which have _GeoIP_seek_record_gl need to
31+
// be wrapped in this mutex.
32+
2733
mu sync.Mutex
2834
}
2935

@@ -178,7 +184,11 @@ func (gi *GeoIP) GetRecord(ip string) *GeoIPRecord {
178184

179185
cip := C.CString(ip)
180186
defer C.free(unsafe.Pointer(cip))
187+
188+
gi.mu.Lock()
181189
record := C.GeoIP_record_by_addr(gi.db, cip)
190+
gi.mu.Unlock()
191+
182192
if record == nil {
183193
return nil
184194
}
@@ -209,7 +219,11 @@ func (gi *GeoIP) GetRegion(ip string) (string, string) {
209219

210220
cip := C.CString(ip)
211221
defer C.free(unsafe.Pointer(cip))
222+
223+
gi.mu.Lock()
212224
region := C.GeoIP_region_by_addr(gi.db, cip)
225+
gi.mu.Unlock()
226+
213227
if region == nil {
214228
return "", ""
215229
}
@@ -270,7 +284,7 @@ func (gi *GeoIP) GetCountry(ip string) (cc string, netmask int) {
270284
return
271285
}
272286

273-
gi.mu.Lock() // Lock to make sure we get the right result from GeoIP_last_netmask
287+
gi.mu.Lock()
274288
defer gi.mu.Unlock()
275289

276290
cip := C.CString(ip)

0 commit comments

Comments
 (0)