Skip to content
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

Add resource multiplier #2877

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
16 changes: 16 additions & 0 deletions api/beta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2012,6 +2012,11 @@ paths:
in: query
schema:
type: string
- name: resource_multiplier
required: false
in: query
schema:
type: string
requestBody:
required: true
content:
Expand Down Expand Up @@ -2153,6 +2158,11 @@ paths:
in: query
schema:
type: string
- name: resource_multiplier
required: false
in: query
schema:
type: string
requestBody:
required: true
content:
Expand Down Expand Up @@ -5090,6 +5100,8 @@ components:
type: string
verify_jwt:
type: boolean
resource_multiplier:
type: string
required:
- slug
- name
Expand Down Expand Up @@ -5125,6 +5137,8 @@ components:
type: string
import_map_path:
type: string
resource_multiplier:
type: string
required:
- version
- created_at
Expand Down Expand Up @@ -5164,6 +5178,8 @@ components:
type: string
import_map_path:
type: string
resource_multiplier:
type: string
required:
- version
- created_at
Expand Down
9 changes: 6 additions & 3 deletions internal/functions/serve/templates/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ Deno.serve({
console.error(`serving the request with ${servicePath}`);

// Ref: https://supabase.com/docs/guides/functions/limits
const memoryLimitMb = 256;
const resourceMultiplier = Math.max(Math.min(parseFloat(functionsConfig[functionName].resourceMultiplier ?? '1'), 4), 1)
const memoryLimitMb = 256 * resourceMultiplier;
const workerTimeoutMs = isFinite(WALLCLOCK_LIMIT_SEC) ? WALLCLOCK_LIMIT_SEC * 1000 : 400 * 1000;
const noModuleCache = false;
const envVarsObj = Deno.env.toObject();
Expand All @@ -161,7 +162,7 @@ Deno.serve({
const forceCreate = false;
const customModuleRoot = ""; // empty string to allow any local path
const cpuTimeSoftLimitMs = 1000;
const cpuTimeHardLimitMs = 2000;
const cpuTimeHardLimitMs = 2000 * resourceMultiplier;

// NOTE(Nyannyacha): Decorator type has been set to tc39 by Lakshan's request,
// but in my opinion, we should probably expose this to customers at some
Expand All @@ -187,7 +188,9 @@ Deno.serve({
maybeEntrypoint
});

return await worker.fetch(req);
const res = await worker.fetch(req);
res.headers.set('x-sb-resource-multiplier', resourceMultiplier)
return res
} catch (e) {
console.error(e);

Expand Down
32 changes: 32 additions & 0 deletions pkg/api/client.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 43 additions & 38 deletions pkg/api/types.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,11 @@ type (
FunctionConfig map[string]function

function struct {
Enabled *bool `toml:"enabled" json:"-"`
VerifyJWT *bool `toml:"verify_jwt" json:"verifyJWT"`
ImportMap string `toml:"import_map" json:"importMapPath,omitempty"`
Entrypoint string `toml:"entrypoint" json:"entrypointPath,omitempty"`
Enabled *bool `toml:"enabled" json:"-"`
VerifyJWT *bool `toml:"verify_jwt" json:"verifyJWT"`
ImportMap string `toml:"import_map" json:"importMapPath,omitempty"`
Entrypoint string `toml:"entrypoint" json:"entrypointPath,omitempty"`
ResourceMultiplier *string `toml:"resource_multiplier" json:"resourceMultiplier,omitempty"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

analytics struct {
Expand Down
19 changes: 11 additions & 8 deletions pkg/function/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func (s *EdgeRuntimeAPI) UpsertFunctions(ctx context.Context, functionConfig con
continue
}
}
resourceMultiplier := function.ResourceMultiplier
var body bytes.Buffer
if err := s.eszip.Bundle(ctx, function.Entrypoint, function.ImportMap, &body); err != nil {
return err
Expand All @@ -52,21 +53,23 @@ func (s *EdgeRuntimeAPI) UpsertFunctions(ctx context.Context, functionConfig con
upsert := func() error {
if _, ok := exists[slug]; ok {
if resp, err := s.client.V1UpdateAFunctionWithBodyWithResponse(ctx, s.project, slug, &api.V1UpdateAFunctionParams{
VerifyJwt: function.VerifyJWT,
ImportMapPath: toFileURL(function.ImportMap),
EntrypointPath: toFileURL(function.Entrypoint),
VerifyJwt: function.VerifyJWT,
ImportMapPath: toFileURL(function.ImportMap),
EntrypointPath: toFileURL(function.Entrypoint),
ResourceMultiplier: resourceMultiplier,
}, eszipContentType, bytes.NewReader(body.Bytes())); err != nil {
return errors.Errorf("failed to update function: %w", err)
} else if resp.JSON200 == nil {
return errors.Errorf("unexpected status %d: %s", resp.StatusCode(), string(resp.Body))
}
} else {
if resp, err := s.client.V1CreateAFunctionWithBodyWithResponse(ctx, s.project, &api.V1CreateAFunctionParams{
Slug: &slug,
Name: &slug,
VerifyJwt: function.VerifyJWT,
ImportMapPath: toFileURL(function.ImportMap),
EntrypointPath: toFileURL(function.Entrypoint),
Slug: &slug,
Name: &slug,
VerifyJwt: function.VerifyJWT,
ImportMapPath: toFileURL(function.ImportMap),
EntrypointPath: toFileURL(function.Entrypoint),
ResourceMultiplier: resourceMultiplier,
}, eszipContentType, bytes.NewReader(body.Bytes())); err != nil {
return errors.Errorf("failed to create function: %w", err)
} else if resp.JSON201 == nil {
Expand Down