untitled

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2022 License: MIT Imports: 1 Imported by: 0

README

untitled

here is a referenced implementation for standard ring-buffer generally.

Not ready for production environment.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type OverflowHandler

type OverflowHandler func(rb *RingBuffer, bufWriting []byte, sizeTarget int64)

OverflowHandler is a user-defined callback, it will be triggered if writer is writing large data into ring buffer and making it full.

type RingBuffer

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

RingBuffer implements a circular buffer. It is a fixed size, and new writes overwrite older data, such that for a buffer of size N, for any amount of writes, only the last N bytes are retained.

func NewBuffer

func NewBuffer(size int64, opts ...RingBufferOption) (*RingBuffer, error)

NewBuffer creates a new buffer of a given size. The size must be greater than 0.

func (*RingBuffer) Bytes

func (b *RingBuffer) Bytes() []byte

Bytes provides a slice of the bytes written. This slice should not be written to.

func (*RingBuffer) Capacity

func (b *RingBuffer) Capacity() int64

Capacity returns the size/capacity of the buffer

func (*RingBuffer) Len

func (b *RingBuffer) Len() int64

Len returns the available length of the buffer

func (*RingBuffer) Reset

func (b *RingBuffer) Reset()

Reset resets the buffer so it has no content.

func (*RingBuffer) Size

func (b *RingBuffer) Size() int64

Size returns the size/capacity of the buffer

采用额外一字节作为头尾指针冲撞检测位置,因此rb的有效尺寸为 Size() - 1

func (*RingBuffer) String

func (b *RingBuffer) String() string

String returns the contents of the buffer as a string

func (*RingBuffer) TotalWritten

func (b *RingBuffer) TotalWritten() int64

TotalWritten provides the total number of bytes written

func (*RingBuffer) Write

func (b *RingBuffer) Write(buf []byte) (int, error)

Write writes up to len(buf) bytes to the internal ring, overriding older data if necessary.

Write 写入buf到ringbuffer内部 如果需要会覆盖旧数据(fifo)

cases:

  1. buf.len < rb.size && without rewind
  2. buf.len < rb.size && rewind
  3. buf.len >= rb.size: buf内容按照rb.size取模,剩余部分写入rb,同时触发overflow事件

type RingBufferOption

type RingBufferOption func(*RingBuffer)

RingBufferOption gives the options prototypes for NewBuffer()

func WithOverflowHandler

func WithOverflowHandler(oh OverflowHandler) RingBufferOption

WithOverflowHandler can handle ring buffer overflow event

Jump to

Keyboard shortcuts

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