Skip to content

Commit

Permalink
fix: adjust naming
Browse files Browse the repository at this point in the history
  • Loading branch information
joel authored and joel committed Mar 13, 2024
1 parent ad827d6 commit 6aa36dd
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions internal/api/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ import (
"github.com/supabase/auth/internal/storage"
)

const SymmetricSignaturePrefix = "v1,"
const (
SymmetricSignaturePrefix = "v1,"
DefaultHTTPHookTimeout = 5 * time.Second
DefaultHTTPHookRetries = 3
)

func (a *API) runPostgresHook(ctx context.Context, tx *storage.Connection, name string, input, output any) ([]byte, error) {
db := a.db.WithContext(ctx)
Expand Down Expand Up @@ -71,17 +75,16 @@ func (a *API) runPostgresHook(ctx context.Context, tx *storage.Connection, name
}

func (a *API) runHTTPHook(hookConfig conf.ExtensibilityPointConfiguration, input, output any) ([]byte, error) {
defaultTimeout := 5 * time.Second

client := http.Client{
Timeout: defaultTimeout,
Timeout: DefaultHTTPHookTimeout,
}
requestURL := hookConfig.URI
hookLog := logrus.WithFields(logrus.Fields{
"component": "auth_hook",
"url": requestURL,
})

defaultHookRetries := 3
inputPayload, err := json.Marshal(input)
if err != nil {
return nil, err
Expand All @@ -90,8 +93,9 @@ func (a *API) runHTTPHook(hookConfig conf.ExtensibilityPointConfiguration, input
if isOverSizeLimit(inputPayload) {
return nil, internalServerError("Over size limit")
}
startTime := time.Now()

for i := 0; i < defaultHookRetries; i++ {
for i := 0; i < DefaultHTTPHookRetries; i++ {
hookLog.Infof("invocation attempt: %d", i)
msgID := uuid.Must(uuid.NewV4())
currentTime := time.Now()
Expand All @@ -113,15 +117,21 @@ func (a *API) runHTTPHook(hookConfig conf.ExtensibilityPointConfiguration, input

watcher, req := watchForConnection(req)
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)
time.Sleep(defaultTimeout)
// TODO: workshop the sleep time value
time.Sleep(DefaultHTTPHookTimeout)
continue
} else if !watcher.gotConn && i < defaultHookRetries-1 {
} else if !watcher.gotConn && i < DefaultHTTPHookRetries-1 {
hookLog.Errorf("Failed to establish a connection on attempt %d with err %s", i, err)
time.Sleep(defaultTimeout)
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())

} else {
return nil, internalServerError("Failed to trigger auth hook, error making HTTP request").WithInternalError(err)
}
Expand All @@ -138,15 +148,15 @@ func (a *API) runHTTPHook(hookConfig conf.ExtensibilityPointConfiguration, input
return nil, err
}
if isOverSizeLimit(body) {
return nil, internalServerError("too large")
return nil, internalServerError("payload too large")
}
return body, nil
case http.StatusTooManyRequests, http.StatusServiceUnavailable:
// TODO (joel): Accept more liberal naming?
retryAfterHeader := rsp.Header.Get("retry-after")
if retryAfterHeader != "" {
if retryAfterHeader != "true" {
continue
}
// TODO: maybe return errors properly for all of these from response
return []byte{}, tooManyRequestsError("too many requests")
case http.StatusBadRequest:
return nil, badRequestError("bad request error")
Expand All @@ -156,8 +166,7 @@ func (a *API) runHTTPHook(hookConfig conf.ExtensibilityPointConfiguration, input
return []byte{}, internalServerError("error executing hooks")
}
}
// TODO (joel): properly check for third attempt
return nil, internalServerError("error ex")
return nil, internalServerError("error executing hook")
}

func generateSignatures(secrets []string, msgID uuid.UUID, currentTime time.Time, inputPayload []byte) ([]string, error) {
Expand Down

0 comments on commit 6aa36dd

Please sign in to comment.