github.com/dubbogo/gost@v1.14.0/bytes/bytes_buffer_pool.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package gxbytes
    19  
    20  import (
    21  	"bytes"
    22  	"sync"
    23  )
    24  
    25  import (
    26  	uatomic "go.uber.org/atomic"
    27  )
    28  
    29  var (
    30  	poolObjectNumber uatomic.Int64
    31  	defaultPool      *ObjectPool
    32  
    33  	minBufCap        = 512
    34  	maxBufCap        = 20 * 1024
    35  	maxPoolObjectNum = int64(4000)
    36  )
    37  
    38  func init() {
    39  	defaultPool = NewObjectPool(func() PoolObject {
    40  		return new(bytes.Buffer)
    41  	})
    42  	poolObjectNumber.Store(0)
    43  }
    44  
    45  // GetBytesBuffer returns bytes.Buffer from pool
    46  func GetBytesBuffer() *bytes.Buffer {
    47  	buf := defaultPool.Get().(*bytes.Buffer)
    48  	bufCap := buf.Cap()
    49  	if bufCap >= minBufCap && bufCap <= maxBufCap && poolObjectNumber.Load() > 0 {
    50  		poolObjectNumber.Dec()
    51  	}
    52  
    53  	return buf
    54  }
    55  
    56  // PutIoBuffer returns IoBuffer to pool
    57  func PutBytesBuffer(buf *bytes.Buffer) {
    58  	if poolObjectNumber.Load() > maxPoolObjectNum {
    59  		return
    60  	}
    61  
    62  	bufCap := buf.Cap()
    63  	if bufCap < minBufCap || bufCap > maxBufCap {
    64  		return
    65  	}
    66  
    67  	defaultPool.Put(buf)
    68  	poolObjectNumber.Add(1)
    69  }
    70  
    71  // Pool object
    72  type PoolObject interface {
    73  	Reset()
    74  }
    75  
    76  type New func() PoolObject
    77  
    78  // Pool is bytes.Buffer Pool
    79  type ObjectPool struct {
    80  	New  New
    81  	pool sync.Pool
    82  }
    83  
    84  func NewObjectPool(n New) *ObjectPool {
    85  	return &ObjectPool{New: n}
    86  }
    87  
    88  // take returns *bytes.Buffer from Pool
    89  func (p *ObjectPool) Get() PoolObject {
    90  	v := p.pool.Get()
    91  	if v == nil {
    92  		return p.New()
    93  	}
    94  
    95  	return v.(PoolObject)
    96  }
    97  
    98  // give returns *byes.Buffer to Pool
    99  func (p *ObjectPool) Put(o PoolObject) {
   100  	o.Reset()
   101  	p.pool.Put(o)
   102  }