vitess.io/vitess@v0.16.2/go/vt/throttler/aggregated_interval_history.go (about) 1 /* 2 Copyright 2019 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package throttler 18 19 import ( 20 "time" 21 ) 22 23 // aggregatedIntervalHistory aggregates data across multiple "intervalHistory" 24 // instances. An instance should be mapped to a thread. 25 type aggregatedIntervalHistory struct { 26 threadCount int 27 historyPerThread []*intervalHistory 28 } 29 30 func newAggregatedIntervalHistory(capacity int64, interval time.Duration, threadCount int) *aggregatedIntervalHistory { 31 historyPerThread := make([]*intervalHistory, threadCount) 32 for i := 0; i < threadCount; i++ { 33 historyPerThread[i] = newIntervalHistory(capacity, interval) 34 } 35 return &aggregatedIntervalHistory{ 36 threadCount: threadCount, 37 historyPerThread: historyPerThread, 38 } 39 } 40 41 // addPerThread calls add() on the thread's intervalHistory instance. 42 func (h *aggregatedIntervalHistory) addPerThread(threadID int, record record) { 43 h.historyPerThread[threadID].add(record) 44 } 45 46 // average aggregates the average of all intervalHistory instances. 47 func (h *aggregatedIntervalHistory) average(from, to time.Time) float64 { 48 sum := 0.0 49 for i := 0; i < h.threadCount; i++ { 50 sum += h.historyPerThread[i].average(from, to) 51 } 52 return sum 53 }