mirror of
https://github.com/therootcompany/golib.git
synced 2026-04-24 12:48:00 +00:00
refactor(geoip): cache tarballs as <edition>_LATEST.tar.gz
Adds geoip.TarGzName(edition) as the single source of truth for the cache filename. The _LATEST suffix signals that the file is whatever MaxMind served most recently (versus the dated Content-Disposition name) and keeps httpcache's ETag sidecar tied to a stable path across releases.
This commit is contained in:
parent
5fc032dc56
commit
e594f2503c
@ -155,13 +155,13 @@ func main() {
|
||||
geoSet := dataset.NewSet(
|
||||
&httpcache.Cacher{
|
||||
URL: geoip.DownloadBase + "/GeoLite2-City/download?suffix=tar.gz",
|
||||
Path: filepath.Join(maxmindDir, "GeoLite2-City.tar.gz"),
|
||||
Path: filepath.Join(maxmindDir, geoip.TarGzName(geoip.CityEdition)),
|
||||
MaxAge: 3 * 24 * time.Hour,
|
||||
Header: authHeader,
|
||||
},
|
||||
&httpcache.Cacher{
|
||||
URL: geoip.DownloadBase + "/GeoLite2-ASN/download?suffix=tar.gz",
|
||||
Path: filepath.Join(maxmindDir, "GeoLite2-ASN.tar.gz"),
|
||||
Path: filepath.Join(maxmindDir, geoip.TarGzName(geoip.ASNEdition)),
|
||||
MaxAge: 3 * 24 * time.Hour,
|
||||
Header: authHeader,
|
||||
},
|
||||
|
||||
@ -51,7 +51,7 @@ func main() {
|
||||
|
||||
exitCode := 0
|
||||
for _, edition := range cfg.EditionIDs {
|
||||
path := filepath.Join(outDir, edition+".tar.gz")
|
||||
path := filepath.Join(outDir, geoip.TarGzName(edition))
|
||||
cacher := &httpcache.Cacher{
|
||||
URL: geoip.DownloadBase + "/" + edition + "/download?suffix=tar.gz",
|
||||
Path: path,
|
||||
|
||||
@ -20,15 +20,15 @@ type Databases struct {
|
||||
ASN *geoip2.Reader
|
||||
}
|
||||
|
||||
// Open reads <dir>/GeoLite2-City.tar.gz and <dir>/GeoLite2-ASN.tar.gz,
|
||||
// Open reads <dir>/<edition>_LATEST.tar.gz for City and ASN editions,
|
||||
// extracts the .mmdb entry from each archive in memory, and returns open
|
||||
// readers. No .mmdb files are written to disk.
|
||||
func Open(dir string) (*Databases, error) {
|
||||
city, err := openMMDBTarGz(filepath.Join(dir, "GeoLite2-City.tar.gz"))
|
||||
city, err := openMMDBTarGz(filepath.Join(dir, TarGzName(CityEdition)))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("city: %w", err)
|
||||
}
|
||||
asn, err := openMMDBTarGz(filepath.Join(dir, "GeoLite2-ASN.tar.gz"))
|
||||
asn, err := openMMDBTarGz(filepath.Join(dir, TarGzName(ASNEdition)))
|
||||
if err != nil {
|
||||
_ = city.Close()
|
||||
return nil, fmt.Errorf("asn: %w", err)
|
||||
|
||||
@ -15,6 +15,14 @@ const (
|
||||
DownloadBase = "https://download.maxmind.com/geoip/databases"
|
||||
)
|
||||
|
||||
// TarGzName returns the cache filename for edition's tar.gz archive.
|
||||
// MaxMind's Content-Disposition names include a release date
|
||||
// (e.g. GeoLite2-ASN_20260101.tar.gz); we use _LATEST so httpcache's
|
||||
// ETag sidecar stays tied to a stable path across releases.
|
||||
func TarGzName(edition string) string {
|
||||
return edition + "_LATEST.tar.gz"
|
||||
}
|
||||
|
||||
// DefaultConfPaths returns the standard locations where GeoIP.conf is looked
|
||||
// up: ./GeoIP.conf, then ~/.config/maxmind/GeoIP.conf.
|
||||
func DefaultConfPaths() []string {
|
||||
|
||||
@ -64,7 +64,7 @@ func TestDownload_CityAndASN(t *testing.T) {
|
||||
td := testdataDir(t)
|
||||
|
||||
for _, edition := range []string{geoip.CityEdition, geoip.ASNEdition} {
|
||||
path := filepath.Join(td, edition+".tar.gz")
|
||||
path := filepath.Join(td, geoip.TarGzName(edition))
|
||||
os.Remove(path)
|
||||
os.Remove(path + ".meta")
|
||||
|
||||
@ -96,7 +96,7 @@ func TestDownload_ConditionalGet_FreshCacher(t *testing.T) {
|
||||
td := testdataDir(t)
|
||||
|
||||
for _, edition := range []string{geoip.CityEdition, geoip.ASNEdition} {
|
||||
path := filepath.Join(td, edition+".tar.gz")
|
||||
path := filepath.Join(td, geoip.TarGzName(edition))
|
||||
|
||||
if _, err := newCacher(cfg, edition, path).Fetch(); err != nil {
|
||||
t.Fatalf("%s initial Fetch: %v", edition, err)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user