Skip to content

Commit

Permalink
fix: replace timer with ticker
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-karan authored and rhnvrm committed Aug 30, 2024
1 parent d25e794 commit 0d43667
Showing 1 changed file with 6 additions and 16 deletions.
22 changes: 6 additions & 16 deletions stores/goredis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,11 @@ func (s *Store) putSync(namespace, group, uri string, b fastcache.Item, ttl time

func (s *Store) putWorker() {
var (
p = s.cn.Pipeline()
count = 0
timer = time.NewTimer(s.config.AsyncCommitFreq)
p = s.cn.Pipeline()
count = 0
ticker = time.NewTicker(s.config.AsyncCommitFreq)
)
defer timer.Stop()
defer ticker.Stop()

for {
select {
Expand All @@ -216,7 +216,7 @@ func (s *Store) putWorker() {
}

// Set a TTL for the group. If one uri in cache group sets a TTL
// then entire group will be evicted. This is a short coming of using
// then entire group will be evicted. This is a shortcoming of using
// hashmap as a group. Needs some work here.
if req.ttl.Seconds() > 0 {
if err := p.PExpire(s.ctx, key, req.ttl).Err(); err != nil {
Expand All @@ -231,26 +231,16 @@ func (s *Store) putWorker() {
}
count = 0
p = s.cn.Pipeline()
// Reset the timer
if !timer.Stop() {
select {
case <-timer.C:
default:
}
}
timer.Reset(s.config.AsyncCommitFreq)
}

case <-timer.C:
case <-ticker.C:
if count > 0 {
if _, err := p.Exec(s.ctx); err != nil {
// Log error
}
count = 0
p = s.cn.Pipeline()
}
// Reset the timer
timer.Reset(s.config.AsyncCommitFreq)

case <-s.ctx.Done():
return
Expand Down

0 comments on commit 0d43667

Please sign in to comment.