timewindow

package
v0.0.0-...-27b2fc9 Latest Latest
Warning

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

Go to latest
Published: May 10, 2017 License: Apache-2.0 Imports: 0 Imported by: 1

Documentation

Overview

Package timewindow provides counts for events in a sliding window of epochs

A Window tracks the number of times a counter has been incremented within a sliding window of epochs. These epochs are normally Unix epochs, but any monotonically incrementing counter is sufficient. The Window is initialized with the size of the sliding window and an epoch to consider as time 0.

As events occur, calling window.Add(time.Now().Unix(), 1) will increment the counter for the current epoch, and move along the sliding window, possibly expiring any counts that have left the window. This count can be retrieved by calling window.Total().

Calling window.Add(time.Now().Unix(), 0) will slide the window forward and expire old elements.

It is acceptable to call window.Add() with an epoch earlier than the currently active epoch. If the time falls outside of the current window, the event will be silently discarded.

Windows are not safe to be called from multiple goroutines.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Window

type Window struct {
	// contains filtered or unexported fields
}

Window is a sliding window of event counts.

func New

func New(epoch0 int64, size int) *Window

New returns a sliding window starting at epoch0 with size seconds of history

func (*Window) Add

func (w *Window) Add(epoch int64, delta int)

Add delta to the counter for epoch and adjust the window if necessary.

func (*Window) Epoch

func (w *Window) Epoch() int64

Epoch returns most recent second for which data has been inserted

func (*Window) Total

func (w *Window) Total() int

Total returns the sum of all counters in the window

Jump to

Keyboard shortcuts

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