github.com/m3db/m3@v1.5.0/src/dbnode/network/server/tchannelthrift/pool.go (about)

     1  // Copyright (c) 2016 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 tchannelthrift
    22  
    23  import (
    24  	"github.com/m3db/m3/src/dbnode/generated/thrift/rpc"
    25  	"github.com/m3db/m3/src/x/pool"
    26  )
    27  
    28  var (
    29  	emptyBlockMetadataV2 rpc.BlockMetadataV2
    30  )
    31  
    32  // BlockMetadataV2Pool provides a pool for block metadata
    33  type BlockMetadataV2Pool interface {
    34  	// Get returns a block metadata
    35  	Get() *rpc.BlockMetadataV2
    36  
    37  	// Put puts a block metadata back to pool
    38  	Put(m *rpc.BlockMetadataV2)
    39  }
    40  
    41  // BlockMetadataV2SlicePool provides a pool for block metadata slices
    42  type BlockMetadataV2SlicePool interface {
    43  	// Get returns a block metadata slice
    44  	Get() []*rpc.BlockMetadataV2
    45  
    46  	// Put puts a block metadata slice back to pool
    47  	Put(m []*rpc.BlockMetadataV2)
    48  }
    49  
    50  type blockMetadataV2Pool struct {
    51  	pool pool.ObjectPool
    52  }
    53  
    54  // NewBlockMetadataV2Pool creates a new block metadata pool
    55  func NewBlockMetadataV2Pool(opts pool.ObjectPoolOptions) BlockMetadataV2Pool {
    56  	p := pool.NewObjectPool(opts)
    57  	p.Init(func() interface{} {
    58  		return rpc.NewBlockMetadataV2()
    59  	})
    60  	return &blockMetadataV2Pool{pool: p}
    61  }
    62  
    63  func (p *blockMetadataV2Pool) Get() *rpc.BlockMetadataV2 {
    64  	return p.pool.Get().(*rpc.BlockMetadataV2)
    65  }
    66  
    67  func (p *blockMetadataV2Pool) Put(metadata *rpc.BlockMetadataV2) {
    68  	if metadata != nil {
    69  		*metadata = emptyBlockMetadataV2
    70  		p.pool.Put(metadata)
    71  	}
    72  }
    73  
    74  type blockMetadataV2SlicePool struct {
    75  	pool     pool.ObjectPool
    76  	capacity int
    77  }
    78  
    79  // NewBlockMetadataV2SlicePool creates a new blockMetadataV2Slice pool
    80  func NewBlockMetadataV2SlicePool(opts pool.ObjectPoolOptions, capacity int) BlockMetadataV2SlicePool {
    81  	p := pool.NewObjectPool(opts)
    82  	p.Init(func() interface{} {
    83  		return make([]*rpc.BlockMetadataV2, 0, capacity)
    84  	})
    85  	return &blockMetadataV2SlicePool{pool: p, capacity: capacity}
    86  }
    87  
    88  func (p *blockMetadataV2SlicePool) Get() []*rpc.BlockMetadataV2 {
    89  	return p.pool.Get().([]*rpc.BlockMetadataV2)
    90  }
    91  
    92  func (p *blockMetadataV2SlicePool) Put(res []*rpc.BlockMetadataV2) {
    93  	if res == nil || cap(res) > p.capacity {
    94  		// Don't return nil or large slices back to pool
    95  		return
    96  	}
    97  	for i := range res {
    98  		res[i] = nil
    99  	}
   100  	res = res[:0]
   101  	p.pool.Put(res)
   102  }