Skip to content

Commit

Permalink
feat: ensure a MR is only processed once at a time; in case of many w…
Browse files Browse the repository at this point in the history
…ebhook events firing
  • Loading branch information
jippi committed Aug 29, 2024
1 parent 3ffb05b commit a889208
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func getClient(ctx context.Context) (scm.Client, error) {
}

func ProcessMR(ctx context.Context, client scm.Client, cfg *config.Config, event any) (err error) {
defer state.LockForProcessing(ctx)()

// Write the config to context so we can pull it out later
ctx = config.WithConfig(ctx, cfg)

Expand Down
18 changes: 18 additions & 0 deletions pkg/state/mutex.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package state

import (
"context"
"sync"
)

var processingMutex sync.Map // Zero value is empty and ready for use

func LockForProcessing(ctx context.Context) func() {
key := Provider(ctx) + "/" + ProjectID(ctx) + "/" + MergeRequestID(ctx)

value, _ := processingMutex.LoadOrStore(key, &sync.Mutex{})
mtx := value.(*sync.Mutex) //nolint
mtx.Lock()

return func() { mtx.Unlock() }
}

0 comments on commit a889208

Please sign in to comment.