Skip to content

Commit

Permalink
fix: switch to time cycle
Browse files Browse the repository at this point in the history
  • Loading branch information
J0 committed Mar 15, 2024
1 parent 697a569 commit 7283622
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
2 changes: 2 additions & 0 deletions internal/api/errorcodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,6 @@ const (
ErrorCodeOverSMSSendRateLimit ErrorCode = "over_sms_send_rate_limit"
ErrorBadCodeVerifier ErrorCode = "bad_code_verifier"
ErrorCodeAnonymousProviderDisabled ErrorCode = "anonymous_provider_disabled"
//TODO: Find a better name for this
ErrorHookGatewayTimeout ErrorCode = "hook_gateway_timeout"
)
4 changes: 4 additions & 0 deletions internal/api/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ func conflictError(fmtString string, args ...interface{}) *HTTPError {
return httpError(http.StatusConflict, ErrorCodeConflict, fmtString, args...)
}

func gatewayTimeoutError(errorCode ErrorCode, fmtString string, args ...interface{}) *HTTPError {
return httpError(http.StatusGatewayTimeout, errorCode, fmtString, args...)
}

// HTTPError is an error with a message and an HTTP status code.
type HTTPError struct {
HTTPStatus int `json:"code"` // do not rename the JSON tags!
Expand Down
19 changes: 9 additions & 10 deletions internal/api/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,12 @@ func (a *API) runHTTPHook(hookConfig conf.ExtensibilityPointConfiguration, input
if err != nil {
return nil, err
}

startTime := time.Now()

start := time.Now()
for i := 0; i < DefaultHTTPHookRetries; i++ {
hookLog.Infof("invocation attempt: %d", i)
if time.Since(start) > time.Duration(i)*DefaultHTTPHookTimeout {
return []byte{}, gatewayTimeoutError(ErrorHookGatewayTimeout, "failed to reach hook within timeout")
}
msgID := uuid.Must(uuid.NewV4())
currentTime := time.Now()
signatureList, err := crypto.GenerateSignatures(hookConfig.HTTPHookSecrets, msgID, currentTime, inputPayload)
Expand All @@ -114,7 +115,6 @@ func (a *API) runHTTPHook(hookConfig conf.ExtensibilityPointConfiguration, input
rsp, err := client.Do(req)

if err != nil {

if terr, ok := err.(net.Error); ok && terr.Timeout() {
hookLog.Errorf("Request timed out for attempt %d with err %s", i, err)
// TODO: workshop the sleep time value
Expand All @@ -125,7 +125,7 @@ func (a *API) runHTTPHook(hookConfig conf.ExtensibilityPointConfiguration, input
time.Sleep(DefaultHTTPHookTimeout)
continue
} else if i == DefaultHTTPHookRetries-1 {
return nil, httpError(http.StatusGatewayTimeout, "Failed to perform webhook in time frame (%v seconds)", time.Since(startTime).Seconds())
return nil, gatewayTimeoutError(ErrorHookGatewayTimeout, "failed to reach hook within allotted interval")

} else {
return nil, internalServerError("Failed to trigger auth hook, error making HTTP request").WithInternalError(err)
Expand All @@ -151,14 +151,13 @@ func (a *API) runHTTPHook(hookConfig conf.ExtensibilityPointConfiguration, input
if retryAfterHeader != "true" {
continue
}
// TODO: maybe return errors properly for all of these from response
return []byte{}, tooManyRequestsError("too many requests")
return []byte{}, internalServerError("Service unavailable")
case http.StatusBadRequest:
return nil, badRequestError("bad request error")
return nil, badRequestError(ErrorCodeValidationFailed, "Invalid Hook Payload")
case http.StatusUnauthorized:
return []byte{}, unauthorizedError("unauthorized")
return []byte{}, httpError(http.StatusUnauthorized, ErrorCodeNoAuthorization, "This endpoint requires a Bearer token")
default:
return []byte{}, internalServerError("error executing hooks")
return []byte{}, internalServerError("Error executing Hook")
}
}
return nil, internalServerError("error executing hook")
Expand Down

0 comments on commit 7283622

Please sign in to comment.