Skip to content

Commit ab07871

Browse files
committed
fix: cache GitHub App ID to reduce SSM calls
Cache the GitHub App ID at module level to avoid repeated SSM parameter fetches on every rate limit metric update. The value is now fetched once per Lambda execution context and reused across invocations.
1 parent 6685cb6 commit ab07871

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

lambdas/functions/control-plane/src/github/rate-limit.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ import { MetricUnit } from '@aws-lambda-powertools/metrics';
44
import yn from 'yn';
55
import { getParameter } from '@aws-github-runner/aws-ssm-util';
66

7+
// Cache the app ID to avoid repeated SSM calls across Lambda invocations
8+
let appIdPromise: Promise<string> | null = null;
9+
10+
async function getAppId(): Promise<string> {
11+
if (!appIdPromise) {
12+
appIdPromise = getParameter(process.env.PARAMETER_GITHUB_APP_ID_NAME);
13+
}
14+
return appIdPromise;
15+
}
16+
717
export async function metricGitHubAppRateLimit(headers: ResponseHeaders): Promise<void> {
818
try {
919
const remaining = parseInt(headers['x-ratelimit-remaining'] as string);
@@ -13,7 +23,7 @@ export async function metricGitHubAppRateLimit(headers: ResponseHeaders): Promis
1323

1424
const updateMetric = yn(process.env.ENABLE_METRIC_GITHUB_APP_RATE_LIMIT);
1525
if (updateMetric) {
16-
const appId = await getParameter(process.env.PARAMETER_GITHUB_APP_ID_NAME);
26+
const appId = await getAppId();
1727
const metric = createSingleMetric('GitHubAppRateLimitRemaining', MetricUnit.Count, remaining, {
1828
AppId: appId,
1929
});

0 commit comments

Comments
 (0)