From a946715b7e42651b89b3353761931b4bff3b2b00 Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Thu, 9 Feb 2023 12:29:49 +0200 Subject: [PATCH] watchtower/wtdb: add a generic Queue interface --- watchtower/wtdb/queue.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 watchtower/wtdb/queue.go diff --git a/watchtower/wtdb/queue.go b/watchtower/wtdb/queue.go new file mode 100644 index 000000000..58d2a8ca8 --- /dev/null +++ b/watchtower/wtdb/queue.go @@ -0,0 +1,22 @@ +package wtdb + +import "errors" + +// ErrEmptyQueue is returned from Pop if there are no items left in the Queue. +var ErrEmptyQueue = errors.New("queue is empty") + +// Queue is an interface describing a FIFO queue for any generic type T. +type Queue[T any] interface { + // Len returns the number of tasks in the queue. + Len() (uint64, error) + + // Push pushes new T items to the tail of the queue. + Push(items ...T) error + + // PopUpTo attempts to pop up to n items from the head of the queue. If + // no more items are in the queue then ErrEmptyQueue is returned. + PopUpTo(n int) ([]T, error) + + // PushHead pushes new T items to the head of the queue. + PushHead(items ...T) error +}