Skip to content

Commit e683cdd

Browse files
fix: add response to retries command
1 parent 4be3f7a commit e683cdd

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

internal/retries/service.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package retries
22

33
import (
4+
"encoding/json"
45
"net/http"
56
"strconv"
67
)
78

89
var callCounts = map[string]int{}
910

11+
type retriesResponse struct {
12+
Retries int `json:"retries"`
13+
}
14+
1015
func HandleRetries(w http.ResponseWriter, r *http.Request) {
1116
requestID := r.URL.Query().Get("request-id")
1217
numRetriesStr := r.URL.Query().Get("num-retries")
@@ -35,11 +40,22 @@ func HandleRetries(w http.ResponseWriter, r *http.Request) {
3540
callCounts[requestID]++
3641

3742
if callCounts[requestID] < numRetries {
38-
w.WriteHeader(http.StatusInternalServerError)
43+
w.WriteHeader(http.StatusServiceUnavailable)
3944
_, _ = w.Write([]byte("request failed please retry"))
4045
return
4146
}
4247

43-
delete(callCounts, requestID)
48+
w.Header().Set("Content-Type", "application/json")
4449
w.WriteHeader(http.StatusOK)
50+
data, err := json.Marshal(retriesResponse{
51+
Retries: callCounts[requestID],
52+
})
53+
if err != nil {
54+
w.WriteHeader(http.StatusInternalServerError)
55+
_, _ = w.Write([]byte("failed to marshal response"))
56+
return
57+
}
58+
_, _ = w.Write(data)
59+
60+
delete(callCounts, requestID)
4561
}

0 commit comments

Comments
 (0)