oschwald/geoip2-golang

Reload the database in thread safe manner

knoguchi opened this issue · 3 comments

The mmdb is updated frequently. I would like to reload the mmdb when the db has changed. How do I safely reload the db while many go routines share the same reader? I understand the reader is thread safe but I have no idea how I can Close and Open the db safely.

This is pretty specific to your application. It can be achieved with sync/atomic or a mutex. I generally open the new database, replace the previous database with the new one, and then close the database after everything switches to the new one. maxminddb also sets a finalizer so it should automatically close at some point when the reader is garbage collected.

@oschwald do you have any pointers for implementing the approach you're recommending here? I was looking at implementing a singleton-esque pattern with an update running on a schedule, but being new to Go I'm not so certain on details (whether to use sync/atomic or what abstraction levels make sense here).

In my own code, I often store the database reader in an atomic.Value and then have a goroutine that watches the database file using fsnotify, replacing the reader when the file is updated. I pass around a pointer to the atomic value (usually wrapped in a struct with helper methods). I don't use a singleton.