ringqueue

package module
v0.0.0-...-41a7607 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2025 License: MIT Imports: 7 Imported by: 1

README

go-ringqueue

Thread-safe Zero-copy Generic Ring (Circular) Queue

What a Ring Queue is and Why use it

So, what is a Ring Queue and why it's useful:

  1. It uses a static / fixed array buffer (no costly allocations)
  2. It does not shift elements on element removal (no costly memcopy)
  3. It can start anywhere in the buffer and loop over the end

img

Original Work

This is a fork of go-container-roundrobin which is licensed under MIT license.

Read full post here: https://www.sergetoro.com/golang-round-robin-queue-from-scratch

MIT License

Copyright (c) 2023 Serge Toro

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

License

MIT License

Copyright (c) 2025 Mohammad Hadi Hosseinpour

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Documentation

Index

Constants

View Source
const (
	WhenFullError = WhenFull(iota)
	WhenFullOverwrite
)
View Source
const (
	WhenEmptyError = WhenEmpty(iota)
	WhenEmptyBlock
)

Variables

View Source
var ErrClosed = fmt.Errorf("queue is closed")
View Source
var ErrEmptyQueue = fmt.Errorf("queue is empty")
View Source
var ErrFullQueue = fmt.Errorf("queue is full")
View Source
var ErrUnsupported = errors.ErrUnsupported

Functions

This section is empty.

Types

type OnCloseFunc

type OnCloseFunc[T any] func(data T)

type RingQueue

type RingQueue[T any] interface {
	fmt.Stringer
	io.Closer
	SetPopDeadline(t time.Time) error
	Len() int
	Cap() int
	Push(element T) (newLen int, err error)
	Pop() (elem T, newLen int, err error)
	Peek() (elem T, len int, err error)
}

func NewSafe

func NewSafe[T any](capacity int, whenFull WhenFull, whenEmpty WhenEmpty, onCloseFunc OnCloseFunc[T]) (RingQueue[T], error)

func NewUnsafe

func NewUnsafe[T any](capacity int, whenFull WhenFull, whenEmpty WhenEmpty, onCloseFunc OnCloseFunc[T]) (RingQueue[T], error)

type WhenEmpty

type WhenEmpty int

type WhenFull

type WhenFull int

Jump to

Keyboard shortcuts

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