v2ray-core/common/buf/buffer_pool.go

133 lines
2.5 KiB
Go
Raw Normal View History

2016-12-09 10:35:27 +00:00
package buf
2016-04-12 14:52:57 +00:00
import (
2016-08-25 09:21:32 +00:00
"os"
"runtime"
2016-08-25 09:21:32 +00:00
"strconv"
2016-04-12 14:52:57 +00:00
"sync"
)
2016-12-06 16:26:51 +00:00
// Pool provides functionality to generate and recycle buffers on demand.
2016-07-28 14:24:15 +00:00
type Pool interface {
2016-12-06 16:36:28 +00:00
// Allocate either returns a unused buffer from the pool, or generates a new one from system.
2016-07-28 14:24:15 +00:00
Allocate() *Buffer
2016-12-06 16:36:28 +00:00
// Free recycles the given buffer.
2016-07-28 14:24:15 +00:00
Free(*Buffer)
}
2016-12-06 16:36:28 +00:00
// SyncPool is a buffer pool based on sync.Pool
2016-11-21 21:08:34 +00:00
type SyncPool struct {
allocator *sync.Pool
}
2016-12-06 16:36:28 +00:00
// NewSyncPool creates a SyncPool with given buffer size.
2016-11-21 21:08:34 +00:00
func NewSyncPool(bufferSize uint32) *SyncPool {
pool := &SyncPool{
allocator: &sync.Pool{
New: func() interface{} { return make([]byte, bufferSize) },
},
}
return pool
}
2016-12-06 16:36:28 +00:00
// Allocate implements Pool.Allocate().
2016-11-21 21:08:34 +00:00
func (p *SyncPool) Allocate() *Buffer {
2016-12-11 08:43:20 +00:00
return &Buffer{
v: p.allocator.Get().([]byte),
pool: p,
}
2016-11-21 21:08:34 +00:00
}
2016-12-06 16:36:28 +00:00
// Free implements Pool.Free().
2016-11-21 21:08:34 +00:00
func (p *SyncPool) Free(buffer *Buffer) {
2016-12-06 12:42:12 +00:00
rawBuffer := buffer.v
2016-11-21 21:08:34 +00:00
if rawBuffer == nil {
return
}
p.allocator.Put(rawBuffer)
}
2016-12-11 08:43:20 +00:00
// BufferPool is a Pool that utilizes an internal cache.
2016-04-12 14:52:57 +00:00
type BufferPool struct {
chain chan []byte
allocator *sync.Pool
}
2016-12-11 08:43:20 +00:00
// NewBufferPool creates a new BufferPool with given buffer size, and internal cache size.
2016-08-25 09:21:32 +00:00
func NewBufferPool(bufferSize, poolSize uint32) *BufferPool {
2016-04-12 14:52:57 +00:00
pool := &BufferPool{
chain: make(chan []byte, poolSize),
allocator: &sync.Pool{
New: func() interface{} { return make([]byte, bufferSize) },
},
}
2016-08-25 09:21:32 +00:00
for i := uint32(0); i < poolSize; i++ {
2016-04-12 14:52:57 +00:00
pool.chain <- make([]byte, bufferSize)
}
return pool
}
2016-12-11 08:43:20 +00:00
// Allocate implements Pool.Allocate().
2016-04-12 14:52:57 +00:00
func (p *BufferPool) Allocate() *Buffer {
var b []byte
select {
case b = <-p.chain:
default:
b = p.allocator.Get().([]byte)
}
2016-12-11 08:43:20 +00:00
return &Buffer{
v: b,
pool: p,
}
2016-04-12 14:52:57 +00:00
}
2016-12-11 08:43:20 +00:00
// Free implements Pool.Free().
2016-04-12 14:52:57 +00:00
func (p *BufferPool) Free(buffer *Buffer) {
2016-12-06 12:42:12 +00:00
rawBuffer := buffer.v
2016-04-12 14:52:57 +00:00
if rawBuffer == nil {
return
}
select {
case p.chain <- rawBuffer:
default:
p.allocator.Put(rawBuffer)
}
}
2016-05-11 17:54:20 +00:00
const (
2016-12-11 08:43:20 +00:00
// Size of a regular buffer.
2017-04-15 19:19:21 +00:00
Size = 2 * 1024
2016-08-25 09:21:32 +00:00
2016-12-21 14:37:16 +00:00
poolSizeEnvKey = "v2ray.buffer.size"
2016-05-11 17:54:20 +00:00
)
2016-08-25 09:21:32 +00:00
var (
2016-12-04 23:48:41 +00:00
mediumPool Pool
2016-08-25 09:21:32 +00:00
)
func getDefaultPoolSize() uint32 {
switch runtime.GOARCH {
case "amd64", "386":
return 20
default:
return 5
}
}
2016-08-25 09:21:32 +00:00
func init() {
2017-02-20 09:33:35 +00:00
size := getDefaultPoolSize()
2016-12-21 14:37:16 +00:00
sizeStr := os.Getenv(poolSizeEnvKey)
2016-08-25 09:21:32 +00:00
if len(sizeStr) > 0 {
customSize, err := strconv.ParseUint(sizeStr, 10, 32)
if err == nil {
size = uint32(customSize)
}
}
2016-12-04 23:48:41 +00:00
if size > 0 {
totalByteSize := size * 1024 * 1024
2016-12-09 11:08:25 +00:00
mediumPool = NewBufferPool(Size, totalByteSize/Size)
2016-12-04 23:48:41 +00:00
} else {
2016-12-09 11:08:25 +00:00
mediumPool = NewSyncPool(Size)
2016-12-04 23:48:41 +00:00
}
2016-08-25 09:21:32 +00:00
}