项目作者: mroth

项目描述 :
:dragon: tiny geoip microservice
高级语言: Go
项目地址: git://github.com/mroth/tinygeoip.git
创建时间: 2018-11-22T04:27:50Z
项目社区:https://github.com/mroth/tinygeoip

开源协议:

下载


tinygeoip :dragon:

Build Status
Go Report Card
Go Reference

A small and fast HTTP based microservice for extremely minimal geoip location
lookups.

It bundles into a ~5MB docker image that can serve over ~250K reqs/sec
(uncached) from my laptop.

API

The API is intentionally extremely minimal and is designed to return only the
absolutely most frequently needed geographic metadata for IP lookups.

The API has only one endpoint /, and you just put the IP address (IPv4 and
IPv6 both accepted) directly in the URI path.

Example:

  1. // $ curl http://${SERVER_IP}/89.160.20.112
  2. {"country":{"iso_code":"SE"},"location":{"latitude":59.4333,"longitude":18.05,"accuracy_radius":200}}

Response reformatted for ease of human reading:

  1. {
  2. "country": {
  3. "iso_code": "SE" // ISO 3166-1 country code
  4. },
  5. "location": {
  6. "latitude": 59.4333, // Approximate latitude of IP
  7. "longitude": 18.05, // Approximate longitude of IP
  8. "accuracy_radius": 200 // Accuracy radius, in km, for the location
  9. }
  10. }

Performance

This package generally favors understandability of code over performance
optimizations. That said, it is written in a way to be fairly highly performant,
and combined with it’s minimal nature, it can trivially handle a sustained
150,000 requests/second on my workstation. This actually makes it faster than any
other similar off-the-shelf packages I tested in a quick informal survey.
(Note: my benchmarking was intentionally not robust, and I’m certainly not
trying to start any microbenchmark wars here.)

Installing and running the server

Compile with standard Go toolchain or download a binary for your platform from
the Releases page.

  1. Usage of tinygeoip:
  2. -addr string
  3. Address to listen for connections on (default ":9000")
  4. -db string
  5. Path for MaxMind database file (default "data/GeoLite2-City.mmdb")
  6. -origin string
  7. 'Access-Control-Allow-Origin' header, empty disables (default "*")
  8. -threads int
  9. Number of threads to use, otherwise number of CPUs (default 8)
  10. -verbose
  11. Log all requests

You will need to provide a city-level precision GeoIP2 database file. Free
GeoLite2 versions are available for download from MaxMindDB.

Go library

A Go library is provided for utilizing within native projects. A standard
http.Handler interface is utilized for compatibility within standard Go HTTP
middleware setups. Alternately, if you are doing the lookups within your
existing application and not over the HTTP microservice, you can get an average
lookup result in approximately 1.2 microseconds.

For more information, see the GoDocs.

Docker Image

A docker image is automatically built from all tagged releases.

To utilize it, be sure to mount your MaxMindDB database as a volume so that the
running container can access it.

[TODO: provide an example for folks not so familiar with Docker.]

Stability

:construction: The current API is considered unstable. This is just being
released and I’d like some feedback to make any potential changes before tagging
a v1.0 which will maintain API stability.

In other words, comments and feedback wanted!

  • klauspost/geoip-service is where some of the initial inspiration for
    this was drawn. The primary difference is here we have a significantly more
    minimal API (with integration tests), which removes the need for caching or
    external JSON serialization libraries (things we initially had here but
    removed as the perf tradeoff was not benchmarking as significant versus the
    added complexity). I also wanted the API payload to be much smaller for client
    efficiency.

  • bluesmoon/node-geoip Seems well received, but uses “somewhere between 512MB and 2GB” of memory, which made it highly unsuitable for my purposes.

License

Software license TBD upon v1.0 release.

The chosen license should also contain an additional clause similar to:

“This software is not licensed for usage in any application related to censorship or preventing access to information based on geographic region. Legal action will be pursued against any entity who uses this software to knowingly violate this provision.”