From 0d43667ebb9693371e8babbefcdc4f07a151da45 Mon Sep 17 00:00:00 2001 From: Karan Sharma Date: Fri, 30 Aug 2024 14:46:48 +0530 Subject: [PATCH] fix: replace timer with ticker --- stores/goredis/redis.go | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/stores/goredis/redis.go b/stores/goredis/redis.go index aded43c..819bce6 100644 --- a/stores/goredis/redis.go +++ b/stores/goredis/redis.go @@ -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 { @@ -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 { @@ -231,17 +231,9 @@ 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 @@ -249,8 +241,6 @@ func (s *Store) putWorker() { count = 0 p = s.cn.Pipeline() } - // Reset the timer - timer.Reset(s.config.AsyncCommitFreq) case <-s.ctx.Done(): return