Version: v1.0.2 Latest Latest

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

Go to latest
Published: Apr 1, 2020 License: Apache-2.0 Imports: 4 Imported by: 0



Package custom_sleep_atomic holds a non-blocking circular buffer (but not lockfree) that will grow and shrink to size requirements.

Usage is simple:

b := Buffer{} // or b := Buffer{Max: Unbounded} if setting a custom max buffer size

// This will only block if the Buffer has reached its max size.
if !b.Push(item) {
	fmt.Println("buffer has reached its max size")

v, ok := b.Pop()
if !ok {
	fmt.Println("nothing in the buffer")

b.Force(item) // This will push an item on and will block if we have reached the max buffer size
v = b.Pull() // This will block until an item becomes available.

Type customization note:

You can make this package compiler safe by replacing interface{} with your own custom type and
importing the library containing that type.

We highly suggest this.



View Source
const Unbounded = -1

Unbounded indicates that the Queue should have no memory bounds.


This section is empty.


This section is empty.


type Buffer

type Buffer struct {
	// Max is the maximum size the Buffer can grow to.  Use Unbounded if
	// you wish to grow the buffer to any size. By default this will grow to 1k items.
	Max int
	// contains filtered or unexported fields

Buffer provides a FIFO circular buffer that can grow and shrink as required. Buffer must not be copied after creation (which means use a pointer if passing between functions).

func (*Buffer) Force

func (c *Buffer) Force(item interface{})

Force will push the item onto the buffer and blocks until the operation completes.

func (*Buffer) Pop

func (c *Buffer) Pop() (value interface{}, ok bool)

Pop returns the next value off the circular buffer. If the buffer is empty ok will be false.

func (*Buffer) Pull

func (c *Buffer) Pull() interface{}

Pull pulls an item off the circular buffer. It blocks until a value is found.

func (*Buffer) Push

func (c *Buffer) Push(item interface{}) (ok bool)

Push pushes an item onto the circular buffer. "ok" indicates if this happens.

Jump to

Keyboard shortcuts

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