Skip to content

Commit

Permalink
Auto-downgrade implicit concurrency
Browse files Browse the repository at this point in the history
  • Loading branch information
inlined committed Oct 31, 2022
1 parent 87e8f0c commit 2018d2e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/deploy/functions/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,17 +321,28 @@ export function inferDetailsFromExisting(
wantE.availableMemoryMb = haveE.availableMemoryMb;
}

// N.B. This code doesn't handle automatic downgrading of concurrency if
// the customer sets CPU <1. We'll instead error that you can't have both.
// We may want to handle this case, though it might also be surprising to
// customers if they _don't_ get an error and we silently drop concurrency.
if (typeof wantE.concurrency === "undefined" && haveE.concurrency) {
wantE.concurrency = haveE.concurrency;
}
if (typeof wantE.cpu === "undefined" && haveE.cpu) {
wantE.cpu = haveE.cpu;
}

// N.B. concurrency has different defaults based on CPU. If the customer
// only specifies CPU and they change that specification to < 1, we should
// turn off concurrency. We'll not do the opposite and turn on concurrency
// if there are >1 CPU because this could expose race conditions. They
// either need to start on concurrency where they've always needed to
// handle race conditions, or they should explicitly enable.
if (typeof wantE.concurrency === "undefined") {
const explicitlySmallCPU = typeof wantE.cpu === "number" && wantE.cpu < 1;
// !availableMemoryMB means we'll default to 256
const implicitlySmallCPU =
wantE.cpu === "gcf_gen1" && (wantE.availableMemoryMb || 256) < 2048;
if (explicitlySmallCPU || implicitlySmallCPU) {
wantE.concurrency = 1;
} else if (haveE.concurrency) {
wantE.concurrency = haveE.concurrency;
}
}

wantE.securityLevel = haveE.securityLevel ? haveE.securityLevel : "SECURE_ALWAYS";

maybeCopyTriggerRegion(wantE, haveE);
Expand Down
17 changes: 17 additions & 0 deletions src/test/deploy/functions/prepare.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,23 @@ describe("prepare", () => {
prepare.inferDetailsFromExisting(backend.of(want), backend.of(have), /* usedDotEnv= */ false);
expect(want.availableMemoryMb).to.equal(512);
});

it("downgrades concurrency if necessary", () => {
const have: backend.Endpoint = {
...ENDPOINT_BASE,
httpsTrigger: {},
concurrency: 80,
cpu: 1,
};
const want: backend.Endpoint = {
...ENDPOINT_BASE,
httpsTrigger: {},
cpu: 0.5,
};

prepare.inferDetailsFromExisting(backend.of(want), backend.of(have), /* useDotEnv= */ false);
expect(want.concurrency).to.equal(1);
});
});

describe("inferBlockingDetails", () => {
Expand Down

0 comments on commit 2018d2e

Please sign in to comment.