github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/x/pool/options.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 pool 22 23 import "github.com/m3db/m3/src/x/instrument" 24 25 const ( 26 _defaultSize = 4096 27 _defaultRefillLowWatermark = 0.0 28 _defaultRefillHighWatermark = 0.0 29 30 // DynamicPoolSize is a magic size value that will force use of sync.Pool-backed pool implementation. 31 DynamicPoolSize = -1 32 ) 33 34 type objectPoolOptions struct { 35 size int 36 refillLowWatermark float64 37 refillHighWatermark float64 38 instrumentOpts instrument.Options 39 onPoolAccessErrorFn OnPoolAccessErrorFn 40 dynamic bool 41 } 42 43 // NewObjectPoolOptions creates a new set of object pool options 44 func NewObjectPoolOptions() ObjectPoolOptions { 45 return &objectPoolOptions{ 46 size: _defaultSize, 47 dynamic: false, 48 refillLowWatermark: _defaultRefillLowWatermark, 49 refillHighWatermark: _defaultRefillHighWatermark, 50 instrumentOpts: instrument.NewOptions(), 51 onPoolAccessErrorFn: func(err error) { panic(err) }, 52 } 53 } 54 55 func (o *objectPoolOptions) SetSize(value int) ObjectPoolOptions { 56 opts := *o 57 opts.size = value 58 return &opts 59 } 60 61 func (o *objectPoolOptions) Size() int { 62 if o.dynamic { 63 return DynamicPoolSize 64 } 65 return o.size 66 } 67 68 func (o *objectPoolOptions) SetDynamic(dynamic bool) ObjectPoolOptions { 69 opts := *o 70 opts.dynamic = dynamic 71 return &opts 72 } 73 74 func (o *objectPoolOptions) Dynamic() bool { 75 // support enabling dynamic pools, even if a user does not expose pool options directly. 76 if o.size == DynamicPoolSize { 77 return true 78 } 79 return o.dynamic 80 } 81 82 func (o *objectPoolOptions) SetRefillLowWatermark(value float64) ObjectPoolOptions { 83 opts := *o 84 opts.refillLowWatermark = value 85 return &opts 86 } 87 88 func (o *objectPoolOptions) RefillLowWatermark() float64 { 89 return o.refillLowWatermark 90 } 91 92 func (o *objectPoolOptions) SetRefillHighWatermark(value float64) ObjectPoolOptions { 93 opts := *o 94 opts.refillHighWatermark = value 95 return &opts 96 } 97 98 func (o *objectPoolOptions) RefillHighWatermark() float64 { 99 return o.refillHighWatermark 100 } 101 102 func (o *objectPoolOptions) SetInstrumentOptions(value instrument.Options) ObjectPoolOptions { 103 opts := *o 104 opts.instrumentOpts = value 105 return &opts 106 } 107 108 func (o *objectPoolOptions) InstrumentOptions() instrument.Options { 109 return o.instrumentOpts 110 } 111 112 func (o *objectPoolOptions) SetOnPoolAccessErrorFn(value OnPoolAccessErrorFn) ObjectPoolOptions { 113 opts := *o 114 opts.onPoolAccessErrorFn = value 115 return &opts 116 } 117 118 func (o *objectPoolOptions) OnPoolAccessErrorFn() OnPoolAccessErrorFn { 119 return o.onPoolAccessErrorFn 120 }