Skip to content

Commit

Permalink
Use binary.Read to check if error when reading expire ts
Browse files Browse the repository at this point in the history
  • Loading branch information
Wikidepia committed Mar 8, 2024
1 parent fc8a6b4 commit a693a6c
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func main() {

// Initialize zerolog
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
zerolog.SetGlobalLevel(zerolog.WarnLevel)
zerolog.SetGlobalLevel(zerolog.DebugLevel)

// Parse grid-cache-size
gridCacheSizeParsed, err := byteSizeStrToInt(*gridCacheSize)
Expand Down Expand Up @@ -153,8 +153,16 @@ func evictCache() {
batch := data.DB.NewBatch()
curTime := uint64(time.Now().UnixNano())
for iter.First(); iter.Valid(); iter.Next() {
timestamp := bytes.Trim(iter.Key(), "exp-")
if binary.LittleEndian.Uint64(timestamp) < curTime {
expireTimestamp := bytes.Trim(iter.Key(), "exp-")

var timestamp uint64
err := binary.Read(bytes.NewBuffer(expireTimestamp[:]), binary.LittleEndian, &timestamp)
if err != nil {
log.Error().Err(err).Msg("Failed to read expire timestamp")
continue
}

if timestamp < curTime {
batch.Delete(iter.Key(), pebble.NoSync)
batch.Delete(iter.Value(), pebble.NoSync)
}
Expand Down

0 comments on commit a693a6c

Please sign in to comment.