buffer

package
v1.2.3 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2022 License: Apache-2.0 Imports: 12 Imported by: 92

README

IoBuffer复用

// GetIoBuffer returns IoBuffer from pool
func GetIoBuffer(size int) types.IoBuffer {
	return ibPool.take(size)
}

// PutIoBuffer returns IoBuffer to pool
func PutIoBuffer(buf types.IoBuffer) {
	if buf.Count(-1) != 0 {
		return
	}
	ibPool.give(buf)
}

Byte复用

// GetBytes returns *[]byte from byteBufferPool
func GetBytes(size int) *[]byte {
	p := getByteBufferPool()
	return p.take(size)
}

// PutBytes Put *[]byte to byteBufferPool
func PutBytes(buf *[]byte) {
	p := getByteBufferPool()
	p.give(buf)
}

自定义结构体复用

请求维度的内存申请复用
  • 模板
package example

import (
        "context"

        "mosn.io/mosn/pkg/buffer"
        "net/http"
)

var ins exampleBufferCtx

// 注册buffer类型到内存复用框架
func init() {
        buffer.RegisterBuffer(&ins)
}

// 需要包含 buffer.TempBufferCtx 到自定义的Ctx, 且要放到第一位
type exampleBufferCtx struct{
        buffer.TempBufferCtx
}

// 实现New()函数, 用于生成自定义buffer
func (ctx exampleBufferCtx) New() interface{} {
        buffer := new(exampleBuffers)
        return buffer
}

// 实现Reset()函数, 用于回收buffer之前,重置buffer内复用的结构体
func (ctx exampleBufferCtx) Reset(i interface{}) {
        buf := i.(*exampleBufferCtx)
        *buf = exampleBufferCtx{}
}

// 自定义buffer结构体,包含需要复用的结构体
type exampleBuffers struct {
        req http.Request
        rsp http.Response
}

// 通过ctx获取复用buffer
func exampleBuffersByContext(ctx context.Context) *exampleBuffers {
        poolCtx := buffer.PoolContext(ctx)
        return poolCtx.Find(&ins, nil).(*exampleBuffers)
}
  • 使用方式
func run(ctx context.Context) {
    // 通过ctx获取内存块
        buffer := exampleBuffersByContext(ctx)
        // 通过指针使用
        req := &buffer.req
        rsp := &buffer.rsp
}

Documentation

Overview

nolint

nolint

Index

Constants

View Source
const (
	AutoExpand      = -1
	MinRead         = 1 << 9
	MaxRead         = 1 << 17
	ResetOffMark    = -1
	DefaultSize     = 1 << 4
	MaxBufferLength = 1 << 20
	MaxThreshold    = 1 << 22
)

Variables

View Source
var (
	EOF                  = 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")
	ConnReadTimeout      = 15 * time.Second
)

Functions

func CleanBufferPoolContext added in v1.2.0

func CleanBufferPoolContext(ctx context.Context) context.Context

CleanBufferPoolContext cleans the bufferValue in the context

func GetBytes

func GetBytes(size int) *[]byte

GetBytes returns *[]byte from byteBufferPool

func GetBytesByContext added in v1.2.0

func GetBytesByContext(context context.Context, size int) *[]byte

GetBytesByContext returns []byte from byteBufferPool by context

func NewBufferPoolContext added in v1.2.0

func NewBufferPoolContext(ctx context.Context) context.Context

NewBufferPoolContext returns a context with bufferValue

func PoolContext added in v1.2.0

func PoolContext(ctx context.Context) *bufferValue

PoolContext returns bufferValue by context

func PutBytes

func PutBytes(buf *[]byte)

PutBytes Put *[]byte to byteBufferPool

func PutIoBuffer

func PutIoBuffer(buf IoBuffer) error

PutIoBuffer is a a wrapper for ibPool

func RegisterBuffer added in v1.2.0

func RegisterBuffer(poolCtx BufferPoolCtx)

func SetLogFunc

func SetLogFunc(f func(msg string))

SetLogFunc use f overwrite logFunc.

func TransmitBufferPoolContext added in v1.2.0

func TransmitBufferPoolContext(dst context.Context, src context.Context)

TransmitBufferPoolContext copy a context

Types

type BufferPoolCtx added in v1.2.0

type BufferPoolCtx interface {
	// Index returns the bufferpool's Index
	Index() int

	// New returns the buffer
	New() interface{}

	// Reset resets the buffer
	Reset(interface{})
}

BufferPoolCtx is the bufferpool's context nolint

type ByteBufferCtx added in v1.2.0

type ByteBufferCtx struct {
	TempBufferCtx
}

func (ByteBufferCtx) New added in v1.2.0

func (ctx ByteBufferCtx) New() interface{}

func (ByteBufferCtx) Reset added in v1.2.0

func (ctx ByteBufferCtx) Reset(i interface{})

type ByteBufferPoolContainer

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

func NewByteBufferPoolContainer

func NewByteBufferPoolContainer() *ByteBufferPoolContainer

func (*ByteBufferPoolContainer) Reset

func (c *ByteBufferPoolContainer) Reset()

func (*ByteBufferPoolContainer) Take

func (c *ByteBufferPoolContainer) Take(size int) *[]byte

type IoBuffer

type IoBuffer = api.IoBuffer

keep this alias for compatbile. if you write new code, use api.IoBuffer instead.

func GetIoBuffer

func GetIoBuffer(size int) IoBuffer

GetIoBuffer is a wrapper for ibPool

func NewIoBuffer

func NewIoBuffer(size int) IoBuffer

NewIoBuffer is an alias for GetIoBuffer

func NewIoBufferBytes

func NewIoBufferBytes(bytes []byte) IoBuffer

func NewIoBufferEOF

func NewIoBufferEOF() IoBuffer

func NewIoBufferString

func NewIoBufferString(s string) IoBuffer

func NewPipeBuffer

func NewPipeBuffer(capacity int) IoBuffer

type IoBufferPool

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

IoBufferPool is Iobuffer Pool

func (*IoBufferPool) GetIoBuffer

func (p *IoBufferPool) GetIoBuffer(size int) IoBuffer

GetIoBuffer returns IoBuffer from pool

func (*IoBufferPool) PutIoBuffer

func (p *IoBufferPool) PutIoBuffer(buf IoBuffer) error

PutIoBuffer returns IoBuffer to pool

type TempBufferCtx added in v1.2.0

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

TempBufferCtx is template for BufferPoolCtx

func (*TempBufferCtx) Index added in v1.2.0

func (t *TempBufferCtx) Index() int

Index returns current index

func (*TempBufferCtx) New added in v1.2.0

func (t *TempBufferCtx) New() interface{}

New is a default action, which needs to implementation

func (*TempBufferCtx) Reset added in v1.2.0

func (t *TempBufferCtx) Reset(x interface{})

Reset is a default action, which needs to implementation

Jump to

Keyboard shortcuts

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