Skip to content

Commit 19b67c5

Browse files
committed
chore: fix casing, make some varaibles clearer
1 parent 2b95c7f commit 19b67c5

1 file changed

Lines changed: 46 additions & 48 deletions

File tree

metrics.go

Lines changed: 46 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,18 @@ type toggleCounters struct {
9898

9999
type metrics struct {
100100
metricsChannels
101-
options metricsOptions
102-
started time.Time
103-
last_close_time time.Time
104-
counters sync.Map // map[string]*toggleCounters
105-
ticker *time.Ticker
106-
close chan struct{}
107-
closed chan struct{}
108-
ctx context.Context
109-
cancel func()
110-
maxSkips float64
111-
errors float64
112-
skips float64
101+
options metricsOptions
102+
started time.Time
103+
lastCloseTime time.Time
104+
counters sync.Map // map[string]*toggleCounters
105+
ticker *time.Ticker
106+
close chan struct{}
107+
closed chan struct{}
108+
ctx context.Context
109+
cancel func()
110+
maxSkips float64
111+
errors float64
112+
skips float64
113113
}
114114

115115
func newMetrics(options metricsOptions, channels metricsChannels) *metrics {
@@ -122,7 +122,7 @@ func newMetrics(options metricsOptions, channels metricsChannels) *metrics {
122122
maxSkips: 10,
123123
errors: 0,
124124
skips: 0,
125-
last_close_time: time.Now(),
125+
lastCloseTime: time.Now(),
126126
}
127127
ctx, cancel := context.WithCancel(context.Background())
128128
m.ctx = ctx
@@ -210,49 +210,49 @@ func (m *metrics) decrementSkip() {
210210
// The consequence is that if the user archives a lot of toggles this internal representation will not lose those
211211
// toggles until the process is terminated. In practice, I don't believe this is a big problem, just means a
212212
// little bit more memory is held than necessary
213-
func (m *metrics) buildBucketAndReset(last_close_time time.Time) (api.Bucket, bool) {
213+
func (m *metrics) buildBucketAndReset(lastCloseTime time.Time) (api.Bucket, bool) {
214214
bucket := api.Bucket{
215-
Start: last_close_time,
215+
Start: lastCloseTime,
216216
Toggles: make(map[string]api.ToggleCount),
217217
}
218218

219219
m.counters.Range(func(key, value any) bool {
220220
name := key.(string)
221-
c := value.(*toggleCounters)
221+
counter := value.(*toggleCounters)
222222

223-
yes := atomic.SwapInt64(&c.yes, 0)
224-
no := atomic.SwapInt64(&c.no, 0)
223+
yes := atomic.SwapInt64(&counter.yes, 0)
224+
no := atomic.SwapInt64(&counter.no, 0)
225225

226226
if yes == 0 && no == 0 {
227-
c.mu.Lock()
228-
emptyVariants := len(c.variants) == 0
229-
c.mu.Unlock()
227+
counter.mu.Lock()
228+
emptyVariants := len(counter.variants) == 0
229+
counter.mu.Unlock()
230230
if emptyVariants {
231231
return true
232232
}
233233
}
234234

235-
tc := api.ToggleCount{
235+
toggleCounters := api.ToggleCount{
236236
Yes: int32(yes),
237237
No: int32(no),
238238
}
239239

240240
// we can have a little locking, as a treat. Variants are likely a luke warm path at best
241241
// until we have evidence that this is a hot path API, I'd like to keep this simple
242242
// simple here means a local lock per toggle counter while we swap out the variants map
243-
c.mu.Lock()
244-
if len(c.variants) > 0 {
245-
vars := make(map[string]int32, len(c.variants))
246-
for vName, cnt := range c.variants {
243+
counter.mu.Lock()
244+
if len(counter.variants) > 0 {
245+
vars := make(map[string]int32, len(counter.variants))
246+
for vName, cnt := range counter.variants {
247247
vars[vName] = int32(cnt)
248248
}
249-
tc.Variants = vars
249+
toggleCounters.Variants = vars
250250

251-
c.variants = make(map[string]int64)
251+
counter.variants = make(map[string]int64)
252252
}
253-
c.mu.Unlock()
253+
counter.mu.Unlock()
254254

255-
bucket.Toggles[name] = tc
255+
bucket.Toggles[name] = toggleCounters
256256
return true
257257
})
258258

@@ -264,11 +264,11 @@ func (m *metrics) buildBucketAndReset(last_close_time time.Time) (api.Bucket, bo
264264
}
265265

266266
func (m *metrics) sendMetrics() {
267-
bucket, ok := m.buildBucketAndReset(m.last_close_time)
267+
bucket, ok := m.buildBucketAndReset(m.lastCloseTime)
268268
if !ok {
269269
return
270270
}
271-
m.last_close_time = time.Now()
271+
m.lastCloseTime = time.Now()
272272
bucket.Stop = time.Now()
273273
payload := MetricsData{
274274
AppName: m.options.appName,
@@ -303,7 +303,7 @@ func (m *metrics) sendMetrics() {
303303

304304
// Set the start time of the current bucket to the one we
305305
// attempted to send.
306-
m.last_close_time = bucket.Start
306+
m.lastCloseTime = bucket.Start
307307

308308
} else {
309309
m.successfulPost()
@@ -350,29 +350,27 @@ func (m *metrics) getOrCreateCounter(name string) *toggleCounters {
350350
}
351351

352352
func (m *metrics) reinsertBucket(bucket api.Bucket) {
353-
for name, tc := range bucket.Toggles {
354-
c := m.getOrCreateCounter(name)
355-
if tc.Yes != 0 {
356-
atomic.AddInt64(&c.yes, int64(tc.Yes))
353+
for name, bucketToggle := range bucket.Toggles {
354+
counter := m.getOrCreateCounter(name)
355+
if bucketToggle.Yes != 0 {
356+
atomic.AddInt64(&counter.yes, int64(bucketToggle.Yes))
357357
}
358-
if tc.No != 0 {
359-
atomic.AddInt64(&c.no, int64(tc.No))
358+
if bucketToggle.No != 0 {
359+
atomic.AddInt64(&counter.no, int64(bucketToggle.No))
360360
}
361361

362-
if len(tc.Variants) > 0 {
363-
c := m.getOrCreateCounter(name)
364-
365-
c.mu.Lock()
366-
if c.variants == nil {
367-
c.variants = make(map[string]int64, len(tc.Variants))
362+
if len(bucketToggle.Variants) > 0 {
363+
counter.mu.Lock()
364+
if counter.variants == nil {
365+
counter.variants = make(map[string]int64, len(bucketToggle.Variants))
368366
}
369-
for vName, cnt := range tc.Variants {
367+
for vName, cnt := range bucketToggle.Variants {
370368
if cnt == 0 {
371369
continue
372370
}
373-
c.variants[vName] += int64(cnt)
371+
counter.variants[vName] += int64(cnt)
374372
}
375-
c.mu.Unlock()
373+
counter.mu.Unlock()
376374
}
377375
}
378376
}

0 commit comments

Comments
 (0)