summaryrefslogtreecommitdiffstats
path: root/internal/pausedtimer
diff options
context:
space:
mode:
authorMarc Pervaz Boocha <marcpervaz@qburst.com>2025-02-21 18:24:35 +0530
committerMarc Pervaz Boocha <marcpervaz@qburst.com>2025-02-21 18:24:35 +0530
commita470aa89cbdabba813636b8a29be8ae3836c03a0 (patch)
tree2551bebaff980cc93bf8fcf60dd8ff50bd7efc48 /internal/pausedtimer
parentAdd Tests and benchmarks (diff)
downloadcache-a470aa89cbdabba813636b8a29be8ae3836c03a0.tar
cache-a470aa89cbdabba813636b8a29be8ae3836c03a0.tar.gz
cache-a470aa89cbdabba813636b8a29be8ae3836c03a0.tar.bz2
cache-a470aa89cbdabba813636b8a29be8ae3836c03a0.tar.lz
cache-a470aa89cbdabba813636b8a29be8ae3836c03a0.tar.xz
cache-a470aa89cbdabba813636b8a29be8ae3836c03a0.tar.zst
cache-a470aa89cbdabba813636b8a29be8ae3836c03a0.zip
Added LTR Eviction
Diffstat (limited to 'internal/pausedtimer')
-rw-r--r--internal/pausedtimer/timer.go54
-rw-r--r--internal/pausedtimer/timer_test.go36
2 files changed, 90 insertions, 0 deletions
diff --git a/internal/pausedtimer/timer.go b/internal/pausedtimer/timer.go
new file mode 100644
index 0000000..691c257
--- /dev/null
+++ b/internal/pausedtimer/timer.go
@@ -0,0 +1,54 @@
+package pausedtimer
+
+import (
+ "math"
+ "time"
+)
+
+// PauseTimer is a struct that wraps a time.Ticker and provides additional functionality
+// to pause and resume the ticker.
+// If the duration is 0, the timer is created in a stopped state.
+type PauseTimer struct {
+ *time.Ticker
+ duration time.Duration
+}
+
+// New creates a new pauseTimer with the specified duration.
+func New(d time.Duration) *PauseTimer {
+ ret := &PauseTimer{duration: d}
+ if d != 0 {
+ ret.Ticker = time.NewTicker(d)
+ } else {
+ ret.Ticker = time.NewTicker(math.MaxInt64)
+ ret.Reset(0)
+ }
+ return ret
+}
+
+// NewStopped creates a new pauseTimer with the specified duration and stops it immediately.
+func NewStopped(d time.Duration) *PauseTimer {
+ ret := New(d)
+ ret.Stop()
+ return ret
+}
+
+// Reset sets the timer to the specified duration and starts it.
+// If the duration is 0, the timer is stopped.
+func (t *PauseTimer) Reset(d time.Duration) {
+ t.duration = d
+ if t.duration == 0 {
+ t.Stop()
+ } else {
+ t.Ticker.Reset(d)
+ }
+}
+
+// Resume resumes the timer with its last set duration.
+func (t *PauseTimer) Resume() {
+ t.Reset(t.GetDuration())
+}
+
+// GetDuration returns the current duration of the timer.
+func (t *PauseTimer) GetDuration() time.Duration {
+ return t.duration
+}
diff --git a/internal/pausedtimer/timer_test.go b/internal/pausedtimer/timer_test.go
new file mode 100644
index 0000000..6e15001
--- /dev/null
+++ b/internal/pausedtimer/timer_test.go
@@ -0,0 +1,36 @@
+package pausedtimer
+
+import (
+ "testing"
+ "time"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestNew(t *testing.T) {
+ d := 1 * time.Second
+ timer := New(d)
+ assert.Equal(t, d, timer.duration)
+ assert.NotNil(t, timer.Ticker)
+}
+
+func TestPauseTimerReset(t *testing.T) {
+ d := 1 * time.Second
+ timer := New(d)
+ newD := 2 * time.Second
+ timer.Reset(newD)
+ assert.Equal(t, newD, timer.duration)
+}
+
+func TestPauseTimerResume(t *testing.T) {
+ d := 1 * time.Second
+ timer := NewStopped(d)
+ timer.Resume()
+ assert.Equal(t, d, timer.duration)
+}
+
+func TestPauseTimerGetDuration(t *testing.T) {
+ d := 1 * time.Second
+ timer := New(d)
+ assert.Equal(t, d, timer.GetDuration())
+}