Documentation
¶
Overview ¶
Package utl contains components for handling data in real time systems.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrTimeout = errors.New("utl: timeout")
ErrTimeout is the error that can be returned by [Blocking.Wait].
Functions ¶
This section is empty.
Types ¶
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.
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 ¶
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) Try ¶
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 ¶
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
},
)
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) Try ¶
func (r *RateLimiter) Try() bool
Try returns true if a token is available, otherwise false. Try does not block.