ratelimit: go.uber.org/ratelimit/internal/clock Index | Files

package clock

import "go.uber.org/ratelimit/internal/clock"

Index

Package Files

clock.go interface.go real.go timers.go

type Clock Uses

type Clock interface {
    AfterFunc(d time.Duration, f func())
    Now() time.Time
    Sleep(d time.Duration)
}

Clock represents an interface to the functions in the standard library time package. Two implementations are available in the clock package. The first is a real-time clock which simply wraps the time package's functions. The second is a mock clock which will only make forward progress when programmatically adjusted.

func New Uses

func New() Clock

New returns an instance of a real-time clock.

type Mock Uses

type Mock struct {
    sync.Mutex
    // contains filtered or unexported fields
}

Mock represents a mock clock that only moves forward programmically. It can be preferable to a real-time clock when testing time-based functionality.

func NewMock Uses

func NewMock() *Mock

NewMock returns an instance of a mock clock. The current time of the mock clock on initialization is the Unix epoch.

func (*Mock) Add Uses

func (m *Mock) Add(d time.Duration)

Add moves the current time of the mock clock forward by the duration. This should only be called from a single goroutine at a time.

func (*Mock) After Uses

func (m *Mock) After(d time.Duration) <-chan time.Time

After produces a channel that will emit the time after a duration passes.

func (*Mock) AfterFunc Uses

func (m *Mock) AfterFunc(d time.Duration, f func()) *Timer

AfterFunc waits for the duration to elapse and then executes a function. A Timer is returned that can be stopped.

func (*Mock) Now Uses

func (m *Mock) Now() time.Time

Now returns the current wall time on the mock clock.

func (*Mock) Sleep Uses

func (m *Mock) Sleep(d time.Duration)

Sleep pauses the goroutine for the given duration on the mock clock. The clock must be moved forward in a separate goroutine.

func (*Mock) Timer Uses

func (m *Mock) Timer(d time.Duration) *Timer

Timer produces a timer that will emit a time some duration after now.

type Timer Uses

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

Timer represents a single event.

func (*Timer) Next Uses

func (t *Timer) Next() time.Time

func (*Timer) Tick Uses

func (t *Timer) Tick()

type Timers Uses

type Timers []*Timer

timers represents a list of sortable timers.

func (Timers) Len Uses

func (ts Timers) Len() int

func (Timers) Less Uses

func (ts Timers) Less(i, j int) bool

func (*Timers) Pop Uses

func (ts *Timers) Pop() interface{}

func (*Timers) Push Uses

func (ts *Timers) Push(t interface{})

func (Timers) Swap Uses

func (ts Timers) Swap(i, j int)

Package clock imports 3 packages (graph) and is imported by 2 packages. Updated 2016-10-25. Refresh now. Tools for package owners.