github.com/TeaOSLab/EdgeNode@v1.3.8/internal/utils/buffer_pool.go (about) 1 // Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved. 2 3 package utils 4 5 import ( 6 "bytes" 7 "sync" 8 ) 9 10 var SharedBufferPool = NewBufferPool() 11 12 // BufferPool pool for get byte slice 13 type BufferPool struct { 14 rawPool *sync.Pool 15 } 16 17 // NewBufferPool 创建新对象 18 func NewBufferPool() *BufferPool { 19 var pool = &BufferPool{} 20 pool.rawPool = &sync.Pool{ 21 New: func() any { 22 return &bytes.Buffer{} 23 }, 24 } 25 return pool 26 } 27 28 // Get 获取一个新的Buffer 29 func (this *BufferPool) Get() (b *bytes.Buffer) { 30 var buffer = this.rawPool.Get().(*bytes.Buffer) 31 if buffer.Len() > 0 { 32 buffer.Reset() 33 } 34 return buffer 35 } 36 37 // Put 放回一个使用过的byte slice 38 func (this *BufferPool) Put(b *bytes.Buffer) { 39 this.rawPool.Put(b) 40 }