github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/timer/tick.go (about) 1 /* For license and copyright information please see the LEGAL file in the code repository */ 2 3 package timer 4 5 import ( 6 "github.com/GeniusesGroup/libgo/protocol" 7 ) 8 9 // NewSyncTick is a convenience wrapper for SyncTimer.Tick() providing access to the ticking. 10 // Unlike After() that providing access to (<-chan struct{}), 11 // due to client need a way to shut it down the underlying 12 // Ticker to recovered by the garbage collector; to prevent **"leaks"**. 13 func NewSyncTick(first, interval protocol.Duration) (t *Sync, err protocol.Error) { 14 var timer Sync 15 timer.Init() 16 err = timer.Tick(first, interval) 17 t = &timer 18 return 19 } 20 21 // NewAsyncTick or Schedule waits for the duration to elapse and then calls callback in each duration elapsed 22 // If callback need blocking operation it must do its logic in new thread(goroutine). 23 // It returns a Timer that can be used to cancel the call using its Stop method. 24 // Schedule an execution at a given time, then once per interval. A typical use case is to execute code once per day at 12am. 25 func NewAsyncTick(first, interval protocol.Duration, callback protocol.TimerListener) (t *Async, err protocol.Error) { 26 var timer Async 27 timer.Init(callback) 28 err = timer.Tick(first, interval) 29 t = &timer 30 return 31 }