add mux slide window

This commit is contained in:
ffdfgdfg
2019-09-08 23:49:16 +08:00
parent 3413ceb7c2
commit 9d3df6be7e
6 changed files with 474 additions and 242 deletions

View File

@@ -9,6 +9,7 @@ const PoolSize = 64 * 1024
const PoolSizeSmall = 100
const PoolSizeUdp = 1472
const PoolSizeCopy = 32 << 10
const PoolSizeWindow = 1<<16 - 1
var BufPool = sync.Pool{
New: func() interface{} {
@@ -59,11 +60,11 @@ func PutBufPoolMax(buf []byte) {
}
}
type CopyBufferPool struct {
type copyBufferPool struct {
pool sync.Pool
}
func (Self *CopyBufferPool) New() {
func (Self *copyBufferPool) New() {
Self.pool = sync.Pool{
New: func() interface{} {
return make([]byte, PoolSizeCopy, PoolSizeCopy)
@@ -71,24 +72,49 @@ func (Self *CopyBufferPool) New() {
}
}
func (Self *CopyBufferPool) Get() []byte {
func (Self *copyBufferPool) Get() []byte {
buf := Self.pool.Get().([]byte)
return buf[:PoolSizeCopy] // just like make a new slice, but data may not be 0
}
func (Self *CopyBufferPool) Put(x []byte) {
func (Self *copyBufferPool) Put(x []byte) {
if len(x) == PoolSizeCopy {
Self.pool.Put(x)
} else {
x = nil // buf is not full, maybe truncated by gc in pool, not allowed
x = nil // buf is not full, not allowed, New method returns a full buf
}
}
type BufferPool struct {
type windowBufferPool struct {
pool sync.Pool
}
func (Self *BufferPool) New() {
func (Self *windowBufferPool) New() {
Self.pool = sync.Pool{
New: func() interface{} {
return make([]byte, 0, PoolSizeWindow)
},
}
}
func (Self *windowBufferPool) Get() (buf []byte) {
buf = Self.pool.Get().([]byte)
return buf[:0]
}
func (Self *windowBufferPool) Put(x []byte) {
if cap(x) == PoolSizeWindow {
Self.pool.Put(x[:PoolSizeWindow]) // make buf to full
} else {
x = nil
}
}
type bufferPool struct {
pool sync.Pool
}
func (Self *bufferPool) New() {
Self.pool = sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
@@ -96,20 +122,20 @@ func (Self *BufferPool) New() {
}
}
func (Self *BufferPool) Get() *bytes.Buffer {
func (Self *bufferPool) Get() *bytes.Buffer {
return Self.pool.Get().(*bytes.Buffer)
}
func (Self *BufferPool) Put(x *bytes.Buffer) {
func (Self *bufferPool) Put(x *bytes.Buffer) {
x.Reset()
Self.pool.Put(x)
}
type MuxPackagerPool struct {
type muxPackagerPool struct {
pool sync.Pool
}
func (Self *MuxPackagerPool) New() {
func (Self *muxPackagerPool) New() {
Self.pool = sync.Pool{
New: func() interface{} {
pack := MuxPackager{}
@@ -118,27 +144,29 @@ func (Self *MuxPackagerPool) New() {
}
}
func (Self *MuxPackagerPool) Get() *MuxPackager {
func (Self *muxPackagerPool) Get() *MuxPackager {
pack := Self.pool.Get().(*MuxPackager)
buf := CopyBuff.Get()
pack.Content = buf
return pack
}
func (Self *MuxPackagerPool) Put(pack *MuxPackager) {
func (Self *muxPackagerPool) Put(pack *MuxPackager) {
CopyBuff.Put(pack.Content)
Self.pool.Put(pack)
}
var once = sync.Once{}
var BuffPool = BufferPool{}
var CopyBuff = CopyBufferPool{}
var MuxPack = MuxPackagerPool{}
var BuffPool = bufferPool{}
var CopyBuff = copyBufferPool{}
var MuxPack = muxPackagerPool{}
var WindowBuff = windowBufferPool{}
func newPool() {
BuffPool.New()
CopyBuff.New()
MuxPack.New()
WindowBuff.New()
}
func init() {