Skip to content

Commit 54bc96d

Browse files
authored
fix: Prevent concurrent map writes in retries endpoint (#19)
2 parents b37a5da + e9668eb commit 54bc96d

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

internal/retries/service.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ import (
44
"encoding/json"
55
"net/http"
66
"strconv"
7+
"sync"
78
)
89

9-
var callCounts = map[string]int{}
10+
var (
11+
callCounts = map[string]int{}
12+
callCountsMutex sync.Mutex
13+
)
1014

1115
type retriesResponse struct {
1216
Retries int `json:"retries"`
@@ -45,11 +49,13 @@ func HandleRetries(w http.ResponseWriter, r *http.Request) {
4549
return
4650
}
4751

52+
callCountsMutex.Lock()
4853
_, ok := callCounts[requestID]
4954
if !ok {
5055
callCounts[requestID] = 0
5156
}
5257
callCounts[requestID]++
58+
callCountsMutex.Unlock()
5359

5460
if callCounts[requestID] < numRetries {
5561
if retryAfter > 0 {
@@ -72,5 +78,7 @@ func HandleRetries(w http.ResponseWriter, r *http.Request) {
7278
}
7379
_, _ = w.Write(data)
7480

81+
callCountsMutex.Lock()
7582
delete(callCounts, requestID)
83+
callCountsMutex.Unlock()
7684
}

0 commit comments

Comments
 (0)