Commit e3391e63 authored by AlexStocks's avatar AlexStocks

Add: example

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