Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🔥 feat: Add support for stopping timestamp updater #101

Merged
merged 4 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions time.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
var (
timestampTimer sync.Once
timestamp uint32
stopChan chan struct{}
)

// Timestamp returns the current time.
Expand All @@ -21,16 +22,32 @@
// which is much better for performance than determining it at runtime each time
func StartTimeStampUpdater() {
timestampTimer.Do(func() {
// set initial value
atomic.StoreUint32(&timestamp, uint32(time.Now().Unix()))
go func(sleep time.Duration) {

c := make(chan struct{})
stopChan = c

gaby marked this conversation as resolved.
Show resolved Hide resolved
go func(localChan chan struct{}, sleep time.Duration) {
ticker := time.NewTicker(sleep)
defer ticker.Stop()

for t := range ticker.C {
// update timestamp
atomic.StoreUint32(&timestamp, uint32(t.Unix()))
for {
select {
case t := <-ticker.C:
atomic.StoreUint32(&timestamp, uint32(t.Unix()))

Check warning on line 37 in time.go

View check run for this annotation

Codecov / codecov/patch

time.go#L36-L37

Added lines #L36 - L37 were not covered by tests
case <-localChan:
return
}
}
}(1 * time.Second) // duration
}(c, 1*time.Second)
})
}

// StopTimeStampUpdater stops the timestamp updater
// WARNING: Make sure to call this function before the program exits, otherwise it will leak goroutines
func StopTimeStampUpdater() {
if stopChan != nil {
close(stopChan)
stopChan = nil
}
}
gaby marked this conversation as resolved.
Show resolved Hide resolved
18 changes: 16 additions & 2 deletions time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ func checkTimeStamp(tb testing.TB, expectedCurrent, actualCurrent uint32) {
}

func Test_TimeStampUpdater(t *testing.T) {
t.Parallel()

StartTimeStampUpdater()

now := uint32(time.Now().Unix())
Expand All @@ -30,6 +28,22 @@ func Test_TimeStampUpdater(t *testing.T) {
checkTimeStamp(t, now+2, Timestamp())
}

func Test_StopTimeStampUpdater(t *testing.T) {
// Start the timestamp updater
StartTimeStampUpdater()

// Stop the updater
StopTimeStampUpdater()

// Capture the timestamp after stopping
stoppedTime := Timestamp()

// Wait before checking the timestamp
time.Sleep(5 * time.Second)
// It should not have changed since we've stopped the updater
require.Equal(t, stoppedTime, Timestamp(), "timestamp should not change after stopping updater")
}

func Benchmark_CalculateTimestamp(b *testing.B) {
var res uint32
StartTimeStampUpdater()
Expand Down
Loading