Changed variable names/types to be more reasonable

Also moved the geoip check to occur after we've make sure the proxy IP
hasn't yet been recorded. This is will cut down on unecessary
computation.
This commit is contained in:
Cecylia Bocovich 2019-06-14 17:00:31 -04:00
parent 92d61f2555
commit 0767a637c1

View file

@ -49,10 +49,10 @@ var (
once sync.Once once sync.Once
) )
const metricsResolution = 86400 * time.Second const metricsResolution = 60 * 60 * 24 * time.Second //86400 seconds
type CountryStats struct { type CountryStats struct {
ips map[string]bool addrs map[string]bool
counts map[string]int counts map[string]int
} }
@ -64,9 +64,9 @@ type Metrics struct {
countryStats CountryStats countryStats CountryStats
clientRoundtripEstimate time.Duration clientRoundtripEstimate time.Duration
proxyIdleCount int proxyIdleCount uint
clientDeniedCount int clientDeniedCount uint
clientProxyMatchCount int clientProxyMatchCount uint
} }
func (s CountryStats) Display() string { func (s CountryStats) Display() string {
@ -82,6 +82,10 @@ func (m *Metrics) UpdateCountryStats(addr string) {
var country string var country string
var ok bool var ok bool
if m.countryStats.addrs[addr] {
return
}
ip := net.ParseIP(addr) ip := net.ParseIP(addr)
if ip.To4() != nil { if ip.To4() != nil {
//This is an IPv4 address //This is an IPv4 address
@ -102,10 +106,8 @@ func (m *Metrics) UpdateCountryStats(addr string) {
} }
//update map of unique ips and counts //update map of unique ips and counts
if !m.countryStats.ips[addr] {
m.countryStats.counts[country]++ m.countryStats.counts[country]++
m.countryStats.ips[addr] = true m.countryStats.addrs[addr] = true
}
return return
} }
@ -140,7 +142,7 @@ func NewMetrics(metricsLogger *log.Logger) (*Metrics, error) {
m.countryStats = CountryStats{ m.countryStats = CountryStats{
counts: make(map[string]int), counts: make(map[string]int),
ips: make(map[string]bool), addrs: make(map[string]bool),
} }
m.logger = metricsLogger m.logger = metricsLogger
@ -174,10 +176,10 @@ func (m *Metrics) zeroMetrics() {
m.clientDeniedCount = 0 m.clientDeniedCount = 0
m.clientProxyMatchCount = 0 m.clientProxyMatchCount = 0
m.countryStats.counts = make(map[string]int) m.countryStats.counts = make(map[string]int)
m.countryStats.ips = make(map[string]bool) m.countryStats.addrs = make(map[string]bool)
} }
// Rounds up a count to the nearest multiple of 8. // Rounds up a count to the nearest multiple of 8.
func binCount(count int) int { func binCount(count uint) uint {
return int((math.Ceil(float64(count) / 8)) * 8) return uint((math.Ceil(float64(count) / 8)) * 8)
} }