mirror of
https://github.com/ehang-io/nps.git
synced 2025-09-02 20:16:52 +00:00
mux test
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package pool
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"sync"
|
||||
)
|
||||
|
||||
@@ -36,6 +37,7 @@ var BufPoolCopy = sync.Pool{
|
||||
return &buf
|
||||
},
|
||||
}
|
||||
|
||||
func PutBufPoolUdp(buf []byte) {
|
||||
if cap(buf) == PoolSizeUdp {
|
||||
BufPoolUdp.Put(buf[:PoolSizeUdp])
|
||||
@@ -48,7 +50,7 @@ func PutBufPoolCopy(buf []byte) {
|
||||
}
|
||||
}
|
||||
|
||||
func GetBufPoolCopy() ([]byte) {
|
||||
func GetBufPoolCopy() []byte {
|
||||
return (*BufPoolCopy.Get().(*[]byte))[:PoolSizeCopy]
|
||||
}
|
||||
|
||||
@@ -57,3 +59,31 @@ func PutBufPoolMax(buf []byte) {
|
||||
BufPoolMax.Put(buf[:PoolSize])
|
||||
}
|
||||
}
|
||||
|
||||
type BufferPool struct {
|
||||
pool sync.Pool
|
||||
}
|
||||
|
||||
func (Self *BufferPool) New() {
|
||||
Self.pool = sync.Pool{
|
||||
New: func() interface{} {
|
||||
return new(bytes.Buffer)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (Self *BufferPool) Get() *bytes.Buffer {
|
||||
return Self.pool.Get().(*bytes.Buffer)
|
||||
}
|
||||
|
||||
func (Self *BufferPool) Put(x *bytes.Buffer) {
|
||||
x.Reset()
|
||||
Self.pool.Put(x)
|
||||
}
|
||||
|
||||
var once = sync.Once{}
|
||||
var BuffPool = BufferPool{}
|
||||
|
||||
func init() {
|
||||
once.Do(BuffPool.New)
|
||||
}
|
||||
|
Reference in New Issue
Block a user