Skip to content

Commit a889208

Browse files
committed
feat: ensure a MR is only processed once at a time; in case of many webhook events firing
1 parent 3ffb05b commit a889208

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

cmd/shared.go

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ func getClient(ctx context.Context) (scm.Client, error) {
2828
}
2929

3030
func ProcessMR(ctx context.Context, client scm.Client, cfg *config.Config, event any) (err error) {
31+
defer state.LockForProcessing(ctx)()
32+
3133
// Write the config to context so we can pull it out later
3234
ctx = config.WithConfig(ctx, cfg)
3335

pkg/state/mutex.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package state
2+
3+
import (
4+
"context"
5+
"sync"
6+
)
7+
8+
var processingMutex sync.Map // Zero value is empty and ready for use
9+
10+
func LockForProcessing(ctx context.Context) func() {
11+
key := Provider(ctx) + "/" + ProjectID(ctx) + "/" + MergeRequestID(ctx)
12+
13+
value, _ := processingMutex.LoadOrStore(key, &sync.Mutex{})
14+
mtx := value.(*sync.Mutex) //nolint
15+
mtx.Lock()
16+
17+
return func() { mtx.Unlock() }
18+
}

0 commit comments

Comments
 (0)