decayment

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

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

Go to latest
Published: Dec 13, 2022 License: MIT Imports: 4 Imported by: 0

README

Decayment GoDoc

Package decayment decays counters after not being updated during threshold

Documentation

Overview

Package decayment decays counters after not being updated during threshold

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type States

type States struct {
	Counts *xsync.MapOf[string, int]
	Seens  *xsync.MapOf[string, time.Time]
	// contains filtered or unexported fields
}

States to be decayed

func New

func New() *States

New state instance

func (*States) Decode

func (s *States) Decode(b []byte) error

Decode the state from a byte array

func (*States) Decr

func (s *States) Decr(threshold int) (uint32, error)

Decr decrements all keys if seen below threshold*time.Second

Example
now := time.Now()
then := now.Add(-2 * time.Second)
states := New()
key := "127.0.0.1"
_ = states.IncrTime("127.0.0.1", then)
count, _ := states.Decr(1)
if v, ok := states.Counts.Load(key); ok && v == 0 && count == 1 {
	fmt.Println("olleh")
}
Output:

olleh

func (*States) Encode

func (s *States) Encode() ([]byte, error)

Encode the state as a byte array

func (*States) Incr

func (s *States) Incr(key string) error

Incr increments parameter key by one setting seen to now

Example
states := New()
key := "127.0.0.1"
_ = states.Incr(key)
if v, ok := states.Counts.Load(key); ok && v == 1 {
	fmt.Println("olleh")
}
Output:

olleh

func (*States) IncrTime

func (s *States) IncrTime(key string, t time.Time) error

IncrTime increments parameter key by one setting seen to parameter t Used for testing to increment a key in the past

Example
states := New()
key := "127.0.0.1"
_ = states.IncrTime(key, time.Now())
if v, ok := states.Counts.Load(key); ok && v == 1 {
	fmt.Println("olleh")
}
Output:

olleh

func (*States) Start

func (s *States) Start(interval int, threshold int)

Start starts the decrementing loop with interval*time.Second and threshold*time.Second

func (*States) Stop

func (s *States) Stop()

Stop stops the decrement loop

Jump to

Keyboard shortcuts

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