Documentation ¶
Overview ¶
Package decayment decays counters after not being updated during threshold
Index ¶
- type States
- func (s *States) Decode(b []byte) error
- func (s *States) Decr(threshold int) (uint32, error)
- func (s *States) Encode() ([]byte, error)
- func (s *States) Incr(key string) error
- func (s *States) IncrTime(key string, t time.Time) error
- func (s *States) Start(interval int, threshold int)
- func (s *States) Stop()
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 (*States) Decr ¶
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) Incr ¶
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 ¶
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
Click to show internal directories.
Click to hide internal directories.