Skip to content

fix race condition on the actions' registry #301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 20, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions service/actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ package actions

import (
"context"
"sync"
"time"

"github.com/pkg/errors"
Expand All @@ -32,7 +33,15 @@ import (
"github.com/arangodb-helper/arangodb/pkg/definitions"
)

var actions map[string]Action
// Registry allows to register new actions.
type Registry struct {
// mutex protects the internal fields of this structure.
mutex sync.RWMutex
// actions holds already registered actions.
actions map[string]Action
}

var registry Registry

// ActionTypes is the list of ActionType
type ActionTypes []ActionType
Expand Down Expand Up @@ -98,11 +107,22 @@ func RegisterAction(action Action) {
return
}

if actions == nil {
actions = make(map[string]Action)
registry.mutex.Lock()
defer registry.mutex.Unlock()

if registry.actions == nil {
registry.actions = make(map[string]Action)
}

actions[action.Name()] = action
registry.actions[action.Name()] = action
}

// GetActions returns actions which are already registered.
func (r *Registry) GetActions() map[string]Action {
registry.mutex.RLock()
defer registry.mutex.RUnlock()

return registry.actions
}

// StartAction starts actions based on type if actionType is on the limit list
Expand Down Expand Up @@ -136,7 +156,7 @@ func StartPreStopActions(logger zerolog.Logger, serverType definitions.ServerTyp
}
}

for _, anyAction := range actions {
for _, anyAction := range registry.GetActions() {
preStopAction, ok := anyAction.(ActionPreStop)
if !ok {
continue
Expand Down