Documentation ¶
Overview ¶
Package timer provides various enhanced timer functions.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type RandTicker ¶
RandTicker is just like time.Ticker, except that it adds randomness to the events.
func NewRandTicker ¶
func NewRandTicker(d, variance time.Duration) *RandTicker
NewRandTicker creates a new RandTicker. d is the duration, and variance specifies the variance. The ticker will tick every d +/- variance.
func (*RandTicker) Stop ¶
func (tkr *RandTicker) Stop()
Stop stops the ticker and closes the underlying channel.
type RateLimiter ¶ added in v0.14.1
type RateLimiter struct {
// contains filtered or unexported fields
}
RateLimiter runs given tasks, at no more than one per defined duration. For example, we can create a RateLimiter of 1second. Then, we can ask it, over time, to run many tasks. It will only ever run a single task in any 1 second time frame. The rest are ignored.
func NewRateLimiter ¶ added in v0.14.1
func NewRateLimiter(d time.Duration) *RateLimiter
NewRateLimiter creates a new limiter with given duration. It is immediately ready to run tasks.
func (*RateLimiter) AllowOne ¶ added in v0.21.0
func (r *RateLimiter) AllowOne()
AllowOne allows the next Do() call to run, even if the rate limiter would otherwise skip it.
func (*RateLimiter) Diff ¶ added in v0.20.0
func (r *RateLimiter) Diff() int64
Diff returns the logical clock diff between the ticker and the last Do() call.
func (*RateLimiter) Do ¶ added in v0.14.1
func (r *RateLimiter) Do(f func() error) (err error)
Do runs a given func assuming rate limiting allows. This function is thread safe. f may be nil, in which case it is not invoked.
func (*RateLimiter) DoEmpty ¶ added in v0.20.0
func (r *RateLimiter) DoEmpty()
DoEmpty is a convenience method to invoke Do() with no function.
func (*RateLimiter) Stop ¶ added in v0.14.1
func (r *RateLimiter) Stop()
Stop terminates rate limiter's operation and will not allow any more Do() executions.
type SuspendableTicker ¶ added in v0.8.0
type SuspendableTicker struct { // C is user facing C chan time.Time // contains filtered or unexported fields }
SuspendableTicker is similar to time.Ticker, but also offers Suspend() and Resume() functions. While the ticker is suspended, nothing comes from the time channel C
func NewSuspendableTicker ¶ added in v0.8.0
func NewSuspendableTicker(d time.Duration, initiallySuspended bool) *SuspendableTicker
NewSuspendableTicker creates a new suspendable ticker, indicating whether the ticker should start suspendable or running
func (*SuspendableTicker) Resume ¶ added in v0.8.0
func (s *SuspendableTicker) Resume()
Resume re-enables time events on channel C
func (*SuspendableTicker) Stop ¶ added in v0.8.0
func (s *SuspendableTicker) Stop()
Stop completely stops the timer, like time.Timer
func (*SuspendableTicker) Suspend ¶ added in v0.8.0
func (s *SuspendableTicker) Suspend()
Suspend stops sending time events on the channel C time events sent during suspended time are lost
func (*SuspendableTicker) TickAfter ¶ added in v0.19.0
func (s *SuspendableTicker) TickAfter(d time.Duration)
TickAfter generates a tick after given duration has passed. It runs asynchronously and returns immediately.
func (*SuspendableTicker) TickNow ¶ added in v0.10.0
func (s *SuspendableTicker) TickNow()
TickNow generates a tick at this point in time. It may block if nothing consumes the tick.
type Timer ¶
type Timer struct {
// contains filtered or unexported fields
}
Timer provides timer functionality that can be controlled by the user. You start the timer by providing it a callback function, which it will call at the specified interval.
var t = timer.NewTimer(1e9) t.Start(KeepHouse) func KeepHouse() { // do house keeping work }
You can stop the timer by calling t.Stop, which is guaranteed to wait if KeepHouse is being executed.
You can create an untimely trigger by calling t.Trigger. You can also schedule an untimely trigger by calling t.TriggerAfter.
The timer interval can be changed on the fly by calling t.SetInterval. A zero value interval will cause the timer to wait indefinitely, and it will react only to an explicit Trigger or Stop.
func (*Timer) SetInterval ¶
SetInterval changes the wait interval. It will cause the timer to restart the wait.
func (*Timer) Stop ¶
func (tm *Timer) Stop()
Stop will stop the timer. It guarantees that the timer will not execute any more calls to keephouse once it has returned.
func (*Timer) Trigger ¶
func (tm *Timer) Trigger()
Trigger will cause the timer to immediately execute the keephouse function. It will then cause the timer to restart the wait.
func (*Timer) TriggerAfter ¶
TriggerAfter waits for the specified duration and triggers the next event.