fix(geo): retry the lookup when a hot swap closes the database
lookupRaw loaded the database pointer and then acquired it. A hot swap could close the old database in between, so the lookup returned an empty answer (and cached it). The swap stores the new database before closing the old one, so reload the pointer and retry when acquire fails. Seen once in 377k lookups on the arm64 runner. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b4a2c2c9ec
commit
db1a586f7e
1 file changed
+27
-10
+27
-10
@@ -179,21 +179,38 @@ func (r *Resolver) Lookup(ip netip.Addr) Info {
|
||||
|
||||
func (r *Resolver) lookupRaw(ip netip.Addr) Info {
|
||||
var in Info
|
||||
if d := r.city.Load(); d != nil && d.r.acquire() {
|
||||
if off, ok := d.r.find(ip); ok {
|
||||
fillCity(d.r.dec, off, &in)
|
||||
withDB(r.city.Load, func(rd *reader) {
|
||||
if off, ok := rd.find(ip); ok {
|
||||
fillCity(rd.dec, off, &in)
|
||||
}
|
||||
d.r.release()
|
||||
}
|
||||
if d := r.asn.Load(); d != nil && d.r.acquire() {
|
||||
if off, ok := d.r.find(ip); ok {
|
||||
fillASN(d.r.dec, off, &in)
|
||||
})
|
||||
withDB(r.asn.Load, func(rd *reader) {
|
||||
if off, ok := rd.find(ip); ok {
|
||||
fillASN(rd.dec, off, &in)
|
||||
}
|
||||
d.r.release()
|
||||
}
|
||||
})
|
||||
return in
|
||||
}
|
||||
|
||||
// withDB runs fn on the current database of a slot. A hot swap stores the new
|
||||
// database before closing the old one, so when acquire fails (the pointer we
|
||||
// loaded was closed in between) the slot already holds its replacement: load
|
||||
// it again instead of returning an empty answer. Bounded, in case the slot is
|
||||
// being closed for good (Close).
|
||||
func withDB(load func() *db, fn func(*reader)) {
|
||||
for attempt := 0; attempt < 8; attempt++ {
|
||||
d := load()
|
||||
if d == nil {
|
||||
return
|
||||
}
|
||||
if d.r.acquire() {
|
||||
fn(d.r)
|
||||
d.r.release()
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// fillCity extrai campos no layout GeoIP2-City; erros de decodificação
|
||||
// deixam o que já foi lido.
|
||||
func fillCity(d decoder, off uint, in *Info) {
|
||||
|
||||
Reference in new issue
Block a user