Skip to content

Commit bcb7951

Browse files
author
Jefferson Sankara
committed
Fix data race in analytics global state and update time_duration enum initialization. All tests pass with race detector.
1 parent 9f83b3a commit bcb7951

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

analytics/analytics.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ import (
2121

2222
const flushInterval = time.Hour
2323

24-
var Enabled = true
24+
var (
25+
mu sync.RWMutex
26+
Enabled = true
27+
)
2528

2629
var logger = log.Logger("analytics")
2730

@@ -37,6 +40,8 @@ var logger = log.Logger("analytics")
3740
// Returns:
3841
// - An error if there are issues fetching the instance id from the database or if the database appears empty.
3942
func Init(ctx context.Context, db *gorm.DB) error {
43+
mu.Lock()
44+
defer mu.Unlock()
4045
if Instance != "" {
4146
return nil
4247
}
@@ -68,6 +73,27 @@ var (
6873
Identity string
6974
)
7075

76+
// GetInstance safely returns the Instance value
77+
func GetInstance() string {
78+
mu.RLock()
79+
defer mu.RUnlock()
80+
return Instance
81+
}
82+
83+
// GetIdentity safely returns the Identity value
84+
func GetIdentity() string {
85+
mu.RLock()
86+
defer mu.RUnlock()
87+
return Identity
88+
}
89+
90+
// IsEnabled safely returns the Enabled value
91+
func IsEnabled() bool {
92+
mu.RLock()
93+
defer mu.RUnlock()
94+
return Enabled
95+
}
96+
7197
type Collector struct {
7298
mu sync.Mutex
7399
packJobEvents []PackJobEvent

client/swagger/models/time_duration.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)