go.uber.org/yarpc@v1.72.1/internal/bufferpool/bufferpool.go (about)

     1  // Copyright (c) 2022 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  // Package bufferpool maintains a pool of bytes.Buffers for use in
    22  // encoding and transport implementations.
    23  package bufferpool
    24  
    25  import (
    26  	"flag"
    27  	"sync"
    28  )
    29  
    30  var _pool = NewPool()
    31  
    32  // Option configures a buffer pool.
    33  type Option func(*Pool)
    34  
    35  // Pool represents a buffer pool with a set of options.
    36  type Pool struct {
    37  	testDetectUseAfterFree bool
    38  	pool                   sync.Pool
    39  }
    40  
    41  func init() {
    42  	// This is a hacky way to determine whether we are running in unit tests where
    43  	// we want to enable use-after-free detection.
    44  	// https://stackoverflow.com/a/36666114
    45  	if flag.Lookup("test.v") != nil {
    46  		_pool = NewPool(DetectUseAfterFreeForTests())
    47  	}
    48  }
    49  
    50  // NewPool returns a pool that we can allocate buffers from.
    51  func NewPool(opts ...Option) *Pool {
    52  	pool := &Pool{}
    53  	for _, opt := range opts {
    54  		opt(pool)
    55  	}
    56  	return pool
    57  }
    58  
    59  // DetectUseAfterFreeForTests is an option that allows unit tests to detect
    60  // bad use of a pooled buffer after it has been released to the pool.
    61  func DetectUseAfterFreeForTests() Option {
    62  	return func(p *Pool) {
    63  		p.testDetectUseAfterFree = true
    64  	}
    65  }
    66  
    67  // Get returns a buffer from the pool.
    68  func (p *Pool) Get() *Buffer {
    69  	buf, ok := p.pool.Get().(*Buffer)
    70  	if !ok {
    71  		buf = newBuffer(p)
    72  	} else {
    73  		buf.reuse()
    74  	}
    75  	return buf
    76  }
    77  
    78  func (p *Pool) release(buf *Buffer) {
    79  	p.pool.Put(buf)
    80  }
    81  
    82  // Get returns a new Buffer from the Buffer pool that has been reset.
    83  func Get() *Buffer {
    84  	return _pool.Get()
    85  }
    86  
    87  // Put returns a Buffer to the Buffer pool.
    88  func Put(buf *Buffer) {
    89  	buf.Release()
    90  }