buffer

package
v1.2.12 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	// AutoExpand 自动展开标识
	AutoExpand = -1

	// ResetOffMark 重叠标记
	ResetOffMark = -1

	// MinRead MaxRead 最大最小读取
	MinRead = 1 << 9
	MaxRead = 1 << 17

	// DefaultSize 默认大小
	DefaultSize = 1 << 4

	// MaxBufferLength 最大缓冲大小
	MaxBufferLength = 1 << 20

	// MaxThreshold 最大阈值
	MaxThreshold = 1 << 22
)

Variables

View Source
var (
	ErrEOF               = errors.New("EOF")
	ErrTooLarge          = errors.New("io buffer: too large")
	ErrNegativeCount     = errors.New("io buffer: negative count")
	ErrInvalidWriteCount = errors.New("io buffer: invalid write count")
	ErrClosedPipeWrite   = errors.New("write on closed buffer")
	ErrDuplicate         = errors.New("PutIoPool duplicate")
)
View Source
var ConnReadTimeout = 15 * time.Second

ConnReadTimeout 连接超时时间

Functions

func GetBytes

func GetBytes(size int) *[]byte

GetBytes 提供外部接口 获取 size 大小的 buffer

Example

Byte 复用

// size 2^6 - 2^18
// 返回向上取整的 2的整数倍 cap, len == size
// 其他特殊的或者在运行期间扩容的 将会被清空
slice := GetBytes(1024)
_ = slice
Output:

func PutBytes

func PutBytes(buf *[]byte)

PutBytes 提供外部接口 将buffer 放回 pool中

Example
slice := make([]byte, 1024)
// 将slice回收
PutBytes(&slice)
Output:

func PutIoPool

func PutIoPool(buf IoBuffer) error

PutIoPool 向pool中回填一个 IoBuffer

Example
mockIoPool := newIoBuffer(1024)
err := PutIoPool(mockIoPool)
if err != nil {
	// 如果一个对象已经被回收了,再次引用被回收的对象会触发错误
}
Output:

Types

type BytePoolContainer

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

BytePoolContainer 暴露给外部使用的容器对象

func NewBytePoolContainer

func NewBytePoolContainer() *BytePoolContainer

NewBytePoolContainer 实例化外部容器

func (*BytePoolContainer) Get

func (B *BytePoolContainer) Get(size int) *[]byte

func (*BytePoolContainer) Reset

func (B *BytePoolContainer) Reset()

Reset 将 bytes 中缓存的buffer全部归还给 pool中

type IoBuffer

type IoBuffer interface {
	// Read reads the next len(p) bytes from the buffer or until the buffer
	// is drained. The return value n is the number of bytes read. If the
	// buffer has no data to return, err is io.EOF (unless len(p) is zero);
	// otherwise it is nil.
	Read(p []byte) (n int, err error)

	// ReadOnce make a one-shot read and appends it to the buffer, growing
	// the buffer as needed. The return value n is the number of bytes read. Any
	// error except io.EOF encountered during the read is also returned. If the
	// buffer becomes too large, ReadFrom will panic with ErrTooLarge.
	ReadOnce(r io.Reader) (n int64, err error)

	// ReadFrom reads data from r until EOF and appends it to the buffer, growing
	// the buffer as needed. The return value n is the number of bytes read. Any
	// error except io.EOF encountered during the read is also returned. If the
	// buffer becomes too large, ReadFrom will panic with ErrTooLarge.
	ReadFrom(r io.Reader) (n int64, err error)

	// Grow updates the length of the buffer by n, growing the buffer as
	// needed. The return value n is the length of p; err is always nil. If the
	// buffer becomes too large, Write will panic with ErrTooLarge.
	Grow(n int) error

	// Write appends the contents of p to the buffer, growing the buffer as
	// needed. The return value n is the length of p; err is always nil. If the
	// buffer becomes too large, Write will panic with ErrTooLarge.
	Write(p []byte) (n int, err error)

	// WriteString appends the string to the buffer, growing the buffer as
	// needed. The return value n is the length of s; err is always nil. If the
	// buffer becomes too large, Write will panic with ErrTooLarge.
	WriteString(s string) (n int, err error)

	// WriteByte appends the byte to the buffer, growing the buffer as
	// needed. The return value n is the length of s; err is always nil. If the
	// buffer becomes too large, Write will panic with ErrTooLarge.
	WriteByte(p byte) error

	// WriteUint16 appends the uint16 to the buffer, growing the buffer as
	// needed. The return value n is the length of s; err is always nil. If the
	// buffer becomes too large, Write will panic with ErrTooLarge.
	WriteUint16(p uint16) error

	// WriteUint32 appends the uint32 to the buffer, growing the buffer as
	// needed. The return value n is the length of s; err is always nil. If the
	// buffer becomes too large, Write will panic with ErrTooLarge.
	WriteUint32(p uint32) error

	// WriteUint64 appends the uint64 to the buffer, growing the buffer as
	// needed. The return value n is the length of s; err is always nil. If the
	// buffer becomes too large, Write will panic with ErrTooLarge.
	WriteUint64(p uint64) error

	// WriteTo writes data to w until the buffer is drained or an error occurs.
	// The return value n is the number of bytes written; it always fits into an
	// int, but it is int64 to match the io.WriterTo interface. Any error
	// encountered during the write is also returned.
	WriteTo(w io.Writer) (n int64, err error)

	// Peek returns n bytes from buffer, without draining any buffered data.
	// If n > readable buffer, nil will be returned.
	// It can be used in codec to check first-n-bytes magic bytes
	// Note: do not change content in return bytes, use write instead
	Peek(n int) []byte

	// Bytes returns all bytes from buffer, without draining any buffered data.
	// It can be used to get fixed-length content, such as headers, body.
	// Note: do not change content in return bytes, use write instead
	Bytes() []byte

	// Drain drains a offset length of bytes in buffer.
	// It can be used with Bytes(), after consuming a fixed-length of data
	Drain(offset int)

	// Len returns the number of bytes of the unread portion of the buffer;
	// b.Len() == len(b.Bytes()).
	Len() int

	// Cap returns the capacity of the buffer's underlying byte slice, that is, the
	// total space allocated for the buffer's data.
	Cap() int

	// Reset resets the buffer to be empty,
	// but it retains the underlying storage for use by future writes.
	Reset()

	// Clone makes a copy of IoBuffer struct
	Clone() IoBuffer

	// String returns the contents of the unread portion of the buffer
	// as a string. If the Buffer is a nil pointer, it returns "<nil>".
	String() string

	// Alloc alloc bytes from BytePoolBuffer
	Alloc(int)

	// Free free bytes to BytePoolBuffer
	Free()

	// Count sets and returns reference count
	Count(int32) int32

	// EOF returns whether Io is EOF on the connection
	EOF() bool

	// SetEOF sets the IoBuffer EOF
	SetEOF(eof bool)
	Append(data []byte) error
	CloseWithError(err error)
}

IoBuffer 接口定义

func GetIoPool

func GetIoPool(size int) IoBuffer

GetIoPool 从pool中 获取一个 IoBuffer

Example
// 创建一个缓冲区为 cap大小的 io对象
io := GetIoPool(1024)
_ = io
Output:

func NewIoBuffer

func NewIoBuffer(size int) IoBuffer

NewIoBuffer GetIoPool 别名

func NewIoBufferBytes

func NewIoBufferBytes(bytes []byte) IoBuffer

NewIoBufferBytes []byte 生成 IoBuffer

func NewIoBufferEOF

func NewIoBufferEOF() IoBuffer

NewIoBufferEOF 生成一个EOF的 IoBuffer

func NewIoBufferString

func NewIoBufferString(s string) IoBuffer

NewIoBufferString string 生成 IoBuffer

func NewPipe

func NewPipe(cap int) IoBuffer

NewPipe 初始化 pipe IoBuffer

type IoPool

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

IoPool 存储 IoBuffer Pool

Jump to

Keyboard shortcuts

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