Question

Assume that a process is using the MaxMind GeoIP C API in many separate threads. Are concurrent calls to GeoIP_record_by_addr safe? Assume that this is the only process concurrently accessing the data and a single GeoIP handle is being used.

Thanks in advance!

Was it helpful?

Solution

According to MaxMind's own docs, it's only thread-safe if you avoid the GEOIP_CHECK_CACHE option. This means the library won't check for database updates on-disk via mtime checks. For a long-running application, if you want fresh data you'll have to either:

  • Restart the app periodically
  • Do your own mtime checks and reload via GeoIP_open()-type calls, but this will require setting up your own mutex to protect the reloading/replacement of your shared GeoIP handle, so at that point you're doing the full thread-safety protection on your own. You may as well turn on GEOIP_CHECK_CACHE and use the mutex to protect everything in the first place and avoid having to write your own reload code.

Two other minor features also aren't thread safe regardless of GEOIP_CHECK_CACHE:

  • If you make use of netmask information (and not everyone does), the lookup functions only return that by setting gi->netmask on the GeoIP handle itself, so obviously a shared handle's netmask won't always give correct values for the "most recent" lookup from the same thread.
  • Obviously, any use of the iterator interfaces for GeoIPCity (GeoIP_next_record()) would be unsafe as well, since the iterator state is stored in the shared handle.

I could be missing other issues in my brief analysis, but IMHO it's more pragmatic to either use a GeoIP handle per-thread or wrap all access to the shared handle in your own mutex, and then you can use all features and have it to do the mtime-based reloading for you.

OTHER TIPS

I had the time to investigate the GeoIP API and it appears that it's treatment of the database is thread safe. Pread or memory accesses.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top