github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/x/pool/config.go (about)

     1  // Copyright (c) 2017 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 pool
    22  
    23  import (
    24  	"errors"
    25  	"strconv"
    26  
    27  	"github.com/m3db/m3/src/x/instrument"
    28  )
    29  
    30  // ObjectPoolConfiguration contains configuration for object pools.
    31  type ObjectPoolConfiguration struct {
    32  	// The size of the pool.
    33  	Size Size `yaml:"size"`
    34  
    35  	// The watermark configuration.
    36  	Watermark WatermarkConfiguration `yaml:"watermark"`
    37  }
    38  
    39  // NewObjectPoolOptions creates a new set of object pool options.
    40  func (c *ObjectPoolConfiguration) NewObjectPoolOptions(
    41  	instrumentOpts instrument.Options,
    42  ) ObjectPoolOptions {
    43  	size := _defaultSize
    44  	if c.Size > 0 {
    45  		size = int(c.Size)
    46  	}
    47  
    48  	return NewObjectPoolOptions().
    49  		SetInstrumentOptions(instrumentOpts).
    50  		SetSize(size).
    51  		SetDynamic(c.Size.IsDynamic()).
    52  		SetRefillLowWatermark(c.Watermark.RefillLowWatermark).
    53  		SetRefillHighWatermark(c.Watermark.RefillHighWatermark)
    54  }
    55  
    56  // BucketizedPoolConfiguration contains configuration for bucketized pools.
    57  type BucketizedPoolConfiguration struct {
    58  	// The pool bucket configuration.
    59  	Buckets []BucketConfiguration `yaml:"buckets"`
    60  
    61  	// The watermark configuration.
    62  	Watermark WatermarkConfiguration `yaml:"watermark"`
    63  }
    64  
    65  // NewObjectPoolOptions creates a new set of object pool options.
    66  func (c *BucketizedPoolConfiguration) NewObjectPoolOptions(
    67  	instrumentOpts instrument.Options,
    68  ) ObjectPoolOptions {
    69  	return NewObjectPoolOptions().
    70  		SetInstrumentOptions(instrumentOpts).
    71  		SetRefillLowWatermark(c.Watermark.RefillLowWatermark).
    72  		SetRefillHighWatermark(c.Watermark.RefillHighWatermark)
    73  }
    74  
    75  // NewBuckets create a new list of buckets.
    76  func (c *BucketizedPoolConfiguration) NewBuckets() []Bucket {
    77  	buckets := make([]Bucket, 0, len(c.Buckets))
    78  	for _, bconfig := range c.Buckets {
    79  		bucket := bconfig.NewBucket()
    80  		buckets = append(buckets, bucket)
    81  	}
    82  	return buckets
    83  }
    84  
    85  // BucketConfiguration contains configuration for a pool bucket.
    86  type BucketConfiguration struct {
    87  	// The count of the items in the bucket.
    88  	Count Size `yaml:"count"`
    89  
    90  	// The capacity of each item in the bucket.
    91  	Capacity int `yaml:"capacity"`
    92  }
    93  
    94  // NewBucket creates a new bucket.
    95  func (c *BucketConfiguration) NewBucket() Bucket {
    96  	return Bucket{
    97  		Capacity: c.Capacity,
    98  		Count:    c.Count,
    99  	}
   100  }
   101  
   102  // WatermarkConfiguration contains watermark configuration for pools.
   103  type WatermarkConfiguration struct {
   104  	// The low watermark to start refilling the pool, if zero none.
   105  	RefillLowWatermark float64 `yaml:"low" validate:"min=0.0,max=1.0"`
   106  
   107  	// The high watermark to stop refilling the pool, if zero none.
   108  	RefillHighWatermark float64 `yaml:"high" validate:"min=0.0,max=1.0"`
   109  }
   110  
   111  // Size stores pool capacity for pools that can be either dynamic or pre-allocated
   112  type Size int
   113  
   114  // UnmarshalText unmarshals Size.
   115  func (s *Size) UnmarshalText(b []byte) error {
   116  	if string(b) == "dynamic" {
   117  		*s = DynamicPoolSize
   118  		return nil
   119  	}
   120  
   121  	i, err := strconv.ParseInt(string(b), 10, 64)
   122  	if err != nil {
   123  		return err
   124  	}
   125  
   126  	if i < 0 {
   127  		return errors.New("pool capacity cannot be negative")
   128  	}
   129  
   130  	*s = Size(i)
   131  
   132  	return nil
   133  }
   134  
   135  // IsDynamic returns whether the pool should be fixed size or not.
   136  func (s Size) IsDynamic() bool {
   137  	return s == DynamicPoolSize
   138  }