diff --git a/internal/cache/api.go b/internal/cache/api.go index 0ed778f..07d77c8 100644 --- a/internal/cache/api.go +++ b/internal/cache/api.go @@ -89,10 +89,22 @@ 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 } + 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 +122,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), }) } diff --git a/internal/geoip/http.go b/internal/geoip/http.go index 20e1452..33d6114 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,13 +117,15 @@ 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: 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) @@ -136,11 +139,12 @@ 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 m.log.Info().Msg(e.Error()) + update.Reset(m.mmUpdateFreq) m.muUpdate.Unlock() continue } else if e != nil { @@ -161,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...") @@ -323,7 +328,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 +364,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 }