Skip to content

Commit c5f62ca

Browse files
committed
feat(functions): add 1.5GB and 3GB memory options for v2
- Extend memory option types/mappings to include 1536MB and 3072MB with display labels. - Map new sizes to sensible v2 CPU defaults (1 CPU for 1.5GB, 2 CPU for 3GB) and keep gen1 allowed set unchanged. - Add validation guard so gen1 rejects unsupported memory values; update coverage to allow new sizes for v2 and assert the gen1 rejection.
1 parent 969b779 commit c5f62ca

File tree

4 files changed

+69
-5
lines changed

4 files changed

+69
-5
lines changed

src/deploy/functions/backend.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,24 @@ export const AllIngressSettings: IngressSettings[] = [
170170
"ALLOW_INTERNAL_ONLY",
171171
"ALLOW_INTERNAL_AND_GCLB",
172172
];
173-
export type MemoryOptions = 128 | 256 | 512 | 1024 | 2048 | 4096 | 8192 | 16384 | 32768;
174-
const allMemoryOptions: MemoryOptions[] = [128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768];
173+
export type MemoryOptions =
174+
| 128
175+
| 256
176+
| 512
177+
| 1024
178+
| 1536
179+
| 2048
180+
| 3072
181+
| 4096
182+
| 8192
183+
| 16384
184+
| 32768;
185+
const allMemoryOptions: MemoryOptions[] = [
186+
128, 256, 512, 1024, 1536, 2048, 3072, 4096, 8192, 16384, 32768,
187+
];
188+
export const GCFV1_MEMORY_OPTIONS: MemoryOptions[] = [
189+
128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768,
190+
];
175191

176192
/**
177193
* Is a given number a valid MemoryOption?
@@ -180,6 +196,10 @@ export function isValidMemoryOption(mem: unknown): mem is MemoryOptions {
180196
return allMemoryOptions.includes(mem as MemoryOptions);
181197
}
182198

199+
export function isValidGcfv1MemoryOption(mem: unknown): mem is MemoryOptions {
200+
return GCFV1_MEMORY_OPTIONS.includes(mem as MemoryOptions);
201+
}
202+
183203
export function isValidEgressSetting(egress: unknown): egress is VpcEgressSettings {
184204
return egress === "PRIVATE_RANGES_ONLY" || egress === "ALL_TRAFFIC";
185205
}
@@ -201,7 +221,9 @@ export function memoryOptionDisplayName(option: MemoryOptions): string {
201221
256: "256MB",
202222
512: "512MB",
203223
1024: "1GB",
224+
1536: "1.5GB",
204225
2048: "2GB",
226+
3072: "3GB",
205227
4096: "4GB",
206228
8192: "8GB",
207229
16384: "16GB",
@@ -222,7 +244,9 @@ export function memoryToGen1Cpu(memory: MemoryOptions): number {
222244
256: 0.1666, // ~1/6
223245
512: 0.3333, // ~1/3
224246
1024: 0.5833, // ~5/7
247+
1536: 0.8,
225248
2048: 1,
249+
3072: 1.5,
226250
4096: 2,
227251
8192: 2,
228252
16384: 4,
@@ -241,7 +265,9 @@ export function memoryToGen2Cpu(memory: MemoryOptions): number {
241265
256: 1,
242266
512: 1,
243267
1024: 1,
268+
1536: 1,
244269
2048: 1,
270+
3072: 2,
245271
4096: 2,
246272
8192: 2,
247273
16384: 4,

src/deploy/functions/build.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,21 @@ export interface SecretEnvVar {
205205
projectId: string; // The project containing the Secret
206206
}
207207

208-
export type MemoryOption = 128 | 256 | 512 | 1024 | 2048 | 4096 | 8192 | 16384 | 32768;
209-
const allMemoryOptions: MemoryOption[] = [128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768];
208+
export type MemoryOption =
209+
| 128
210+
| 256
211+
| 512
212+
| 1024
213+
| 1536
214+
| 2048
215+
| 3072
216+
| 4096
217+
| 8192
218+
| 16384
219+
| 32768;
220+
const allMemoryOptions: MemoryOption[] = [
221+
128, 256, 512, 1024, 1536, 2048, 3072, 4096, 8192, 16384, 32768,
222+
];
210223

211224
// Run is an automatic migration from gcfv2 and is not used on the wire.
212225
export type FunctionsPlatform = Exclude<backend.FunctionsPlatform, "run">;

src/deploy/functions/validate.spec.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,16 @@ describe("validate", () => {
135135
expect(() => validate.endpointsAreValid(backend.of(ep))).to.throw(/GCF gen 1/);
136136
});
137137

138+
it("disallows unsupported memory for GCF gen 1", () => {
139+
const ep: backend.Endpoint = {
140+
...ENDPOINT_BASE,
141+
platform: "gcfv1",
142+
availableMemoryMb: 1536,
143+
};
144+
145+
expect(() => validate.endpointsAreValid(backend.of(ep))).to.throw(/only supports/);
146+
});
147+
138148
it("Disallows concurrency for low-CPU gen 2", () => {
139149
const ep: backend.Endpoint = {
140150
...ENDPOINT_BASE,
@@ -289,7 +299,7 @@ describe("validate", () => {
289299
});
290300
}
291301

292-
for (const mem of [128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768] as const) {
302+
for (const mem of [128, 256, 512, 1024, 1536, 2048, 3072, 4096, 8192, 16384, 32768] as const) {
293303
it(`allows gcfv2 endpoints with mem ${mem} and no cpu`, () => {
294304
const ep: backend.Endpoint = {
295305
...ENDPOINT_BASE,

src/deploy/functions/validate.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@ export function endpointsAreValid(wantBackend: backend.Backend): void {
3737
}
3838

3939
// Our SDK doesn't let people articulate this, but it's theoretically possible in the manifest syntax.
40+
const gcfV1WithUnsupportedMemory = matchingIds(
41+
endpoints,
42+
(endpoint) =>
43+
endpoint.platform === "gcfv1" &&
44+
endpoint.availableMemoryMb !== undefined &&
45+
endpoint.availableMemoryMb !== null &&
46+
!backend.isValidGcfv1MemoryOption(endpoint.availableMemoryMb),
47+
);
48+
if (gcfV1WithUnsupportedMemory.length) {
49+
const msg = `Cannot set availableMemoryMb on the functions ${gcfV1WithUnsupportedMemory} because GCF gen 1 only supports ${backend.GCFV1_MEMORY_OPTIONS.join(
50+
", ",
51+
)} MB`;
52+
throw new FirebaseError(msg);
53+
}
54+
4055
const gcfV1WithConcurrency = matchingIds(
4156
endpoints,
4257
(endpoint) => (endpoint.concurrency || 1) !== 1 && endpoint.platform === "gcfv1",

0 commit comments

Comments
 (0)