-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: clean up webhook template configuration and nest it under regul…
…ar webhook configuration, add tests
- Loading branch information
Showing
16 changed files
with
338 additions
and
198 deletions.
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
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
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
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
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
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
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
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package audit | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"io" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/cenkalti/backoff/v4" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type retryClient struct { | ||
logger *zap.Logger | ||
*http.Client | ||
maxBackoffDuration time.Duration | ||
} | ||
|
||
// Retrier contains a minimal API for retrying a request onto an endpoint upon failure or non-200 status. | ||
type Retrier interface { | ||
RequestRetry(ctx context.Context, body []byte, fn RequestCreator) error | ||
} | ||
|
||
// NewRetrier is a constructor for a retryClient, but returns a contract for the consumer | ||
// to just execute retryable HTTP requests. | ||
func NewRetrier(logger *zap.Logger, maxBackoffDuration time.Duration) Retrier { | ||
return &retryClient{ | ||
logger: logger, | ||
Client: &http.Client{ | ||
Timeout: 5 * time.Second, | ||
}, | ||
maxBackoffDuration: maxBackoffDuration, | ||
} | ||
} | ||
|
||
// RequestCreator provides a basic function for rewinding a request with a body, upon | ||
// request failure. | ||
type RequestCreator func(ctx context.Context, body []byte) (*http.Request, error) | ||
|
||
func (r *retryClient) RequestRetry(ctx context.Context, body []byte, fn RequestCreator) error { | ||
be := backoff.NewExponentialBackOff() | ||
be.MaxElapsedTime = r.maxBackoffDuration | ||
|
||
ticker := backoff.NewTicker(be) | ||
defer ticker.Stop() | ||
|
||
successfulRequest := false | ||
|
||
// Make requests with configured retries. | ||
for range ticker.C { | ||
req, err := fn(ctx, body) | ||
if err != nil { | ||
r.logger.Error("error creating HTTP request", zap.Error(err)) | ||
return err | ||
} | ||
|
||
resp, err := r.Client.Do(req) | ||
if err != nil { | ||
r.logger.Debug("webhook request failed, retrying...", zap.Error(err)) | ||
continue | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
r.logger.Debug("webhook request failed, retrying...", zap.Int("status_code", resp.StatusCode)) | ||
_, _ = io.Copy(io.Discard, resp.Body) | ||
_ = resp.Body.Close() | ||
continue | ||
} | ||
|
||
_, _ = io.Copy(io.Discard, resp.Body) | ||
_ = resp.Body.Close() | ||
|
||
r.logger.Debug("successful request to webhook") | ||
successfulRequest = true | ||
break | ||
} | ||
|
||
if !successfulRequest { | ||
return errors.New("failed to send event to webhook") | ||
} | ||
|
||
return nil | ||
} |
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package audit | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
"github.com/h2non/gock" | ||
"github.com/stretchr/testify/assert" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func TestRetrier_Failure(t *testing.T) { | ||
retrier := NewRetrier(zap.NewNop(), 5*time.Second) | ||
|
||
gock.New("https://respond.io"). | ||
MatchHeader("Content-Type", "application/json"). | ||
Post("/webhook"). | ||
Reply(500) | ||
defer gock.Off() | ||
|
||
rc := func(ctx context.Context, body []byte) (*http.Request, error) { | ||
return http.NewRequestWithContext(ctx, http.MethodPost, "https://respond.io/webhook", bytes.NewBuffer(body)) | ||
} | ||
|
||
err := retrier.RequestRetry(context.TODO(), []byte(`{"hello": "world"}`), rc) | ||
|
||
assert.EqualError(t, err, "failed to send event to webhook") | ||
} | ||
|
||
func TestRetrier_Success(t *testing.T) { | ||
retrier := NewRetrier(zap.NewNop(), 5*time.Second) | ||
|
||
gock.New("https://respond.io"). | ||
MatchHeader("Content-Type", "application/json"). | ||
Post("/webhook"). | ||
Reply(200) | ||
defer gock.Off() | ||
|
||
rc := func(ctx context.Context, body []byte) (*http.Request, error) { | ||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "https://respond.io/webhook", bytes.NewBuffer(body)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
req.Header.Add("Content-Type", "application/json") | ||
|
||
return req, nil | ||
} | ||
|
||
err := retrier.RequestRetry(context.TODO(), []byte(`{"hello": "world"}`), rc) | ||
|
||
assert.Nil(t, err) | ||
} |
Oops, something went wrong.