-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extend allowed memory for GCP functions (#339)
- Allows 8 GB memory for Gen 1 functions - Allows 16 GB and 32GB for Gen 2 functions - Adds validation logic to ensure that CPU allocations are consistent with memory limits
- Loading branch information
Showing
5 changed files
with
129 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export type ValidMemory = | ||
| '128MB' | ||
| '256MB' | ||
| '512MB' | ||
| '1024MB' | ||
| '2048MB' | ||
| '4096MB' | ||
| '8192MB' | ||
| '16384MB' | ||
| '32768MB' | ||
|
||
export type Gen = 1 | 2 |
64 changes: 64 additions & 0 deletions
64
packages/gcp-functions/src/utils/validate-resources.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { validateResources } from './validate-resources' | ||
import { Gen, ValidMemory } from '../types' | ||
|
||
describe('validateResources', () => { | ||
it('should throw an error if memory is gen2 and gen is 1', () => { | ||
const resources = { | ||
cpu: 4, | ||
memory: '16384MB' as const, | ||
gen: 1 as const | ||
} | ||
|
||
expect(() => validateResources(resources)).toThrow() | ||
}) | ||
|
||
it('should throw an error if CPU is less than minimum required for memory', () => { | ||
const resources = { | ||
cpu: 0.5, | ||
memory: '1024MB' as const, | ||
gen: 2 as const | ||
} | ||
|
||
expect(() => validateResources(resources)).toThrow() | ||
}) | ||
|
||
it('should not throw an error for valid resources', () => { | ||
const resources = { | ||
cpu: 1, | ||
memory: '1024MB' as ValidMemory, | ||
gen: 2 as Gen | ||
} | ||
|
||
expect(() => validateResources(resources)).not.toThrow() | ||
}) | ||
|
||
it('should not throw an error for gen2 memory and gen 2', () => { | ||
const resources = { | ||
cpu: 4, | ||
memory: '16384MB' as const, | ||
gen: 2 as Gen | ||
} | ||
|
||
expect(() => validateResources(resources)).not.toThrow() | ||
}) | ||
|
||
it('should not throw an error for minimum CPU and memory', () => { | ||
const resources = { | ||
cpu: 0.083, | ||
memory: '128MB' as const, | ||
gen: 2 as const | ||
} | ||
|
||
expect(() => validateResources(resources)).not.toThrow() | ||
}) | ||
|
||
it('should not throw an error if CPU is higher than minimum required for memory', () => { | ||
const resources = { | ||
cpu: 1, | ||
memory: '256MB' as const, | ||
gen: 1 as const | ||
} | ||
|
||
expect(() => validateResources(resources)).not.toThrow() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Gen, ValidMemory } from '../types' | ||
|
||
const gen2Memory: ValidMemory[] = ['16384MB', '32768MB'] | ||
const minimumCPU: Record<ValidMemory, number> = { | ||
'128MB': 0.083, | ||
'256MB': 0.167, | ||
'512MB': 0.333, | ||
'1024MB': 0.583, | ||
'2048MB': 1, | ||
'4096MB': 2, | ||
'8192MB': 2, | ||
'16384MB': 4, | ||
'32768MB': 8 | ||
} | ||
const docsLink = 'https://cloud.google.com/functions/docs/configuring/memory' | ||
|
||
export const validateResources = ({ | ||
cpu, | ||
memory, | ||
gen | ||
}: { | ||
cpu: number | ||
memory: ValidMemory | ||
gen: Gen | ||
}) => { | ||
if (gen2Memory.includes(memory) && gen === 1) { | ||
throw new Error( | ||
`Setting memory to "${memory}" is not supported when gen is 2. | ||
See ${docsLink} for more information. | ||
` | ||
) | ||
} | ||
|
||
if (cpu < minimumCPU[memory]) { | ||
throw new Error( | ||
`CPU value of ${cpu} is not enough for memory of ${memory}. | ||
See ${docsLink} for more information | ||
` | ||
) | ||
} | ||
} |