buffer

package
v0.24.0 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2021 License: Apache-2.0 Imports: 7 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

This section is empty.

Variables

This section is empty.

Functions

func GetBytesByContext

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

GetBytesByContext returns []byte from byteBufferPool by context

func NewBufferPoolContext

func NewBufferPoolContext(ctx context.Context) context.Context

NewBufferPoolContext returns a context with bufferValue

func PoolContext

func PoolContext(ctx context.Context) *bufferValue

PoolContext returns bufferValue by context

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 TempBufferCtx

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

TempBufferCtx is template for 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