priorityq/mq/lib.go

202 lines
4.6 KiB
Go
Raw Permalink Normal View History

// Package mq implements a concurrent, dual-priority message queue.
//
// [Q] is similar to a buffered channel, except that senders can assign one of
// two priority levels to each item, "high" or "low." Receivers will always
// get a high-priority item ahead of any low-priority ones.
//
// For example:
//
// q := mq.Make[string](8)
2023-03-04 05:18:31 +00:00
// q.SendLow("world")
// q.SendHigh("hello")
// word1, _ := mq.Recv()
// word2, _ := mq.Recv()
// fmt.Println(word1, word2)
// q.Close()
// // Output: hello world
//
// # Implementation
//
2023-03-04 05:18:31 +00:00
// Each [Q] has two circular buffers, one for each priority level. Currently,
// the capacities for these are fixed and equal. If one buffer is full,
// attempts to send further items with its priority level will block
// ([Q.Send]) or fail ([Q.TrySend]).
//
2023-03-04 05:18:31 +00:00
// Compared [pq.Q], the limitation on priority levels increases performance,
// as its circular buffers are much less expensive than the heap operations of
// a traditional priority queue.
package mq
2023-03-01 04:33:22 +00:00
import (
"sync"
"gogs.humancabbage.net/sam/priorityq"
2023-03-04 05:18:31 +00:00
"gogs.humancabbage.net/sam/priorityq/pq"
2023-03-04 04:09:51 +00:00
"gogs.humancabbage.net/sam/priorityq/queue"
2023-03-01 04:33:22 +00:00
)
2023-03-04 05:18:31 +00:00
// so that godoc (kinda) works
var _ *pq.Q[int, int]
// Q is a concurrent, dual-priority message queue.
2023-03-01 04:33:22 +00:00
type Q[T any] struct {
*state[T]
}
// Make a new queue.
2023-03-01 04:33:22 +00:00
func Make[T any](cap int) Q[T] {
2023-03-04 04:09:51 +00:00
high := queue.Make[T](cap)
low := queue.Make[T](cap)
2023-03-01 04:33:22 +00:00
s := &state[T]{
high: high,
low: low,
}
s.canRecv = sync.Cond{L: &s.mu}
s.canSendHigh = sync.Cond{L: &s.mu}
s.canSendLow = sync.Cond{L: &s.mu}
2023-03-01 04:33:22 +00:00
return Q[T]{s}
}
type state[T any] struct {
mu sync.Mutex
2023-03-04 04:09:51 +00:00
high queue.Q[T]
low queue.Q[T]
canSendHigh sync.Cond
canSendLow sync.Cond
canRecv sync.Cond
2023-03-01 04:33:22 +00:00
closed bool
}
// Close marks the queue as closed.
//
// Attempting to close an already-closed queue results in a panic.
2023-03-01 04:33:22 +00:00
func (s *state[T]) Close() {
s.mu.Lock()
if s.closed {
s.mu.Unlock()
panic("close of closed queue")
}
2023-03-01 04:33:22 +00:00
s.closed = true
s.mu.Unlock()
s.canRecv.Broadcast()
}
// Recv gets an item, blocking when empty until one is available.
2023-03-01 04:33:22 +00:00
//
// The returned bool will be true if the queue still has items or is open.
// It will be false if the queue is empty and closed.
func (s *state[T]) Recv() (T, bool) {
s.mu.Lock()
defer s.mu.Unlock()
for {
for !s.closed && !s.high.CanPop() && !s.low.CanPop() {
s.canRecv.Wait()
}
if s.closed && !s.high.CanPop() && !s.low.CanPop() {
var empty T
return empty, false
}
if s.high.CanPop() {
value := s.high.PopFront()
s.canSendHigh.Broadcast()
return value, true
}
if s.low.CanPop() {
value := s.low.PopFront()
s.canSendLow.Broadcast()
return value, true
}
}
}
// Send is an alias for SendLow.
func (s *state[T]) Send(value T) {
s.SendLow(value)
}
// SendHigh adds an item with high priority, blocking if full.
2023-03-01 04:33:22 +00:00
func (s *state[T]) SendHigh(value T) {
s.send(value, &s.high, &s.canSendHigh)
2023-03-01 04:33:22 +00:00
}
// SendLow adds an item with low buffer, blocking if full.
2023-03-01 04:33:22 +00:00
func (s *state[T]) SendLow(value T) {
s.send(value, &s.low, &s.canSendLow)
2023-03-01 04:33:22 +00:00
}
// TryRecv attempts to get an item from the queue, without blocking.
2023-03-01 04:33:22 +00:00
//
// The error indicates whether the attempt succeeded, the queue is empty, or
// the queue is closed.
func (s *state[T]) TryRecv() (value T, err error) {
2023-03-01 04:33:22 +00:00
s.mu.Lock()
defer s.mu.Unlock()
if s.high.CanPop() {
value = s.high.PopFront()
s.canSendHigh.Broadcast()
return
}
if s.low.CanPop() {
value = s.low.PopFront()
s.canSendLow.Broadcast()
return
}
if s.closed {
err = priorityq.ErrClosed
} else {
err = priorityq.ErrEmpty
}
2023-03-01 04:33:22 +00:00
return
}
// TrySend is an alias for TrySendLow.
func (s *state[T]) TrySend(value T) error {
return s.trySend(value, &s.low)
}
// TrySendHigh attempts to add an item with high priority, without blocking.
2023-03-01 04:33:22 +00:00
//
// Returns an error from the root priorityq package, or nil if successful.
func (s *state[T]) TrySendHigh(value T) error {
2023-03-01 04:33:22 +00:00
return s.trySend(value, &s.high)
}
// TrySendLow attempts to add an item with low priority, without blocking.
2023-03-01 04:33:22 +00:00
//
// Returns an error from the root priorityq package, or nil if successful.
func (s *state[T]) TrySendLow(value T) error {
2023-03-01 04:33:22 +00:00
return s.trySend(value, &s.low)
}
2023-03-04 04:09:51 +00:00
func (s *state[T]) send(value T, buf *queue.Q[T], cond *sync.Cond) {
2023-03-01 04:33:22 +00:00
s.mu.Lock()
defer s.mu.Unlock()
for {
for !s.closed && !buf.CanPush() {
cond.Wait()
}
2023-03-01 04:33:22 +00:00
if s.closed {
panic("send on closed queue")
}
if buf.CanPush() {
buf.PushBack(value)
s.canRecv.Broadcast()
return
}
}
}
2023-03-04 04:09:51 +00:00
func (s *state[T]) trySend(value T, buf *queue.Q[T]) error {
2023-03-01 04:33:22 +00:00
s.mu.Lock()
defer s.mu.Unlock()
if s.closed {
return priorityq.ErrClosed
}
2023-03-01 04:33:22 +00:00
if !buf.CanPush() {
return priorityq.ErrFull
2023-03-01 04:33:22 +00:00
}
buf.PushBack(value)
s.canRecv.Broadcast()
return nil
2023-03-01 04:33:22 +00:00
}