Skip to content

Commit 1c98b85

Browse files
committed
Runtime, Recorder: Use httpClient.CheckRetry to handle http code 429 Too Many Requests.
The notification controller sends a 429 Too Many Requests message when a message is duplicated. Thus if we receive a 429, we should discard the message.
1 parent 86263bb commit 1c98b85

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

runtime/events/recorder.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ limitations under the License.
1717
package events
1818

1919
import (
20-
"bytes"
20+
"context"
2121
"encoding/json"
2222
"errors"
2323
"fmt"
@@ -93,7 +93,7 @@ func NewRecorder(mgr ctrl.Manager, log logr.Logger, webhook, reportingController
9393

9494
httpClient := retryablehttp.NewClient()
9595
httpClient.HTTPClient.Timeout = 5 * time.Second
96-
httpClient.CheckRetry = retryablehttp.ErrorPropagatedRetryPolicy
96+
httpClient.CheckRetry = checkRetry
9797
httpClient.Logger = nil
9898

9999
return &Recorder{
@@ -120,7 +120,7 @@ func NewRecorderForScheme(scheme *runtime.Scheme,
120120

121121
httpClient := retryablehttp.NewClient()
122122
httpClient.HTTPClient.Timeout = 5 * time.Second
123-
httpClient.CheckRetry = retryablehttp.ErrorPropagatedRetryPolicy
123+
httpClient.CheckRetry = checkRetry
124124
httpClient.Logger = nil
125125

126126
return &Recorder{
@@ -133,6 +133,19 @@ func NewRecorderForScheme(scheme *runtime.Scheme,
133133
}, nil
134134
}
135135

136+
func checkRetry(ctx context.Context, resp *http.Response, err error) (bool, error) {
137+
if resp != nil && responseIsMessageIsDuplicated(resp) {
138+
return false, nil // Don't retry
139+
}
140+
return retryablehttp.ErrorPropagatedRetryPolicy(ctx, resp, err)
141+
}
142+
143+
// The Notification Controller returns a 429 Too Many Requests response
144+
// when the posted message is a duplicate (within a certain time window).
145+
func responseIsMessageIsDuplicated(resp *http.Response) bool {
146+
return resp.StatusCode == http.StatusTooManyRequests
147+
}
148+
136149
// Event records an event in the webhook address.
137150
func (r *Recorder) Event(object runtime.Object, eventtype, reason, message string) {
138151
r.AnnotatedEventf(object, nil, eventtype, reason, "%s", message)
@@ -250,12 +263,6 @@ func (r *Recorder) AnnotatedEventf(
250263
return
251264
}
252265

253-
// avoid retrying rate limited requests
254-
if res, _ := r.Client.HTTPClient.Post(r.Webhook, "application/json", bytes.NewReader(body)); res != nil &&
255-
(res.StatusCode == http.StatusTooManyRequests || res.StatusCode == http.StatusAccepted) {
256-
return
257-
}
258-
259266
if _, err := r.Client.Post(r.Webhook, "application/json", body); err != nil {
260267
log.Error(err, "unable to record event")
261268
return

0 commit comments

Comments
 (0)