Skip to content

Commit

Permalink
fix: Eliminate data race in modules integration
Browse files Browse the repository at this point in the history
Use local struct fields instead of globals.
Ensure modules cache is filled only once and safe for concurrent use
using sync.Once.

Fixes #104
  • Loading branch information
rhcarvalho committed Dec 5, 2019
1 parent 54d5222 commit 78e3836
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions integrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import (
// Modules Integration
// ================================

type modulesIntegration struct{}

var _modulesCache map[string]string // nolint: gochecknoglobals
var _modulesCached bool // nolint: gochecknoglobals
type modulesIntegration struct {
once sync.Once
modules map[string]string
}

func (mi *modulesIntegration) Name() string {
return "Modules"
Expand All @@ -31,26 +31,20 @@ func (mi *modulesIntegration) SetupOnce(client *Client) {

func (mi *modulesIntegration) processor(event *Event, hint *EventHint) *Event {
if len(event.Modules) == 0 {
event.Modules = extractModules()
mi.once.Do(func() {
mi.modules = extractModules()
})
}

event.Modules = mi.modules
return event
}

func extractModules() map[string]string {
if _modulesCached {
return _modulesCache
}

_modulesCached = true
extractedModules, err := getModules()
if err != nil {
Logger.Printf("ModuleIntegration wasn't able to extract modules: %v\n", err)
return nil
}

_modulesCache = extractedModules

return extractedModules
}

Expand Down

0 comments on commit 78e3836

Please sign in to comment.