Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid retrying rate limited events #96

Merged
merged 1 commit into from
Apr 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions runtime/events/recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ limitations under the License.
package events

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
"os"
"time"
Expand Down Expand Up @@ -49,6 +51,7 @@ func NewRecorder(webhook, reportingController string) (*Recorder, error) {

httpClient := retryablehttp.NewClient()
httpClient.HTTPClient.Timeout = 5 * time.Second
httpClient.CheckRetry = retryablehttp.ErrorPropagatedRetryPolicy
httpClient.Logger = nil

return &Recorder{
Expand Down Expand Up @@ -120,6 +123,12 @@ func (r *Recorder) Eventf(
return fmt.Errorf("failed to marshal object into json, error: %w", err)
}

// avoid retrying rate limited requests
if res, _ := r.Client.HTTPClient.Post(r.Webhook, "application/json", bytes.NewReader(body)); res != nil &&
(res.StatusCode == http.StatusTooManyRequests || res.StatusCode == http.StatusAccepted) {
return nil
}

if _, err := r.Client.Post(r.Webhook, "application/json", body); err != nil {
return err
}
Expand Down
30 changes: 28 additions & 2 deletions runtime/events/recorder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package events

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -86,5 +85,32 @@ func TestEventRecorder_Eventf_Retry(t *testing.T) {
}

err = eventRecorder.EventErrorf(obj, nil, "sync", "sync %s", obj.Name)
require.EqualError(t, err, fmt.Sprintf("POST %s giving up after 3 attempt(s)", ts.URL))
require.Error(t, err)
}

func TestEventRecorder_Eventf_RateLimited(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
require.NoError(t, err)

var payload Event
err = json.Unmarshal(b, &payload)
require.NoError(t, err)

w.WriteHeader(http.StatusTooManyRequests)
}))
defer ts.Close()

eventRecorder, err := NewRecorder(ts.URL, "test-controller")
require.NoError(t, err)
eventRecorder.Client.RetryMax = 2

obj := corev1.ObjectReference{
Kind: "GitRepository",
Namespace: "gitops-system",
Name: "webapp",
}

err = eventRecorder.EventInfof(obj, nil, "sync", "sync %s", obj.Name)
require.NoError(t, err)
}