File tree Expand file tree Collapse file tree 1 file changed +18
-2
lines changed Expand file tree Collapse file tree 1 file changed +18
-2
lines changed Original file line number Diff line number Diff line change 1
1
package retries
2
2
3
3
import (
4
+ "encoding/json"
4
5
"net/http"
5
6
"strconv"
6
7
)
7
8
8
9
var callCounts = map [string ]int {}
9
10
11
+ type retriesResponse struct {
12
+ Retries int `json:"retries"`
13
+ }
14
+
10
15
func HandleRetries (w http.ResponseWriter , r * http.Request ) {
11
16
requestID := r .URL .Query ().Get ("request-id" )
12
17
numRetriesStr := r .URL .Query ().Get ("num-retries" )
@@ -35,11 +40,22 @@ func HandleRetries(w http.ResponseWriter, r *http.Request) {
35
40
callCounts [requestID ]++
36
41
37
42
if callCounts [requestID ] < numRetries {
38
- w .WriteHeader (http .StatusInternalServerError )
43
+ w .WriteHeader (http .StatusServiceUnavailable )
39
44
_ , _ = w .Write ([]byte ("request failed please retry" ))
40
45
return
41
46
}
42
47
43
- delete ( callCounts , requestID )
48
+ w . Header (). Set ( "Content-Type" , "application/json" )
44
49
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 )
45
61
}
You can’t perform that action at this time.
0 commit comments