Skip to content

Commit d9710f9

Browse files
authored
using sync.Map instead of mutex + Map for concurrency lock (#319)
1 parent 3fe51d2 commit d9710f9

File tree

1 file changed

+6
-15
lines changed

1 file changed

+6
-15
lines changed

pkg/ingestor/api/api.go

+6-15
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ type IngestorAPI struct {
3939
Cfg *config.KubehoundConfig
4040
providers *providers.ProvidersFactoryConfig
4141

42-
mu *sync.RWMutex // mutex to sync write to the runIDs map
43-
runIDs map[string]bool // runIDs map to monitor and avoid concurrency processing on the same runID
42+
runIDs sync.Map // runIDs map to monitor and avoid concurrency processing on the same runID
4443
}
4544

4645
var (
@@ -51,16 +50,12 @@ var (
5150

5251
func NewIngestorAPI(cfg *config.KubehoundConfig, puller puller.DataPuller, notifier notifier.Notifier,
5352
p *providers.ProvidersFactoryConfig) *IngestorAPI {
54-
var mu sync.RWMutex
55-
var runIDs = make(map[string]bool)
56-
5753
return &IngestorAPI{
5854
notifier: notifier,
5955
puller: puller,
6056
Cfg: cfg,
6157
providers: p,
62-
mu: &mu,
63-
runIDs: runIDs,
58+
runIDs: sync.Map{},
6459
}
6560
}
6661

@@ -306,24 +301,20 @@ func (g *IngestorAPI) Notify(ctx context.Context, clusterName string, runID stri
306301
// Using a map to monitor all runIDs being processed,
307302
// Using a mutex to write/read data to the runIDs map
308303
func (g *IngestorAPI) lockRunID(runID string) error {
309-
g.mu.Lock()
310-
defer g.mu.Unlock()
311-
entry, ok := g.runIDs[runID]
304+
_, ok := g.runIDs.Load(runID)
312305

313306
// If a runID is being processed, dropping the request
314-
if ok && entry {
307+
if ok {
315308
return fmt.Errorf("%w [runID:%s]", ErrCurrentlyIngesting, runID)
316309
}
317310

318311
// Locking the current runID
319-
g.runIDs[runID] = true
312+
g.runIDs.Store(runID, true)
320313

321314
return nil
322315
}
323316

324317
// Delocking the runID
325318
func (g *IngestorAPI) unlockRunID(runID string) {
326-
g.mu.Lock()
327-
g.runIDs[runID] = false
328-
g.mu.Unlock()
319+
g.runIDs.Delete(runID)
329320
}

0 commit comments

Comments
 (0)