This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
feat: Added option for configuring number of retries for http event sender, as well as additional logging #465
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
22872b7
feat: Added option for configuring number of retries for http event s…
bacherfl 98a550e
added option for injecting retry callback
bacherfl d574fe0
added option for injecting retry callback
bacherfl da769aa
remove retry callback option
bacherfl ddf2121
remove retry callback option unit test
bacherfl 6e1f0d5
added comment
bacherfl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,16 +41,27 @@ const keptnSpecVersionCEExtension = "shkeptnspecversion" | |
const triggeredIDCEExtension = "triggeredid" | ||
const keptnGitCommitIDCEExtension = "gitcommitid" | ||
|
||
type HTTPSenderOption func(httpSender *HTTPEventSender) | ||
|
||
// WithSendRetries allows to specify the number of retries that are performed if the receiver of an event returns a HTTP error code | ||
func WithSendRetries(retries int) HTTPSenderOption { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [golint] reported by reviewdog 🐶 |
||
return func(httpSender *HTTPEventSender) { | ||
httpSender.nrRetries = retries | ||
} | ||
} | ||
|
||
// HTTPEventSender sends CloudEvents via HTTP | ||
type HTTPEventSender struct { | ||
// EventsEndpoint is the http endpoint the events are sent to | ||
EventsEndpoint string | ||
// Client is an implementation of the cloudevents.Client interface | ||
Client cloudevents.Client | ||
// nrRetries is the number of retries that are attempted if the endpoint an event is forwarded to returns an http code outside the 2xx range | ||
nrRetries int | ||
} | ||
|
||
// NewHTTPEventSender creates a new HTTPSender | ||
func NewHTTPEventSender(endpoint string) (*HTTPEventSender, error) { | ||
func NewHTTPEventSender(endpoint string, opts ...HTTPSenderOption) (*HTTPEventSender, error) { | ||
if endpoint == "" { | ||
endpoint = DefaultHTTPEventEndpoint | ||
} | ||
|
@@ -75,6 +86,11 @@ func NewHTTPEventSender(endpoint string) (*HTTPEventSender, error) { | |
httpSender := &HTTPEventSender{ | ||
EventsEndpoint: endpoint, | ||
Client: c, | ||
nrRetries: MAX_SEND_RETRIES, | ||
} | ||
|
||
for _, o := range opts { | ||
o(httpSender) | ||
} | ||
return httpSender, nil | ||
} | ||
|
@@ -88,7 +104,7 @@ func (httpSender HTTPEventSender) Send(ctx context.Context, event cloudevents.Ev | |
ctx = cloudevents.ContextWithTarget(ctx, httpSender.EventsEndpoint) | ||
ctx = cloudevents.WithEncodingStructured(ctx) | ||
var result protocol.Result | ||
for i := 0; i <= MAX_SEND_RETRIES; i++ { | ||
for i := 0; i <= httpSender.nrRetries; i++ { | ||
result = httpSender.Client.Send(ctx, event) | ||
httpResult, ok := result.(*httpprotocol.Result) | ||
switch { | ||
|
@@ -103,7 +119,7 @@ func (httpSender HTTPEventSender) Send(ctx context.Context, event cloudevents.Ev | |
return nil | ||
} | ||
} | ||
return errors.New("Failed to send cloudevent: " + result.Error()) | ||
return fmt.Errorf("could not send cloudevent after %d retries. Received result from the receiver: %w", httpSender.nrRetries, result) | ||
} | ||
|
||
// EventSender fakes the sending of CloudEvents | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[golint] reported by reviewdog 🐶
exported type HTTPSenderOption should have comment or be unexported