buffer

package
v0.0.0-...-78a648b Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2020 License: Apache-2.0 Imports: 10 Imported by: 0

README

自定义结构体复用

请求维度的内存申请复用
  • 模板
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
}

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)
}

Documentation

Index

Constants

View Source
const DefaultSize = 1 << 4
View Source
const MaxRead = 1 << 17
View Source
const MinRead = 1 << 9
View Source
const ResetOffMark = -1

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      = types.DefaultConnReadTimeout
)

Functions

func GetBytes

func GetBytes(size int) *[]byte

GetBytes returns *[]byte from byteBufferPool

func GetBytesByContext

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

GetBytesByContext returns []byte from byteBufferPool by context

func GetIoBuffer

func GetIoBuffer(size int) types.IoBuffer

GetIoBuffer returns IoBuffer from pool

func NewBufferPoolContext

func NewBufferPoolContext(ctx context.Context) context.Context

NewBufferPoolContext returns a context with bufferValue

func NewIoBuffer

func NewIoBuffer(capacity int) types.IoBuffer

func NewIoBufferBytes

func NewIoBufferBytes(bytes []byte) types.IoBuffer

func NewIoBufferEOF

func NewIoBufferEOF() types.IoBuffer

func NewIoBufferString

func NewIoBufferString(s string) types.IoBuffer

func PoolContext

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 types.IoBuffer) error

PutIoBuffer returns IoBuffer to pool

func RegisterBuffer

func RegisterBuffer(poolCtx types.BufferPoolCtx)

func TransmitBufferPoolContext

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

TransmitBufferPoolContext copy a context

Types

type ByteBufferCtx

type ByteBufferCtx struct {
	TempBufferCtx
}

func (ByteBufferCtx) New

func (ctx ByteBufferCtx) New() interface{}

func (ByteBufferCtx) Reset

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

type ByteBufferPoolContainer

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

type IoBuffer

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

IoBuffer

func (*IoBuffer) Alloc

func (b *IoBuffer) Alloc(size int)

func (*IoBuffer) Append

func (b *IoBuffer) Append(data []byte) error

func (*IoBuffer) AppendByte

func (b *IoBuffer) AppendByte(data byte) error

func (*IoBuffer) Bytes

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

func (*IoBuffer) Cap

func (b *IoBuffer) Cap() int

func (*IoBuffer) Clone

func (b *IoBuffer) Clone() types.IoBuffer

func (*IoBuffer) Count

func (b *IoBuffer) Count(count int32) int32

func (*IoBuffer) Cut

func (b *IoBuffer) Cut(offset int) types.IoBuffer

func (*IoBuffer) Drain

func (b *IoBuffer) Drain(offset int)

func (*IoBuffer) EOF

func (b *IoBuffer) EOF() bool

func (*IoBuffer) Free

func (b *IoBuffer) Free()

func (*IoBuffer) Len

func (b *IoBuffer) Len() int

func (*IoBuffer) Mark

func (b *IoBuffer) Mark()

func (*IoBuffer) Peek

func (b *IoBuffer) Peek(n int) []byte

func (*IoBuffer) Read

func (b *IoBuffer) Read(p []byte) (n int, err error)

func (*IoBuffer) ReadFrom

func (b *IoBuffer) ReadFrom(r io.Reader) (n int64, err error)

func (*IoBuffer) ReadOnce

func (b *IoBuffer) ReadOnce(r io.Reader) (n int64, e error)

func (*IoBuffer) Reset

func (b *IoBuffer) Reset()

func (*IoBuffer) Restore

func (b *IoBuffer) Restore()

func (*IoBuffer) SetEOF

func (b *IoBuffer) SetEOF(eof bool)

func (*IoBuffer) String

func (b *IoBuffer) String() string

func (*IoBuffer) Write

func (b *IoBuffer) Write(p []byte) (n int, err error)

func (*IoBuffer) WriteString

func (b *IoBuffer) WriteString(s string) (n int, err error)

func (*IoBuffer) WriteTo

func (b *IoBuffer) WriteTo(w io.Writer) (n int64, err error)

type IoBufferPool

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

IoBufferPool is Iobuffer Pool

type TempBufferCtx

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

TempBufferCtx is template for types.BufferPoolCtx

func (*TempBufferCtx) Index

func (t *TempBufferCtx) Index() int

func (*TempBufferCtx) New

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

func (*TempBufferCtx) Reset

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

Jump to

Keyboard shortcuts

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