github.com/uber-go/tally/v4@v4.1.17/m3/resource_pool.go (about) 1 // Copyright (c) 2021 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 m3 22 23 import ( 24 tally "github.com/uber-go/tally/v4" 25 customtransport "github.com/uber-go/tally/v4/m3/customtransports" 26 m3thrift "github.com/uber-go/tally/v4/m3/thrift/v2" 27 "github.com/uber-go/tally/v4/thirdparty/github.com/apache/thrift/lib/go/thrift" 28 ) 29 30 const ( 31 batchPoolSize = 10 32 metricPoolSize = DefaultMaxQueueSize 33 protoPoolSize = 10 34 ) 35 36 type resourcePool struct { 37 metricSlicePool *tally.ObjectPool 38 metricTagSlicePool *tally.ObjectPool 39 protoPool *tally.ObjectPool 40 } 41 42 func newResourcePool(protoFac thrift.TProtocolFactory) *resourcePool { 43 metricSlicePool := tally.NewObjectPool(batchPoolSize) 44 metricSlicePool.Init(func() interface{} { 45 return make([]m3thrift.Metric, 0, batchPoolSize) 46 }) 47 48 metricTagSlicePool := tally.NewObjectPool(DefaultMaxQueueSize) 49 metricTagSlicePool.Init(func() interface{} { 50 return make([]m3thrift.MetricTag, 0, batchPoolSize) 51 }) 52 53 protoPool := tally.NewObjectPool(protoPoolSize) 54 protoPool.Init(func() interface{} { 55 return protoFac.GetProtocol(&customtransport.TCalcTransport{}) 56 }) 57 58 return &resourcePool{ 59 metricSlicePool: metricSlicePool, 60 metricTagSlicePool: metricTagSlicePool, 61 protoPool: protoPool, 62 } 63 } 64 65 func (r *resourcePool) getMetricSlice() []m3thrift.Metric { 66 return r.metricSlicePool.Get().([]m3thrift.Metric) 67 } 68 69 func (r *resourcePool) getMetricTagSlice() []m3thrift.MetricTag { 70 return r.metricTagSlicePool.Get().([]m3thrift.MetricTag) 71 } 72 73 func (r *resourcePool) getProto() thrift.TProtocol { 74 o := r.protoPool.Get() 75 return o.(thrift.TProtocol) 76 } 77 78 //nolint:unused 79 func (r *resourcePool) releaseProto(proto thrift.TProtocol) { 80 calc := proto.Transport().(*customtransport.TCalcTransport) 81 calc.ResetCount() 82 r.protoPool.Put(proto) 83 } 84 85 func (r *resourcePool) releaseMetricSlice(metrics []m3thrift.Metric) { 86 for i := 0; i < len(metrics); i++ { 87 metrics[i].Tags = nil 88 } 89 90 r.metricSlicePool.Put(metrics[:0]) 91 } 92 93 //nolint:unused 94 func (r *resourcePool) releaseMetricTagSlice(tags []m3thrift.MetricTag) { 95 r.metricSlicePool.Put(tags[:0]) 96 }