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