ratecount

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2021 License: MPL-2.0 Imports: 3 Imported by: 0

README

Go Reference Go Report Card

Package ratecount provides a thread-safe rate calculator and few helpers.

License

Copyright Chung-Ping Jen ronmi.ren@gmail.com 2021-

MPL v2.0

Documentation

Overview

Package ratecount provides thread-safe rate calculator and few helpers

Index

Examples

Constants

View Source
const (
	KB  = 1e3
	MB  = 1e6
	GB  = 1e9
	TB  = 1e12
	KiB = 1024
	MiB = 1024 * 1024
	GiB = 1024 * 1024 * 1024
	TiB = 1024 * 1024 * 1024 * 1024
)

common constants used with *Counter.In()

Variables

This section is empty.

Functions

This section is empty.

Types

type Counter

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

Counter implements a thread-safe rate counter, See NewAvgCounter for detail.

Example
c := NewAvgCounter(100*time.Millisecond, 3)

for i := 0; i < 51; i++ {
	c.Incr(1)
	time.Sleep(10 * time.Millisecond)
}

// should be 9 or 10
fmt.Println(c.Rate() == 9 || c.Rate() == 10)
Output:
true

func NewAvgCounter

func NewAvgCounter(res time.Duration, n uint) *Counter

NewAvgCounter creates a rate counter computes average of last n time windows.

IT RETURNS nil IF res == 0 || n == 0

It calculates how many "tokens" (in most cases, bytes) is transfered in given time window res, caches last n windows, and calculates average transfer rate. As it calculates AVERAGE OF LAST N windows, you'll get lower rate within first n windows.

For example, NewAvgCounter(time.Second, 5) calculates transfer speed (token per second) for last 5 seconds. NewAvgCounter(10*time.Second, 5) calculates transfer speed (token per 10 seconds) for last 50 seconds.

To be more specific, say you have a precise ticker, which calls Incr(1) at t = 0.5 / 1.5 / 2.5 / ... (1 tick per second), so calling Rate() at t=6 returns average speed of t=1~5, and calling Rate() at t=3 returns result from t=-2~2, or (t1+t2) / 5.

func NewCounter

func NewCounter(res time.Duration) *Counter

NewCounter is shortcut of NewAvgCounter(res, 1)

func (*Counter) In

func (c *Counter) In(i int64) int64

In returns Rate()/i. Use it like a pro: fmt.Printf("%d kb", c.In(KB))

func (*Counter) Incr

func (c *Counter) Incr(n int64)

Incr increases counter in current window

func (*Counter) Rate

func (c *Counter) Rate() int64

Rate retrieves average rate

type RatedReader

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

RatedReader computes transfer rate when you read from it

func NewReader

func NewReader(r io.Reader, c *Counter) *RatedReader

NewReader creates a RatedReader

func (*RatedReader) In

func (r *RatedReader) In(i int64) int64

In wraps Counter.In

func (*RatedReader) Rate

func (r *RatedReader) Rate() int64

Rate wraps Counter.Rate

func (*RatedReader) Read

func (r *RatedReader) Read(p []byte) (n int, err error)

Read implements io.Reader.Read()

type RatedWriter

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

RatedWriter computes transfer rate when you write to it

func NewWriter

func NewWriter(w io.Writer, c *Counter) *RatedWriter

NewWriter creates a RatedWriter

func (*RatedWriter) In

func (w *RatedWriter) In(i int64) int64

In wraps Counter.In()

func (*RatedWriter) Rate

func (w *RatedWriter) Rate() int64

Rate wraps Counter.Rate()

func (*RatedWriter) Write

func (w *RatedWriter) Write(p []byte) (n int, err error)

Write implements io.Writer.Write

Jump to

Keyboard shortcuts

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