Skip to content

Commit

Permalink
fix/109 first day patch (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
MindHunter86 authored Aug 11, 2024
2 parents 8ba5c7e + 3bd0e3a commit ffd633f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
14 changes: 13 additions & 1 deletion internal/cache/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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),
})
}

Expand Down
13 changes: 9 additions & 4 deletions internal/geoip/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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...")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit ffd633f

Please sign in to comment.