Commit e3391e63 authored by AlexStocks's avatar AlexStocks

Add: example

parent 6d48f632
......@@ -30,13 +30,14 @@ type ContextKey int
// Context key types(built-in)
const (
ContextKeyStart ContextKey = iota
ContextKeyBufferPoolCtx
ContextKeyBufferPoolCtx ContextKey = iota
ContextKeyEnd
)
const maxBufferPool = 16
const (
maxBufferPool = 16
)
var (
index int32
......
......@@ -22,19 +22,19 @@ import (
"sync"
)
var ins = ByteBufferCtx{}
func init() {
RegisterBuffer(&ins)
}
const minShift = 6
const maxShift = 18
const errSlot = -1
const (
minShift = 6
maxShift = 18
errSlot = -1
)
var bbPool *byteBufferPool
var (
bbPool *byteBufferPool
ins = ByteBufferCtx{}
)
func init() {
RegisterBuffer(&ins)
bbPool = newByteBufferPool()
}
......
## copy from github.com/sofastack/sofa-mosn
## 自定义结构体复用
##### 请求维度的内存申请复用
* 模板
```
// ## copy from github.com/sofastack/sofa-mosn
// ## 自定义结构体复用
// ##### 请求维度的内存申请复用
package example
import (
......@@ -11,19 +9,23 @@ import (
)
import (
"github.com/dubbogo/gost/bytes"
gxbytes "github.com/divebomb/gost/bytes"
)
/////////////////////////////////
// 请求维度的内存申请复用
/////////////////////////////////
var ins exampleBufferCtx
// 注册buffer类型到内存复用框架
func init() {
buffer.RegisterBuffer(&ins)
gxbytes.RegisterBuffer(&ins)
}
// 需要包含 buffer.TempBufferCtx 到自定义的Ctx, 且要放到第一位
type exampleBufferCtx struct{
buffer.TempBufferCtx
// 需要包含 gxbytes.TempBufferCtx 到自定义的Ctx, 且要放到第一位
type exampleBufferCtx struct {
gxbytes.TempBufferCtx
}
// 实现New()函数, 用于生成自定义buffer
......@@ -46,49 +48,51 @@ type exampleBuffers struct {
// 通过ctx获取复用buffer
func exampleBuffersByContext(ctx context.Context) *exampleBuffers {
poolCtx := buffer.PoolContext(ctx)
poolCtx := gxbytes.PoolContext(ctx)
return poolCtx.Find(&ins, nil).(*exampleBuffers)
}
```
* 使用方式
```
// 使用方式
func run(ctx context.Context) {
// 通过ctx获取内存块
// 通过ctx获取内存块
buffer := exampleBuffersByContext(ctx)
// 通过指针使用
req := &buffer.req
rsp := &buffer.rsp
_, _ = req, rsp
}
```
## IoBuffer复用
```
/////////////////////////////////
// IoBuffer复用
/////////////////////////////////
// GetIoBuffer returns IoBuffer from pool
func GetIoBuffer(size int) Buffer {
return ibPool.take(size)
func GetIoBuffer(size int) gxbytes.Buffer {
//return ibPool.take(size)
return gxbytes.GetIoBuffer(size)
}
// PutIoBuffer returns IoBuffer to pool
func PutIoBuffer(buf Buffer) {
if buf.Count(-1) != 0 {
return
}
ibPool.give(buf)
func PutIoBuffer(buf gxbytes.Buffer) error {
//if buf.Count(-1) != 0 {
// return
//}
//ibPool.give(buf)
return gxbytes.PutIoBuffer(buf)
}
```
/////////////////////////////////
// Byte复用
/////////////////////////////////
## Byte复用
```
// GetBytes returns *[]byte from byteBufferPool
func GetBytes(size int) *[]byte {
p := getByteBufferPool()
return p.take(size)
return gxbytes.GetBytes(size)
}
// PutBytes Put *[]byte to byteBufferPool
func PutBytes(buf *[]byte) {
p := getByteBufferPool()
p.give(buf)
gxbytes.PutBytes(buf)
}
```
......@@ -26,12 +26,17 @@ import (
"sync/atomic"
)
const MinRead = 1 << 9
const MaxRead = 1 << 17
const ResetOffMark = -1
const DefaultSize = 1 << 4
const (
MinRead = 1 << 9
MaxRead = 1 << 17
ResetOffMark = -1
DefaultSize = 1 << 4
DefaultConnReadTimeout = 15 * time.Second
)
var nullByte []byte
var (
nullByte []byte
)
var (
EOF = errors.New("EOF")
......@@ -68,11 +73,6 @@ func (b *IoBuffer) Read(p []byte) (n int, err error) {
return
}
// Default connection arguments
const (
DefaultConnReadTimeout = 15 * time.Second
)
func (b *IoBuffer) ReadOnce(r io.Reader) (n int64, err error) {
var (
m int
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment