Skip to content

fix: retry on error listing functions #3490

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

Merged
merged 2 commits into from
Apr 27, 2025
Merged
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
26 changes: 18 additions & 8 deletions pkg/function/batch.go
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ import (
"context"
"fmt"
"io"
"net/http"
"os"
"strings"
"time"
@@ -22,19 +23,28 @@ const (
)

func (s *EdgeRuntimeAPI) UpsertFunctions(ctx context.Context, functionConfig config.FunctionConfig, filter ...func(string) bool) error {
var result []api.FunctionResponse
if resp, err := s.client.V1ListAllFunctionsWithResponse(ctx, s.project); err != nil {
return errors.Errorf("failed to list functions: %w", err)
} else if resp.JSON200 == nil {
return errors.Errorf("unexpected list functions status %d: %s", resp.StatusCode(), string(resp.Body))
} else {
result = *resp.JSON200
policy := backoff.WithContext(backoff.WithMaxRetries(backoff.NewExponentialBackOff(), maxRetries), ctx)
result, err := backoff.RetryWithData(func() ([]api.FunctionResponse, error) {
resp, err := s.client.V1ListAllFunctionsWithResponse(ctx, s.project)
if err != nil {
return nil, errors.Errorf("failed to list functions: %w", err)
} else if resp.JSON200 == nil {
err = errors.Errorf("unexpected list functions status %d: %s", resp.StatusCode(), string(resp.Body))
if resp.StatusCode() < http.StatusInternalServerError {
err = &backoff.PermanentError{Err: err}
}
return nil, err
}
return *resp.JSON200, nil
}, policy)
if err != nil {
return err
}
policy.Reset()
exists := make(map[string]struct{}, len(result))
for _, f := range result {
exists[f.Slug] = struct{}{}
}
policy := backoff.WithContext(backoff.WithMaxRetries(backoff.NewExponentialBackOff(), maxRetries), ctx)
var toUpdate []api.BulkUpdateFunctionBody
OUTER:
for slug, function := range functionConfig {
18 changes: 7 additions & 11 deletions pkg/function/batch_test.go
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@ import (
"github.com/h2non/gock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/supabase/cli/internal/testing/apitest"
"github.com/supabase/cli/pkg/api"
"github.com/supabase/cli/pkg/config"
)
@@ -41,28 +42,23 @@ func TestUpsertFunctions(t *testing.T) {
era.eszip = &MockBundler{}
})

t.Run("throws error on network failure", func(t *testing.T) {
t.Run("retries on network failure", func(t *testing.T) {
// Setup mock api
defer gock.OffAll()
gock.New(mockApiHost).
Get("/v1/projects/" + mockProject + "/functions").
ReplyError(errors.New("network error"))
// Run test
err := client.UpsertFunctions(context.Background(), nil)
// Check error
assert.ErrorContains(t, err, "network error")
})

t.Run("throws error on service unavailable", func(t *testing.T) {
// Setup mock api
defer gock.OffAll()
gock.New(mockApiHost).
Get("/v1/projects/" + mockProject + "/functions").
Reply(http.StatusServiceUnavailable)
gock.New(mockApiHost).
Get("/v1/projects/" + mockProject + "/functions").
Reply(http.StatusBadRequest)
// Run test
err := client.UpsertFunctions(context.Background(), nil)
// Check error
assert.ErrorContains(t, err, "unexpected list functions status 503:")
assert.ErrorContains(t, err, "unexpected list functions status 400:")
assert.Empty(t, apitest.ListUnmatchedRequests())
})

t.Run("retries on create failure", func(t *testing.T) {