diff --git a/broker/broker.go b/broker/broker.go index 3795f2b4a8a6781db94b65b1ae74195728330dd3..26fc450c914a80c0bba1c08b48b1770156af1117 100644 --- a/broker/broker.go +++ b/broker/broker.go @@ -149,6 +149,15 @@ func proxyPolls(ctx *BrokerContext, w http.ResponseWriter, r *http.Request) { return } log.Println("Received snowflake: ", id) + + // Log geoip stats + remoteIP, _, err := net.SplitHostPort(r.RemoteAddr) + if err != nil { + log.Println("Error processing proxy IP: ", err.Error()) + } else { + ctx.metrics.UpdateCountryStats(remoteIP) + } + // Wait for a client to avail an offer to the snowflake, or timeout if nil. offer := ctx.RequestOffer(id) if nil == offer { @@ -224,15 +233,6 @@ func proxyAnswers(ctx *BrokerContext, w http.ResponseWriter, r *http.Request) { return } - // Get proxy country stats - remoteIP, _, err := net.SplitHostPort(r.RemoteAddr) - if err != nil { - log.Println("Error processing proxy IP: ", err.Error()) - } else { - - ctx.metrics.UpdateCountryStats(remoteIP) - } - log.Println("Received answer.") snowflake.answerChannel <- body } diff --git a/broker/metrics.go b/broker/metrics.go index a7f077a4faffd9f41dbd8b34c1242ed13bdb8dd6..b03c6db11423a24e6e580fed76afedd7d53492c8 100644 --- a/broker/metrics.go +++ b/broker/metrics.go @@ -17,6 +17,7 @@ var ( const metricsResolution = 86400 * time.Second type CountryStats struct { + ips map[string]bool counts map[string]int } @@ -65,8 +66,11 @@ func (m *Metrics) UpdateCountryStats(addr string) { log.Println("Unknown geoip") } - //update map of countries and counts - m.countryStats.counts[country]++ + //update map of unique ips and counts + if !m.countryStats.ips[addr] { + m.countryStats.counts[country]++ + m.countryStats.ips[addr] = true + } return } @@ -101,6 +105,7 @@ func NewMetrics(metricsLogger *log.Logger) (*Metrics, error) { m.countryStats = CountryStats{ counts: make(map[string]int), + ips: make(map[string]bool), } m.logger = metricsLogger @@ -126,6 +131,7 @@ func (m *Metrics) logMetrics() { m.clientDeniedCount = 0 m.clientProxyMatchCount = 0 m.countryStats.counts = make(map[string]int) + m.countryStats.ips = make(map[string]bool) } }