diff --git a/binary/pool.go b/binary/pool.go index 895a3cd8..d0d7a244 100644 --- a/binary/pool.go +++ b/binary/pool.go @@ -95,3 +95,38 @@ func releaseZlibWriter(w *zlibWriter) { zlibPool.Put(w) } } + +const size128k = 128 * 1024 + +var b128kPool = sync.Pool{ + New: func() interface{} { + return make128kSlicePointer() + }, +} + +// Get128KBytes 获取一个128k大小 []byte +func Get128KBytes() *[]byte { + buf := b128kPool.Get().(*[]byte) + if buf == nil { + return make128kSlicePointer() + } + if cap(*buf) < size128k { + return make128kSlicePointer() + } + *buf = (*buf)[:size128k] + return buf +} + +// Put128KBytes 放回一个128k大小 []byte +func Put128KBytes(b *[]byte) { + if cap(*b) < size128k || cap(*b) > 2*size128k { // 太大或太小的 []byte 不要放入 + return + } + *b = (*b)[:cap(*b)] + b128kPool.Put(b) +} + +func make128kSlicePointer() *[]byte { + data := make([]byte, size128k) + return &data +} diff --git a/client/highway.go b/client/highway.go index 6ee3ef68..45f2ad7c 100644 --- a/client/highway.go +++ b/client/highway.go @@ -46,7 +46,10 @@ func (c *QQClient) highwayUploadStream(ip uint32, port int, updKey []byte, strea defer conn.Close() offset := 0 reader := binary.NewNetworkReader(conn) - chunk := make([]byte, chunkSize) + chunk := *binary.Get128KBytes() + defer func() { // 延迟捕获 chunk + binary.Put128KBytes(&chunk) + }() w := binary.NewWriter() defer binary.PutBuffer(w) for { @@ -128,7 +131,10 @@ func (c *QQClient) highwayUploadByBDH(stream io.Reader, length int64, cmdId int3 return nil, errors.Wrap(err, "echo error") } var rspExt []byte - chunk := make([]byte, chunkSize) + chunk := *binary.Get128KBytes() + defer func() { // 延迟捕获 chunk + binary.Put128KBytes(&chunk) + }() w := binary.NewWriter() defer binary.PutBuffer(w) for {