Skip to content

Commit

Permalink
optimize maintaining of changed counters and gauges
Browse files Browse the repository at this point in the history
  • Loading branch information
shaan420 committed Oct 26, 2023
1 parent 7653526 commit 99dbd54
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@ type scope struct {

counterChangeNotifyCh chan *counter
gaugeChangeNotifyCh chan *gauge
changedCounters []*counter
changedGauges []*gauge
}

// ScopeOptions is a set of options to construct a scope.
Expand Down Expand Up @@ -190,8 +188,6 @@ func newRootScope(opts ScopeOptions, interval time.Duration) *scope {
root: true,
counterChangeNotifyCh: make(chan *counter, 1024),
gaugeChangeNotifyCh: make(chan *gauge, 1024),
changedCounters: make([]*counter, 0, _defaultInitialSliceSize),
changedGauges: make([]*gauge, 0, _defaultInitialSliceSize),
}

// NB(r): Take a copy of the tags on creation
Expand Down Expand Up @@ -292,18 +288,21 @@ func (s *scope) reportRegistry() {

func (s *scope) processLoop(interval time.Duration) {
ticker := time.NewTicker(interval)
counters := make([]*counter, 0, _defaultInitialSliceSize)
gauges := make([]*gauge, 0, _defaultInitialSliceSize)

defer ticker.Stop()
for {
select {
case c := <-s.counterChangeNotifyCh:
s.changedCounters = append(s.changedCounters, c)
counters = append(counters, c)
case g := <-s.gaugeChangeNotifyCh:
s.changedGauges = append(s.changedGauges, g)
gauges = append(gauges, g)
case <-ticker.C:
s.reportChanges(s.changedCounters, s.changedGauges)
s.reportChanges(counters, gauges)
// Reset the changed counters and gauges
s.changedCounters = s.changedCounters[:0]
s.changedGauges = s.changedGauges[:0]
counters = counters[:0]
gauges = gauges[:0]
default:
return
}
Expand Down

0 comments on commit 99dbd54

Please sign in to comment.