Help us shape the future of Go! Take the 2026 Go Developer Survey

geoip

package
v1.3.104 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 31, 2026 License: AGPL-3.0 Imports: 17 Imported by: 0

Documentation

Overview

Package geoip pkg/geoip/geoip.go c0-com-util a lookup function. Used by the visor (for service-discovery geolocation tagging without an HTTP round-trip to the geoip service) and by `cmd/svc/geoip` (the standalone HTTP geoip service).

Package geoip pkg/geoip/geoip_embedded.go c0-com-util

Package geoip pkg/geoip/geoip_mapped.go c0-com-util

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClientIP

func ClientIP(r *http.Request) string

ClientIP is the caller's address: X-Real-Ip or the first X-Forwarded-For entry when a proxy set them, else the connection's remote address.

func Embedded

func Embedded() bool

Embedded reports whether this build carries the database at all, without decompressing it.

func EmbeddedDB

func EmbeddedDB() []byte

EmbeddedDB returns the raw bytes of the embedded GeoLite2-City database, decompressed from the committed gzip on first call and cached thereafter. The standalone geoip service uses this when no external `--db` path is configured; the visor uses this for in-process lookups. Returns nil only if the committed blob fails to decompress (build/repo corruption), which callers surface as "database not initialized".

func Handler

func Handler(db *geoip2.Reader) http.Handler

Handler answers a lookup of ?ip=, or of the caller's own address when it is not given, as JSON.

func OpenEmbedded

func OpenEmbedded() (*geoip2.Reader, error)

OpenEmbedded returns a *geoip2.Reader backed by the embedded database. Reuse the reader — it's safe for concurrent use.

It calls EmbeddedDB() (not the bare `embedded` var) so the lazy gzip decompress is guaranteed to have run: since #3500 the DB is decompressed only inside EmbeddedDB()'s sync.Once, and a caller that reached OpenEmbedded() first would otherwise get OpenBytes(nil) → no database → empty geoip results (the regression that dropped every visor's country code from the node list on v1.3.85).

func Shared

func Shared() (*geoip2.Reader, error)

Shared returns the process-wide reader over the embedded database, opening it on first use. Callers keep this lazy: a visor that never answers a geo query never pays for it. The reader is memory-mapped over a cached inflated copy (see geoip_mapped.go) so the ~60 MB database stays out of the Go heap; each candidate cache directory is tried in turn, and only if none of them takes the file does it fall back to inflating into memory. The reader is safe for concurrent use and is never closed.

Types

type Result

type Result struct {
	IP            string   `json:"ip_address"`
	Latitude      *float64 `json:"latitude"`
	Longitude     *float64 `json:"longitude"`
	PostalCode    string   `json:"postal_code"`
	ContinentCode string   `json:"continent_code"`
	ContinentName string   `json:"continent_name"`
	CountryCode   string   `json:"country_code"`
	CountryName   string   `json:"country_name"`
	RegionCode    string   `json:"region_code"`
	RegionName    string   `json:"region_name"`
	CityName      string   `json:"city_name"`
	Timezone      string   `json:"timezone"`
}

Result is the schema returned by both the in-process Lookup and the HTTP geoip service. The JSON tags match what `cmd/svc/geoip` returns over HTTP so callers can swap between the two without conversion.

func Lookup

func Lookup(db *geoip2.Reader, ipStr string) (*Result, error)

Lookup looks up an IP in the given database reader and returns a Result.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL