From 87639c220035f6d00826a96f2cd91b13b6dfae51 Mon Sep 17 00:00:00 2001 From: Vadimka Komissarov Date: Tue, 6 Aug 2024 14:21:50 +0000 Subject: [PATCH 1/5] fix integer divide by zero --- internal/cache/api.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/internal/cache/api.go b/internal/cache/api.go index 0ed778f..986cd72 100644 --- a/internal/cache/api.go +++ b/internal/cache/api.go @@ -93,6 +93,13 @@ func (m *Cache) ApiStats() io.Reader { return math.Round(val*ratio) / ratio } + rate := func(misses, hits int) int { + if misses == 0 || hits == 0 { + return 0 + } + return misses * 100 / hits + } + buf := bytes.NewBuffer(nil) tb.SetOutputMirror(buf) @@ -110,7 +117,7 @@ func (m *Cache) ApiStats() io.Reader { cache.Stats().DelHits, cache.Stats().DelMisses, cache.Stats().Collisions, - round(float64(cache.Stats().Misses*100/cache.Stats().Hits), 2), + round(float64(rate(int(cache.Stats().Misses), int(cache.Stats().Hits))), 2), }) } From a5e41c41dcca9b704bd45a9873a474ff9b69a09f Mon Sep 17 00:00:00 2001 From: Vadimka Komissarov Date: Tue, 6 Aug 2024 14:22:38 +0000 Subject: [PATCH 2/5] addition to above commit --- internal/cache/api.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/internal/cache/api.go b/internal/cache/api.go index 986cd72..07d77c8 100644 --- a/internal/cache/api.go +++ b/internal/cache/api.go @@ -89,6 +89,10 @@ func (m *Cache) ApiStats() io.Reader { } round := func(val float64, precision uint) float64 { + if val == 0 { + return 0 + } + ratio := math.Pow(10, float64(precision)) return math.Round(val*ratio) / ratio } @@ -97,6 +101,7 @@ func (m *Cache) ApiStats() io.Reader { if misses == 0 || hits == 0 { return 0 } + return misses * 100 / hits } From 2c3fab9ac03783507e613e7ea2f4b328860e4266 Mon Sep 17 00:00:00 2001 From: Vadimka Komissarov Date: Tue, 6 Aug 2024 14:29:01 +0000 Subject: [PATCH 3/5] add more visible logs --- internal/geoip/http.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/internal/geoip/http.go b/internal/geoip/http.go index 20e1452..4883930 100644 --- a/internal/geoip/http.go +++ b/internal/geoip/http.go @@ -82,6 +82,7 @@ func (m *GeoIPHTTPClient) Bootstrap() { return } defer m.destroyDB(m.fd, m.Reader) + m.log.Info().Msg("geoip initial downloading has been completed") if !m.skipVerify { if e = m.Reader.Verify(); e != nil { @@ -116,7 +117,7 @@ func (m *GeoIPHTTPClient) loop() { var update *time.Timer if m.mmUpdateFreq != 0 { update = time.NewTimer(m.mmUpdateFreq) - m.log.Debug().Msgf("geoip database updater enabled; update period - %s", m.mmUpdateFreq.String()) + m.log.Info().Msgf("geoip database updater enabled; update period - %s", m.mmUpdateFreq.String()) } LOOP: @@ -136,7 +137,7 @@ LOOP: } m.log.Info().Msg("starting geoip database update") m.log.Debug().Msg("geoip database update, downloading...") - defer m.log.Debug().Msg("geoip database update, finished") + defer m.log.Info().Msg("geoip database update, finished") newfd, newrd, e := m.databaseDownload() if e != nil && newfd != nil && newrd != nil { // update is not required @@ -323,7 +324,7 @@ func (m *GeoIPHTTPClient) databaseDownload() (fd *os.File, _ *maxminddb.Reader, if fd, e = m.makeTempFile(); e != nil { return } - m.log.Debug().Msgf("file %s has been successfully allocated", fd.Name()) + m.log.Info().Msgf("file %s has been successfully allocated", fd.Name()) req := m.acquireGeoIPRequest(nil) defer fasthttp.ReleaseRequest(req) @@ -359,7 +360,7 @@ func (m *GeoIPHTTPClient) databaseDownload() (fd *os.File, _ *maxminddb.Reader, return } - m.log.Debug().Msg("maxmind database sha256 verification passed") + m.log.Info().Msg("maxmind database sha256 verification passed") m.mmLastHash = expectedHash } From fa838bf9377103c98468868cf211a0e9c605b53b Mon Sep 17 00:00:00 2001 From: Vadimka Komissarov Date: Fri, 9 Aug 2024 07:53:17 +0000 Subject: [PATCH 4/5] fix panic while geoip is disabled --- internal/proxy/proxy.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/internal/proxy/proxy.go b/internal/proxy/proxy.go index 13709da..25232e1 100644 --- a/internal/proxy/proxy.go +++ b/internal/proxy/proxy.go @@ -31,6 +31,11 @@ type ProxyConfig struct { func NewProxy(c context.Context) *Proxy { cli := c.Value(utils.CKCliCtx).(*cli.Context) + var gip geoip.GeoIPClient + if c.Value(utils.CKGeoIP) != nil { + gip = c.Value(utils.CKGeoIP).(geoip.GeoIPClient) + } + return &Proxy{ client: NewClient(cli), config: &ProxyConfig{ @@ -40,7 +45,7 @@ func NewProxy(c context.Context) *Proxy { }, cache: c.Value(utils.CKCache).(*cache.Cache), - geoip: c.Value(utils.CKGeoIP).(geoip.GeoIPClient), + geoip: gip, } } From 29bb377de448a5297dccac93570bf5d0aaa55268 Mon Sep 17 00:00:00 2001 From: Vadimka Komissarov Date: Sun, 11 Aug 2024 09:59:20 +0000 Subject: [PATCH 5/5] geoip - small refactoring in loop --- internal/geoip/http.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/internal/geoip/http.go b/internal/geoip/http.go index 4883930..33d6114 100644 --- a/internal/geoip/http.go +++ b/internal/geoip/http.go @@ -124,6 +124,8 @@ LOOP: for { select { case <-update.C: + update.Stop() + if !m.muUpdate.TryLock() { m.log.Error().Msg("could not start the mmdb update, last proccess is not marked as complete") update.Reset(m.mmRetryFreq) @@ -142,6 +144,7 @@ LOOP: newfd, newrd, e := m.databaseDownload() if e != nil && newfd != nil && newrd != nil { // update is not required m.log.Info().Msg(e.Error()) + update.Reset(m.mmUpdateFreq) m.muUpdate.Unlock() continue } else if e != nil { @@ -162,6 +165,7 @@ LOOP: m.Verify() } + update.Reset(m.mmUpdateFreq) m.muUpdate.Unlock() case <-m.done(): m.log.Info().Msg("internal abort() has been caught; initiate application closing...")