Add throttling to bitbucket server client

Signed-off-by: Kevin Johnson <kljohnson@verisk.com>
This commit is contained in:
Kevin Johnson
2024-11-26 13:31:30 -07:00
committed by Fredrik Adelöw
parent 696dbde655
commit b3ff40c269
3 changed files with 28 additions and 12 deletions
@@ -56,6 +56,7 @@
"@backstage/errors": "workspace:^",
"@backstage/integration": "workspace:^",
"@backstage/plugin-catalog-node": "workspace:^",
"p-throttle": "^4.1.1",
"uuid": "^11.0.0"
},
"devDependencies": {
@@ -19,6 +19,19 @@ import {
getBitbucketServerRequestOptions,
} from '@backstage/integration';
import { BitbucketServerProject, BitbucketServerRepository } from './types';
import pThrottle from 'p-throttle';
const throttle = pThrottle({
limit: 1,
interval: 500,
});
const throttledFetch = throttle(
async (url: fetch.RequestInfo, options?: fetch.RequestInit) => {
console.log(JSON.stringify(url));
return await fetch(url, options);
},
);
/**
* A client for interacting with a Bitbucket Server instance
@@ -65,7 +78,7 @@ export class BitbucketServerClient {
path: string;
}): Promise<Response> {
const base = new URL(this.config.apiBaseUrl);
return fetch(
return throttledFetch(
`${base.protocol}//${base.host}/projects/${options.projectKey}/repos/${options.repo}/raw/${options.path}`,
getBitbucketServerRequestOptions(this.config),
);
@@ -76,7 +89,7 @@ export class BitbucketServerClient {
repo: string;
}): Promise<BitbucketServerRepository> {
const request = `${this.config.apiBaseUrl}/projects/${options.projectKey}/repos/${options.repo}`;
const response = await fetch(
const response = await throttledFetch(
request,
getBitbucketServerRequestOptions(this.config),
);
@@ -117,16 +130,17 @@ export class BitbucketServerClient {
}
private async request(req: Request): Promise<Response> {
return fetch(req, getBitbucketServerRequestOptions(this.config)).then(
(response: Response) => {
if (!response.ok) {
throw new Error(
`Unexpected response for ${req.method} ${req.url}. Expected 200 but got ${response.status} - ${response.statusText}`,
);
}
return response;
},
);
return throttledFetch(
req,
getBitbucketServerRequestOptions(this.config),
).then((response: Response) => {
if (!response.ok) {
throw new Error(
`Unexpected response for ${req.method} ${req.url}. Expected 200 but got ${response.status} - ${response.statusText}`,
);
}
return response;
});
}
}
+1
View File
@@ -5876,6 +5876,7 @@ __metadata:
"@backstage/plugin-catalog-node": "workspace:^"
luxon: ^3.0.0
msw: ^1.0.0
p-throttle: ^4.1.1
uuid: ^11.0.0
languageName: unknown
linkType: soft