breaking: move maxmind config to config.providers

- moved maxmind to separate module
- code refactored
- simplified test
This commit is contained in:
yusing
2025-05-03 20:58:09 +08:00
parent ac1470d81d
commit ccb4639f43
14 changed files with 314 additions and 369 deletions

View File

@@ -0,0 +1,36 @@
package maxmind
import (
"github.com/puzpuzpuz/xsync/v3"
)
var cityCache = xsync.NewMapOf[string, *City]()
func (cfg *MaxMind) lookupCity(ip *IPInfo) (*City, bool) {
if ip.City != nil {
return ip.City, true
}
if cfg.db.Reader == nil {
return nil, false
}
city, ok := cityCache.Load(ip.Str)
if ok {
ip.City = city
return city, true
}
cfg.db.RLock()
defer cfg.db.RUnlock()
city = new(City)
err := cfg.db.Lookup(ip.IP, city)
if err != nil {
return nil, false
}
cityCache.Store(ip.Str, city)
ip.City = city
return city, true
}