timer

package
v0.19.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 4, 2024 License: Apache-2.0 Imports: 6 Imported by: 9

Documentation

Overview

Package timer provides various enhanced timer functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SleepContext added in v0.9.0

func SleepContext(ctx context.Context, duration time.Duration) error

SleepContext sleeps for the specified time period. If the context expires early, it returns an error.

Types

type RandTicker

type RandTicker struct {
	C <-chan time.Time
	// contains filtered or unexported fields
}

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) 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) 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 NewTimer

func NewTimer(interval time.Duration) *Timer

NewTimer creates a new Timer object

func (*Timer) Interval

func (tm *Timer) Interval() time.Duration

Interval returns the current interval.

func (*Timer) Running

func (tm *Timer) Running() bool

func (*Timer) SetInterval

func (tm *Timer) SetInterval(ns time.Duration)

SetInterval changes the wait interval. It will cause the timer to restart the wait.

func (*Timer) Start

func (tm *Timer) Start(keephouse func())

Start starts the timer.

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

func (tm *Timer) TriggerAfter(duration time.Duration)

TriggerAfter waits for the specified duration and triggers the next event.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL