Documentation ¶
Index ¶
- type OverflowHandler
- type RingBuffer
- func (b *RingBuffer) Bytes() []byte
- func (b *RingBuffer) Capacity() int64
- func (b *RingBuffer) Len() int64
- func (b *RingBuffer) Reset()
- func (b *RingBuffer) Size() int64
- func (b *RingBuffer) String() string
- func (b *RingBuffer) TotalWritten() int64
- func (b *RingBuffer) Write(buf []byte) (int, error)
- type RingBufferOption
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:
- buf.len < rb.size && without rewind
- buf.len < rb.size && rewind
- 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