Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
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
b640a570
Commit
b640a570
authored
Sep 12, 2021
by
Xin.Zh
Committed by
AlexStocks
Sep 12, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
limit bytes pool object size (#70)
parent
dbf1d9e3
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
31 additions
and
2 deletions
+31
-2
.gitignore
.gitignore
+1
-0
bytes_buffer_pool.go
bytes/bytes_buffer_pool.go
+30
-2
No files found.
.gitignore
View file @
b640a570
...
...
@@ -16,6 +16,7 @@
*.idea
*.iml
*.vscode
target/
classes
...
...
bytes/bytes_buffer_pool.go
View file @
b640a570
...
...
@@ -22,22 +22,50 @@ import (
"sync"
)
var
defaultPool
*
ObjectPool
import
(
uatomic
"go.uber.org/atomic"
)
var
(
poolObjectNumber
uatomic
.
Int64
defaultPool
*
ObjectPool
minBufCap
=
512
maxBufCap
=
20
*
1024
maxPoolObjectNum
=
int64
(
4000
)
)
func
init
()
{
defaultPool
=
NewObjectPool
(
func
()
PoolObject
{
return
new
(
bytes
.
Buffer
)
})
poolObjectNumber
.
Store
(
0
)
}
// GetBytesBuffer returns bytes.Buffer from pool
func
GetBytesBuffer
()
*
bytes
.
Buffer
{
return
defaultPool
.
Get
()
.
(
*
bytes
.
Buffer
)
buf
:=
defaultPool
.
Get
()
.
(
*
bytes
.
Buffer
)
bufCap
:=
buf
.
Cap
()
if
bufCap
>=
minBufCap
&&
bufCap
<=
maxBufCap
&&
poolObjectNumber
.
Load
()
>
0
{
poolObjectNumber
.
Dec
()
}
return
buf
}
// PutIoBuffer returns IoBuffer to pool
func
PutBytesBuffer
(
buf
*
bytes
.
Buffer
)
{
if
poolObjectNumber
.
Load
()
>
maxPoolObjectNum
{
return
}
bufCap
:=
buf
.
Cap
()
if
bufCap
<
minBufCap
||
bufCap
>
maxBufCap
{
return
}
defaultPool
.
Put
(
buf
)
poolObjectNumber
.
Add
(
1
)
}
// Pool object
...
...
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