github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/x/sync/options.go (about) 1 // Copyright (c) 2018 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 sync 22 23 import ( 24 "runtime" 25 "time" 26 27 "github.com/m3db/m3/src/x/instrument" 28 ) 29 30 const ( 31 defaultKillWorkerProbability = 0.001 32 defaultGrowOnDemand = false 33 ) 34 35 var ( 36 defaultNumShards = int64(runtime.GOMAXPROCS(0)) 37 defaultNowFn = time.Now 38 ) 39 40 // NowFn is a function that returns the current time. 41 type NowFn func() time.Time 42 43 // NewPooledWorkerPoolOptions returns a new PooledWorkerPoolOptions with default options 44 func NewPooledWorkerPoolOptions() PooledWorkerPoolOptions { 45 return &pooledWorkerPoolOptions{ 46 growOnDemand: defaultGrowOnDemand, 47 numShards: defaultNumShards, 48 killWorkerProbability: defaultKillWorkerProbability, 49 nowFn: defaultNowFn, 50 iOpts: instrument.NewOptions(), 51 } 52 } 53 54 type pooledWorkerPoolOptions struct { 55 growOnDemand bool 56 numShards int64 57 killWorkerProbability float64 58 nowFn NowFn 59 iOpts instrument.Options 60 } 61 62 func (o *pooledWorkerPoolOptions) SetGrowOnDemand(value bool) PooledWorkerPoolOptions { 63 opts := *o 64 opts.growOnDemand = value 65 return &opts 66 } 67 68 func (o *pooledWorkerPoolOptions) GrowOnDemand() bool { 69 return o.growOnDemand 70 } 71 72 func (o *pooledWorkerPoolOptions) SetNumShards(value int64) PooledWorkerPoolOptions { 73 opts := *o 74 opts.numShards = value 75 return &opts 76 } 77 78 func (o *pooledWorkerPoolOptions) NumShards() int64 { 79 return o.numShards 80 } 81 82 func (o *pooledWorkerPoolOptions) SetKillWorkerProbability(value float64) PooledWorkerPoolOptions { 83 opts := *o 84 opts.killWorkerProbability = value 85 return &opts 86 } 87 88 func (o *pooledWorkerPoolOptions) KillWorkerProbability() float64 { 89 return o.killWorkerProbability 90 } 91 92 func (o *pooledWorkerPoolOptions) SetNowFn(value NowFn) PooledWorkerPoolOptions { 93 opts := *o 94 opts.nowFn = value 95 return &opts 96 } 97 98 func (o *pooledWorkerPoolOptions) NowFn() NowFn { 99 return o.nowFn 100 } 101 102 func (o *pooledWorkerPoolOptions) SetInstrumentOptions(value instrument.Options) PooledWorkerPoolOptions { 103 opts := *o 104 opts.iOpts = value 105 return &opts 106 } 107 108 func (o *pooledWorkerPoolOptions) InstrumentOptions() instrument.Options { 109 return o.iOpts 110 }