Count number of queries with known IPs

chubin/logging
Igor Chubin 2 years ago
parent 074b8b6ec8
commit 3765dcbfbf

@ -12,6 +12,7 @@ import (
// Config of the program. // Config of the program.
type Config struct { type Config struct {
Cache Cache
Geo
Logging Logging
Server Server
Uplink Uplink
@ -67,12 +68,21 @@ type Cache struct {
Size int `yaml:"size,omitempty"` Size int `yaml:"size,omitempty"`
} }
// Geo contains geolocation configuration.
type Geo struct {
// IPCache contains the path to the IP Geodata cache.
IPCache string `yaml:"ipCache,omitempty"`
}
// Default contains the default configuration. // Default contains the default configuration.
func Default() *Config { func Default() *Config {
return &Config{ return &Config{
Cache{ Cache{
Size: 12800, Size: 12800,
}, },
Geo{
IPCache: "/wttr.in/cache/ip2l",
},
Logging{ Logging{
AccessLog: "/wttr.in/log/access.log", AccessLog: "/wttr.in/log/access.log",
ErrorsLog: "/wttr.in/log/errors.log", ErrorsLog: "/wttr.in/log/errors.log",

@ -15,6 +15,7 @@ import (
lru "github.com/hashicorp/golang-lru" lru "github.com/hashicorp/golang-lru"
"github.com/chubin/wttr.in/internal/config" "github.com/chubin/wttr.in/internal/config"
geoip "github.com/chubin/wttr.in/internal/geo/ip"
"github.com/chubin/wttr.in/internal/routing" "github.com/chubin/wttr.in/internal/routing"
"github.com/chubin/wttr.in/internal/stats" "github.com/chubin/wttr.in/internal/stats"
"github.com/chubin/wttr.in/internal/util" "github.com/chubin/wttr.in/internal/util"
@ -54,6 +55,7 @@ type RequestProcessor struct {
router routing.Router router routing.Router
upstreamTransport *http.Transport upstreamTransport *http.Transport
config *config.Config config *config.Config
geoIPCache *geoip.Cache
} }
// NewRequestProcessor returns new RequestProcessor. // NewRequestProcessor returns new RequestProcessor.
@ -80,6 +82,7 @@ func NewRequestProcessor(config *config.Config) (*RequestProcessor, error) {
stats: stats.New(), stats: stats.New(),
upstreamTransport: transport, upstreamTransport: transport,
config: config, config: config,
geoIPCache: geoip.NewCache(config),
} }
// Initialize routes. // Initialize routes.
@ -161,6 +164,13 @@ func (rp *RequestProcessor) ProcessRequest(r *http.Request) (*responseWithHeader
} }
} }
// How many IP addresses are known.
ip := util.ReadUserIP(r)
_, err = rp.geoIPCache.Read(ip)
if err == nil {
rp.stats.Inc("geoip")
}
rp.lruCache.Add(cacheDigest, responseWithHeader{InProgress: true}) rp.lruCache.Add(cacheDigest, responseWithHeader{InProgress: true})
response, err = get(r, rp.upstreamTransport) response, err = get(r, rp.upstreamTransport)

@ -75,6 +75,7 @@ func (c *Stats) Show() []byte {
fmt.Fprintf(&b, "%-20s: %d\n", "Upstream queries", c.v["total"]-c.v["cache1"]) fmt.Fprintf(&b, "%-20s: %d\n", "Upstream queries", c.v["total"]-c.v["cache1"])
fmt.Fprintf(&b, "%-20s: %d\n", "Queries with format", c.v["format"]) fmt.Fprintf(&b, "%-20s: %d\n", "Queries with format", c.v["format"])
fmt.Fprintf(&b, "%-20s: %d\n", "Queries with format=j1", c.v["format=j1"]) fmt.Fprintf(&b, "%-20s: %d\n", "Queries with format=j1", c.v["format=j1"])
fmt.Fprintf(&b, "%-20s: %d\n", "Queries with known IP", c.v["geoip"])
return b.Bytes() return b.Bytes()
} }

Loading…
Cancel
Save