Skip to content

Commit

Permalink
feat: add resource_multiplier to function config
Browse files Browse the repository at this point in the history
  • Loading branch information
laktek committed Nov 13, 2024
1 parent bbb06f1 commit 4541f8b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
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
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"`
}

analytics struct {
Expand Down
5 changes: 3 additions & 2 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 @@ -55,7 +56,7 @@ func (s *EdgeRuntimeAPI) UpsertFunctions(ctx context.Context, functionConfig con
VerifyJwt: function.VerifyJWT,
ImportMapPath: toFileURL(function.ImportMap),
EntrypointPath: toFileURL(function.Entrypoint),
ResourceMultiplier: function.ResourceMultiplier,
ResourceMultiplier: &resourceMultiplier,
}, eszipContentType, bytes.NewReader(body.Bytes())); err != nil {
return errors.Errorf("failed to update function: %w", err)
} else if resp.JSON200 == nil {
Expand All @@ -68,7 +69,7 @@ func (s *EdgeRuntimeAPI) UpsertFunctions(ctx context.Context, functionConfig con
VerifyJwt: function.VerifyJWT,
ImportMapPath: toFileURL(function.ImportMap),
EntrypointPath: toFileURL(function.Entrypoint),
ResourceMultiplier: function.ResourceMultiplier,
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

0 comments on commit 4541f8b

Please sign in to comment.