Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
G
gostnops
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wei.xuan
gostnops
Commits
e3391e63
Commit
e3391e63
authored
Jun 29, 2019
by
AlexStocks
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add: example
parent
6d48f632
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
61 additions
and
56 deletions
+61
-56
buffer.go
bytes/buffer.go
+4
-3
bytebufferpool.go
bytes/bytebufferpool.go
+10
-10
example.go
bytes/examples/example.go
+37
-33
iobuffer.go
bytes/iobuffer.go
+10
-10
No files found.
bytes/buffer.go
View file @
e3391e63
...
...
@@ -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
...
...
bytes/bytebufferpool.go
View file @
e3391e63
...
...
@@ -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
()
}
...
...
bytes/
README.md
→
bytes/
examples/example.go
View file @
e3391e63
## 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
)
}
```
bytes/iobuffer.go
View file @
e3391e63
...
...
@@ -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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment