utl

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2024 License: MIT Imports: 4 Imported by: 7

README

utl

Documentation

Overview

Package utl contains components for handling data in real time systems.

Index

Constants

This section is empty.

Variables

View Source
var ErrTimeout = errors.New("utl: timeout")

ErrTimeout is the error that can be returned by [Blocking.Wait].

Functions

This section is empty.

Types

type Blocking added in v0.3.1

type Blocking interface {
	Done()
	Wait() error
}

Blocking interface.

func WithTimeout added in v0.3.1

func WithTimeout(d time.Duration) Blocking

WithTimeout returns a Blocking interface ready to use..

type Cache

type Cache[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Cache is an in-memory cache of items of type V each having a key K. There is a fixed time-to-live for any item. Items retrieved that exceed that duration are replaced from another source, such as a Redis database.

func NewCache

func NewCache[K comparable, V any](ttl time.Duration, replace func(K) (V, bool)) *Cache[K, V]

NewCache returns a new *Cache ready to use. The function argument is used to replace expired items: given a key value it should return an item V and a boolean set to true if the item was found.

func (*Cache[K, V]) Get

func (x *Cache[K, V]) Get(key K) (item V, ok bool)

Get the item having the given key. Return also true if the item was found, otherwise false.

type ConflatingQueue

type ConflatingQueue[K comparable, V any] struct {
	// contains filtered or unexported fields
}

ConflatingQueue is a queue of items V each having a key K. Multiple items may arrive for a key value but only the most recent item is of interest.

For example, consider a queue of prices. Prices for 'A' then 'B' are pushed to the queue. Another price for 'A' is pushed and this replaces the existing price for 'A' in the queue. When the consumer calls ConflatingQueue.Pop it will return the latest price for A (because 'A' was the first of any price pushed), and a second call will return the price for 'B'.

func NewConflatingQueue

func NewConflatingQueue[K comparable, V any](key func(V) K, options ...ConflatingQueueOption[K, V]) *ConflatingQueue[K, V]

NewConflatingQueue returns a *ConflatingQueue ready to use. The function argument returns the key value for a given item.

func (*ConflatingQueue[K, V]) C

func (x *ConflatingQueue[K, V]) C() chan struct{}

C returns a channel on which a struct{} is sent when the queue is not empty. This is useful when including the queue in a select statement. Use ConflatingQueue.Pop to consume the item.

This channel is only for notification; the length of the channel does not reflect the number of items in the queue.

func (*ConflatingQueue[K, V]) Pop

func (x *ConflatingQueue[K, V]) Pop() V

Pop from the queue. If the queue is empty this will return the zero value of T.

func (*ConflatingQueue[K, V]) Push

func (x *ConflatingQueue[K, V]) Push(item V)

Push an item into the queue. If an item with the same key is in the queue:

  • If no WithConflateOption was specified this item will replace the existing item
  • If WithConflateOption was specified this item will be merged into the existing item using the provided function

type ConflatingQueueOption added in v0.2.0

type ConflatingQueueOption[K comparable, V any] func(*ConflatingQueue[K, V])

ConflatingQueueOption is an option specified when a queue is manufactured.

func WithConflateOption added in v0.3.0

func WithConflateOption[K comparable, V any](conflate func(existing, next V) V) ConflatingQueueOption[K, V]

WithConflateOption specifies that the given function will be used to conflate an item, rather than the default of substitution.

func WithPoolOption added in v0.2.0

func WithPoolOption[K comparable, V any](pool *Pool[V]) ConflatingQueueOption[K, V]

WithPoolOption specifies that conflated items need to be returned to a pool.

type Once

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

Once is a repeatable sync.Once.

func NewOnce

func NewOnce(set bool) *Once

NewOnce returns a *Once ready to use. If the argument is true the returned *Once is set to return true the first time Once.Try is called.

func (*Once) Reset

func (x *Once) Reset() bool

Reset Once. Return true if it was actually reset, false if ignored.

func (*Once) Try

func (x *Once) Try() bool

Try returns true if an action can be taken. If this returns true, subsequent calls to this method will return false until Once.Reset has been called.

type Pool

type Pool[T any] struct {
	// contains filtered or unexported fields
}

A Pool of reusable items.

func NewPool

func NewPool[T any](n int, zero func(T) T) *Pool[T]

NewPool returns a *Pool of the given type with capacity 'n'. The function arguments is to 'zero' an item when it is pushed back to the pool. If T is a pointer type then the function should zero the referenced item, not assign nil. For example:

pool := NewPool(
	2,
	func(item *X) *X {
	    if item == nil {
	        return &X{}
	    }
	    item.x = 0
	    return item
	},
)

func (*Pool[T]) Get added in v0.2.0

func (x *Pool[T]) Get() T

Get returns a reusable item from the pool. If the pool is empty, a new zeroed item is created.

func (*Pool[T]) Recycle added in v0.2.0

func (x *Pool[T]) Recycle(t T)

Recycle returns an item to the pool. The item is zeroed as it is added.

type RateLimiter

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

A RateLimiter for governing the rate of an action.

func NewRateLimiter

func NewRateLimiter(n int, interval time.Duration) *RateLimiter

NewRateLimiter returns a *RateLimiter with the given request rate of 'n' requests over 'interval' time period.

func (*RateLimiter) Block

func (r *RateLimiter) Block()

Block until a token is available.

func (*RateLimiter) Try

func (r *RateLimiter) Try() bool

Try returns true if a token is available, otherwise false. Try does not block.

Jump to

Keyboard shortcuts

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