utils

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2023 License: Apache-2.0 Imports: 9 Imported by: 15

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrDisposed is returned when an operation is performed on a disposed
	// queue.
	ErrDisposed = errors.New(`queue: disposed`)

	// ErrTimeout is returned when an applicable queue operation times out.
	ErrTimeout = errors.New(`queue: poll timed out`)

	// ErrEmpty is returned when an non-applicable queue operation was called
	// due to the queue's empty item state
	ErrEmpty = errors.New(`queue: empty queue`)
)

Functions

func Assert

func Assert(err error, msg ...interface{})

Assert if nil != err

func AssertBytes

func AssertBytes(b []byte, err error) []byte

AssertBytes check error

func AssertIf

func AssertIf(exp bool, msg string, args ...interface{})

AssertIf exp

func AssertLength

func AssertLength(n int, err error) int

AssertLength check error

func AssertLong

func AssertLong(n int64, err error) int64

AssertLong check error

func CountOf

func CountOf(buffs [][]byte) (n int64)

CountOf count bytes of buffs

func MustToBytes

func MustToBytes(message interface{}) []byte

MustToBytes any error to panic

func MustToReader

func MustToReader(message interface{}) io.Reader

MustToReader any error to panic

func ToBytes

func ToBytes(message interface{}) ([]byte, error)

ToBytes unwrap a message to []byte

func ToReader

func ToReader(message interface{}) (io.Reader, error)

ToReader wrap message to io.Reader

Types

type ByteReader

type ByteReader interface {
	io.Reader
	io.ByteReader
}

ByteReader defines byte reader

func NewByteReader

func NewByteReader(r io.Reader) ByteReader

NewByteReader create a ByteReader from io.Reader

type RingBuffer added in v1.1.0

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

RingBuffer is a MPMC buffer that achieves threadsafety with CAS operations only. A put on full or get on empty call will block until an item is put or retrieved. Calling Dispose on the RingBuffer will unblock any blocked threads with an error. This buffer is similar to the buffer described here: http://www.1024cores.net/home/lock-free-algorithms/queues/bounded-mpmc-queue with some minor additions.

func NewRingBuffer added in v1.1.0

func NewRingBuffer(size uint64) *RingBuffer

NewRingBuffer will allocate, initialize, and return a ring buffer with the specified size.

func (*RingBuffer) Cap added in v1.1.0

func (rb *RingBuffer) Cap() uint64

Cap returns the capacity of this ring buffer.

func (*RingBuffer) Dispose added in v1.1.0

func (rb *RingBuffer) Dispose()

Dispose will dispose of this queue and free any blocked threads in the Put and/or Get methods. Calling those methods on a disposed queue will return an error.

func (*RingBuffer) Get added in v1.1.0

func (rb *RingBuffer) Get() (interface{}, error)

Get will return the next item in the queue. This call will block if the queue is empty. This call will unblock when an item is added to the queue or Dispose is called on the queue. An error will be returned if the queue is disposed.

func (*RingBuffer) IsDisposed added in v1.1.0

func (rb *RingBuffer) IsDisposed() bool

IsDisposed will return a bool indicating if this queue has been disposed.

func (*RingBuffer) Len added in v1.1.0

func (rb *RingBuffer) Len() uint64

Len returns the number of items in the queue.

func (*RingBuffer) Offer added in v1.1.0

func (rb *RingBuffer) Offer(item interface{}) (bool, error)

Offer adds the provided item to the queue if there is space. If the queue is full, this call will return false. An error will be returned if the queue is disposed.

func (*RingBuffer) Poll added in v1.1.0

func (rb *RingBuffer) Poll(timeout time.Duration) (interface{}, error)

Poll will return the next item in the queue. This call will block if the queue is empty. This call will unblock when an item is added to the queue, Dispose is called on the queue, or the timeout is reached. An error will be returned if the queue is disposed or a timeout occurs. A non-positive timeout will block indefinitely.

func (*RingBuffer) Put added in v1.1.0

func (rb *RingBuffer) Put(item interface{}) error

Put adds the provided item to the queue. If the queue is full, this call will block until an item is added to the queue or Dispose is called on the queue. An error will be returned if the queue is disposed.

type SpinLocker added in v1.1.0

type SpinLocker int32

func (*SpinLocker) Lock added in v1.1.0

func (s *SpinLocker) Lock()

func (*SpinLocker) Unlock added in v1.1.0

func (s *SpinLocker) Unlock()

Jump to

Keyboard shortcuts

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