queue

package
v0.0.0-...-2992cac Latest Latest
Warning

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

Go to latest
Published: May 7, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultChannelCapacity   = 128
	DefaultBufferingIdleTime = 10 * time.Second
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Buffer

type Buffer struct {
	// contains filtered or unexported fields
}

Buffer provides the channel that capacity can be extended dynamically. When make a channel, the capacity is defined by make function, such as make(chan int, 8), it does not support capacity extending. This package is useful in these scene, the capacity of the channel could be set dynamically or set to unrestricted if the memory is enough.

Buffer includes two buffers internal, a go chan and a self-defined queue struct. The input for the Buffer will be inserted to go chan firstly, when the go chan is full, the input elements will be inserted to the self-defined queue struct and then a background goroutine will put the elements in queue into go chan continuously. As you see, the self-defined queue is used for buffering the elements when the go chan is full, and it keeps the order of elements input.

All methods of Buffer are thread-safe.

func NewBuffer

func NewBuffer(options ...BufferOption) *Buffer

NewBuffer create a buffer with the options. The options have default value if inputs are not set. The Dispose method is required to call when the Buffer is not used, or leak of goroutine will be happened.

func (*Buffer) Channel

func (b *Buffer) Channel() <-chan interface{}

Channel return the receiver chan. The chan will be close after Dispose method is called.

func (*Buffer) Dispose

func (b *Buffer) Dispose() []interface{}

Dispose is required to called when the Buffer is not used.

func (*Buffer) Push

func (b *Buffer) Push(elm interface{}) PushResult

Push is input method for Buffer. It's thread-safe. After Dispose method is called, the input element will not be dropped instead of insert.

func (*Buffer) SetCapacity

func (b *Buffer) SetCapacity(cap int)

SetCapacity set the self-defined queue's capacity dynamically.

func (*Buffer) Size

func (b *Buffer) Size() int

type BufferOption

type BufferOption func(*Buffer)

func WithBufferingIdleTime

func WithBufferingIdleTime(dur time.Duration) BufferOption

WithBufferingIdleTime defines the idle time of the background goroutine keeping when the self-defined queue is empty. The default value is 10 seconds.

func WithChannelCapacity

func WithChannelCapacity(cap int) BufferOption

WithChannelCapacity set the channel capacity, this value could not be changed after the Buffer is created. The default value is 128.

func WithQueueCapacity

func WithQueueCapacity(cap int) BufferOption

WithQueueCapacity set the self-defined queue capacity, this value could be changed by SetCapacity method. The default value is unlimited, meaning the queue could be always extended when it's required.

func WithQueuePolicy

func WithQueuePolicy(policy Policy) BufferOption

WithQueuePolicy defines the policy when the queue is full.

PolicyDrop: drop the input element

PolicyRemove: remove the tail of queue and insert the input element

PolicyClear: clear the all queue, and insert element to the new queue.

The default value is PolicyDrop

type Policy

type Policy string
const (
	PolicyDrop   Policy = "drop"
	PolicyRemove Policy = "remove"
	PolicyClear  Policy = "clear"
)

func (Policy) String

func (p Policy) String() string

type PushResult

type PushResult string
const (
	PushToChan         PushResult = "push to chan"
	PushToQueue        PushResult = "push to queue"
	PushToQueueReplace PushResult = "push to queue replace"
	PushDropped        PushResult = "push dropped"
)

func (PushResult) String

func (r PushResult) String() string

Jump to

Keyboard shortcuts

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