ticker

package
v0.5.1-beta-rc2 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2018 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Mock

type Mock struct {

	// Force is used to force-feed a ticks into the ticker. Useful for
	// debugging when trying to wake an event.
	Force chan time.Time
	// contains filtered or unexported fields
}

Mock implements the Ticker interface, and provides a method of force-feeding ticks, even while paused.

func MockNew

func MockNew(interval time.Duration) *Mock

MockNew returns a Mock Ticker, used for testing and debugging. It supports the ability to force-feed events that get output by the

func (*Mock) Pause

func (m *Mock) Pause()

Pause suspends the underlying ticker, such that Ticks() stops signaling at regular intervals.

NOTE: Part of the Ticker interface.

func (*Mock) Resume

func (m *Mock) Resume()

Resume starts underlying time.Ticker and causes the ticker to begin delivering scheduled events.

NOTE: Part of the Ticker interface.

func (*Mock) Stop

func (m *Mock) Stop()

Stop suspends the underlying ticker, such that Ticks() stops signaling at regular intervals, and permanently frees up any resources.

NOTE: Part of the Ticker interface.

func (*Mock) Ticks

func (m *Mock) Ticks() <-chan time.Time

Ticks returns a receive-only channel that delivers times at the ticker's prescribed interval when active. Force-fed ticks can be delivered at any time.

NOTE: Part of the Ticker interface.

type Ticker

type Ticker interface {
	// Ticks returns a read-only channel delivering ticks according to a
	// prescribed interval. The value returned does not need to be the same
	// channel, and may be nil.
	//
	// NOTE: Callers should assume that reads from Ticks() are stale after
	// any invocations of Resume, Pause, or Stop.
	Ticks() <-chan time.Time

	// Resume starts or resumes the underlying ticker, such that Ticks()
	// will fire at regular intervals. After calling Resume, Ticks() should
	// minimally send ticks at the prescribed interval.
	//
	// NOTE: It MUST be safe to call Resume at any time, and more than once
	// successively.
	Resume()

	// Pause suspends the underlying ticker, such that Ticks() stops
	// signaling at regular intervals. After calling Pause, the ticker
	// should not send any ticks scheduled with the chosen interval. Forced
	// ticks are still permissible, as in the case of the Mock Ticker.
	//
	// NOTE: It MUST be safe to call Pause at any time, and more than once
	// successively.
	Pause()

	// Stop suspends the underlying ticker, such that Ticks() stops
	// signaling at regular intervals, and permanently frees up any
	// remaining resources.
	//
	// NOTE: The behavior of a Ticker is undefined after calling Stop.
	Stop()
}

Ticker defines a resumable ticker interface, whose activity can be toggled to free up resources during periods of inactivity.

Example of resuming ticker:

ticker.Resume() // can remove to keep inactive at first
defer ticker.Stop()
for {
  select {
    case <-ticker.Tick():
      if shouldGoInactive {
        ticker.Pause()
        continue
      }
      ...

    case <-otherEvent:
      ...
      if shouldGoActive {
        ticker.Resume()
      }
  }

NOTE: ONE DOES NOT SIMPLY assume that Tickers are safe for concurrent access.

func New

func New(interval time.Duration) Ticker

New returns a new ticker that signals with the given interval when not paused. The ticker starts off inactive.

Jump to

Keyboard shortcuts

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