chanx

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2023 License: MIT Imports: 3 Imported by: 17

README

chanx

Unbounded chan with ringbuffer.

License GoDoc travis Go Report Card coveralls

Refer to the below articles and issues:

  1. https://github.com/golang/go/issues/20352
  2. https://stackoverflow.com/questions/41906146/why-go-channels-limit-the-buffer-size
  3. https://medium.com/capital-one-tech/building-an-unbounded-channel-in-go-789e175cd2cd
  4. https://erikwinter.nl/articles/2020/channel-with-infinite-buffer-in-golang/

Usage

If you want to use it with Go 1.17.x or below, you can use github.com/smallnest/chanx@1.0.0. Since github.com/smallnest/chanx@1.1.0, it support Go generic.

ch := NewUnboundedChan(1000)
// or ch := NewUnboundedChanSize(10,200,1000)

go func() {
    for ...... {
        ...
        ch.In <- ... // send values
        ...
    }

    close(ch.In) // close In channel
}()


for v := range ch.Out { // read values
    fmt.Println(v)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrIsEmpty = errors.New("ringbuffer is empty")

Functions

This section is empty.

Types

type RingBuffer

type RingBuffer[T any] struct {
	// contains filtered or unexported fields
}

RingBuffer is a ring buffer for common types. It never is full and always grows if it will be full. It is not thread-safe(goroutine-safe) so you must use the lock-like synchronization primitive to use it in multiple writers and multiple readers.

func NewRingBuffer

func NewRingBuffer[T any](initialSize int) *RingBuffer[T]

func (*RingBuffer[T]) Capacity

func (r *RingBuffer[T]) Capacity() int

Capacity returns the size of the underlying buffer.

func (*RingBuffer[T]) IsEmpty

func (r *RingBuffer[T]) IsEmpty() bool

func (*RingBuffer[T]) Len

func (r *RingBuffer[T]) Len() int

func (*RingBuffer[T]) Peek

func (r *RingBuffer[T]) Peek() T

func (*RingBuffer[T]) Pop

func (r *RingBuffer[T]) Pop() T

func (*RingBuffer[T]) Read

func (r *RingBuffer[T]) Read() (T, error)

func (*RingBuffer[T]) Reset

func (r *RingBuffer[T]) Reset()

func (*RingBuffer[T]) Write

func (r *RingBuffer[T]) Write(v T)

type UnboundedChan

type UnboundedChan[T any] struct {
	In  chan<- T // channel for write
	Out <-chan T // channel for read
	// contains filtered or unexported fields
}

UnboundedChan is an unbounded chan. In is used to write without blocking, which supports multiple writers. and Out is used to read, which supports multiple readers. You can close the in channel if you want.

func NewUnboundedChan

func NewUnboundedChan[T any](ctx context.Context, initCapacity int) *UnboundedChan[T]

NewUnboundedChan creates the unbounded chan. in is used to write without blocking, which supports multiple writers. and out is used to read, which supports multiple readers. You can close the in channel if you want.

func NewUnboundedChanSize

func NewUnboundedChanSize[T any](ctx context.Context, initInCapacity, initOutCapacity, initBufCapacity int) *UnboundedChan[T]

NewUnboundedChanSize is like NewUnboundedChan but you can set initial capacity for In, Out, Buffer.

func (UnboundedChan[T]) BufLen

func (c UnboundedChan[T]) BufLen() int

BufLen returns len of the buffer. It is not accurate and only for your evaluating approximate number of elements in this chan, see https://github.com/smallnest/chanx/issues/7.

func (UnboundedChan[T]) Len

func (c UnboundedChan[T]) Len() int

Len returns len of In plus len of Out plus len of buffer. It is not accurate and only for your evaluating approximate number of elements in this chan, see https://github.com/smallnest/chanx/issues/7.

Jump to

Keyboard shortcuts

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