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

RetryConfig.RequestTimeout to configure the timeout of a single request #5503

Merged
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
12 changes: 11 additions & 1 deletion pkg/kncloudevents/message_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,18 @@ func (s *HTTPMessageSender) SendWithRetries(req *nethttp.Request, config *RetryC
return s.Send(req)
}

client := s.Client
if config.RequestTimeout != 0 {
client = &nethttp.Client{
Transport: client.Transport,
CheckRedirect: client.CheckRedirect,
Jar: client.Jar,
Timeout: config.RequestTimeout,
}
}

retryableClient := retryablehttp.Client{
HTTPClient: s.Client,
HTTPClient: client,
RetryWaitMin: defaultRetryWaitMin,
RetryWaitMax: defaultRetryWaitMax,
RetryMax: config.RetryMax,
Expand Down
40 changes: 40 additions & 0 deletions pkg/kncloudevents/message_sender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,46 @@ func TestHTTPMessageSenderSendWithRetriesWithBufferedMessage(t *testing.T) {
}
}

func TestHTTPMessageSenderSendWithRetriesWithSingleRequestTimeout(t *testing.T) {
t.Parallel()

timeout := time.Second * 3

var n int32
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
newVal := atomic.AddInt32(&n, 1)
if newVal >= 5 {
writer.WriteHeader(http.StatusOK)
} else {
// Let's add one more second
time.Sleep(timeout)
writer.WriteHeader(http.StatusAccepted)
}
}))
defer server.Close()

sender := &HTTPMessageSender{
Client: getClient(),
}
config := &RetryConfig{
RetryMax: 5,
CheckRetry: RetryIfGreaterThan300,
Backoff: func(attemptNum int, resp *http.Response) time.Duration {
return time.Millisecond
},
RequestTimeout: timeout,
}

request, err := http.NewRequest("POST", server.URL, nil)
require.NoError(t, err)

got, err := sender.SendWithRetries(request, config)

require.Equal(t, 5, int(atomic.LoadInt32(&n)))
require.NoError(t, err)
require.Equal(t, http.StatusOK, got.StatusCode)
}

func TestRetriesOnNetworkErrors(t *testing.T) {

n := int32(10)
Expand Down
3 changes: 3 additions & 0 deletions pkg/kncloudevents/retries.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ type RetryConfig struct {

CheckRetry CheckRetry
Backoff Backoff

// RequestTimeout represents the timeout of the single request
RequestTimeout time.Duration
}

func NoRetries() RetryConfig {
Expand Down