cticker

package module
v0.0.0-...-bd2f021 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2019 License: BSD-2-Clause Imports: 3 Imported by: 6

README

go-cticker Go Report Card License GoDoc Build Status

go-cticker is a Go library that provides a ticker which ticks according to wall clock and is reliable under clock drift and clock adjustments; that is if you ask it to tick on the minute it will ensure that it does so even if the underlying clock is inaccurate or gets adjusted.

Features

  • Reliable under clock drift.
  • Reliable under clock adjustments.

Installation

go get -u github.com/multiplay/go-cticker

Examples

Using go-cticker is very much like time.NewTicker with the addition of an accuracy and start time.

The following creates a ticker which ticks on the minute according the hosts wall clock with an accuracy of plus or minus one second.

package main

import (
	"fmt"
	"time"

	"github.com/multiplay/go-cticker"
)

func main() {
	t := cticker.New(time.Minute, time.Second)
	for tick := range t.C {
		// Process tick
		fmt.Println("tick:", tick)
	}
}

Documentation

License

go-cticker is available under the BSD 2-Clause License.

Documentation

Overview

Package cticker provides a ticker which ticks according to wall clock instead of monotonic clock. It is reliable under both clock drift and clock adjustments, that is if you ask it to tick on the minute it will ensure that it does so even if the underlying clock is inaccurate or gets adjusted.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Ticker

type Ticker struct {
	C <-chan time.Time // The channel on which ticks are delivered.
	// contains filtered or unexported fields
}

Ticker holds a channel that delivers `ticks` on wall clock boundaries. Unlike time.Ticker it will fire early if the clock is adjusted to an earlier time, therefore it can be used for events which need to trigger on wall clock boundaries e.g. every minute on the minute.

Example

TODO(steve): remove this nolint when go tool vet is fixed. nolint: vet

t := cticker.New(time.Minute, time.Second)
for tick := range t.C {
	// Process tick
	fmt.Println("tick:", tick)
}
Output:

func New

func New(d, accuracy time.Duration) *Ticker

New returns a new Ticker containing a channel that will send the time at d wall clock boundaries plus or minus accuracy. It will drop ticks to make up for slow receivers. The duration d must be greater than zero; if not, NewTicker will panic. The accuracy must be less than d; it not, NewTicker will panic. Stop the ticker to release its associated resources.

func (*Ticker) Stop

func (t *Ticker) Stop()

Stop turns off the ticker. After Stop, no more ticks will be sent. Stop does not close the channel to prevent a read from the channel succeeding incorrectly.

Jump to

Keyboard shortcuts

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