1
0
Fork 0

feat(api): remove client-side request mutex

This commit is contained in:
Sean Sube 2023-02-04 12:24:31 -06:00
parent 88815b3537
commit 4e7bfd79d7
Signed by: ssube
GPG Key ID: 3EED7B957D362AF1
1 changed files with 11 additions and 45 deletions

View File

@ -304,12 +304,8 @@ export function appendUpscaleToURL(url: URL, upscale: UpscaleParams) {
* Make an API client using the given API root and fetch client. * Make an API client using the given API root and fetch client.
*/ */
export function makeClient(root: string, f = fetch): ApiClient { export function makeClient(root: string, f = fetch): ApiClient {
let pending: Promise<ImageResponse> | undefined;
function throttleRequest(url: URL, options: RequestInit): Promise<ImageResponse> { function throttleRequest(url: URL, options: RequestInit): Promise<ImageResponse> {
return f(url, options).then((res) => parseApiResponse(root, res)).finally(() => { return f(url, options).then((res) => parseApiResponse(root, res));
pending = undefined;
});
} }
return { return {
@ -344,10 +340,6 @@ export function makeClient(root: string, f = fetch): ApiClient {
return await res.json() as Array<string>; return await res.json() as Array<string>;
}, },
async img2img(model: ModelParams, params: Img2ImgParams, upscale?: UpscaleParams): Promise<ImageResponse> { async img2img(model: ModelParams, params: Img2ImgParams, upscale?: UpscaleParams): Promise<ImageResponse> {
if (doesExist(pending)) {
return pending;
}
const url = makeImageURL(root, 'img2img', params); const url = makeImageURL(root, 'img2img', params);
appendModelToURL(url, model); appendModelToURL(url, model);
@ -360,19 +352,13 @@ export function makeClient(root: string, f = fetch): ApiClient {
const body = new FormData(); const body = new FormData();
body.append('source', params.source, 'source'); body.append('source', params.source, 'source');
pending = throttleRequest(url, { // eslint-disable-next-line no-return-await
return await throttleRequest(url, {
body, body,
method: 'POST', method: 'POST',
}); });
// eslint-disable-next-line no-return-await
return await pending;
}, },
async txt2img(model: ModelParams, params: Txt2ImgParams, upscale?: UpscaleParams): Promise<ImageResponse> { async txt2img(model: ModelParams, params: Txt2ImgParams, upscale?: UpscaleParams): Promise<ImageResponse> {
if (doesExist(pending)) {
return pending;
}
const url = makeImageURL(root, 'txt2img', params); const url = makeImageURL(root, 'txt2img', params);
appendModelToURL(url, model); appendModelToURL(url, model);
@ -388,18 +374,12 @@ export function makeClient(root: string, f = fetch): ApiClient {
appendUpscaleToURL(url, upscale); appendUpscaleToURL(url, upscale);
} }
pending = throttleRequest(url, { // eslint-disable-next-line no-return-await
return await throttleRequest(url, {
method: 'POST', method: 'POST',
}); });
// eslint-disable-next-line no-return-await
return await pending;
}, },
async inpaint(model: ModelParams, params: InpaintParams, upscale?: UpscaleParams) { async inpaint(model: ModelParams, params: InpaintParams, upscale?: UpscaleParams) {
if (doesExist(pending)) {
return pending;
}
const url = makeImageURL(root, 'inpaint', params); const url = makeImageURL(root, 'inpaint', params);
appendModelToURL(url, model); appendModelToURL(url, model);
@ -416,19 +396,13 @@ export function makeClient(root: string, f = fetch): ApiClient {
body.append('mask', params.mask, 'mask'); body.append('mask', params.mask, 'mask');
body.append('source', params.source, 'source'); body.append('source', params.source, 'source');
pending = throttleRequest(url, { // eslint-disable-next-line no-return-await
return await throttleRequest(url, {
body, body,
method: 'POST', method: 'POST',
}); });
// eslint-disable-next-line no-return-await
return await pending;
}, },
async outpaint(model: ModelParams, params: OutpaintParams, upscale?: UpscaleParams) { async outpaint(model: ModelParams, params: OutpaintParams, upscale?: UpscaleParams) {
if (doesExist(pending)) {
return pending;
}
const url = makeImageURL(root, 'inpaint', params); const url = makeImageURL(root, 'inpaint', params);
appendModelToURL(url, model); appendModelToURL(url, model);
@ -461,19 +435,13 @@ export function makeClient(root: string, f = fetch): ApiClient {
body.append('mask', params.mask, 'mask'); body.append('mask', params.mask, 'mask');
body.append('source', params.source, 'source'); body.append('source', params.source, 'source');
pending = throttleRequest(url, { // eslint-disable-next-line no-return-await
return await throttleRequest(url, {
body, body,
method: 'POST', method: 'POST',
}); });
// eslint-disable-next-line no-return-await
return await pending;
}, },
async upscale(model: ModelParams, params: UpscaleReqParams, upscale: UpscaleParams): Promise<ImageResponse> { async upscale(model: ModelParams, params: UpscaleReqParams, upscale: UpscaleParams): Promise<ImageResponse> {
if (doesExist(pending)) {
return pending;
}
const url = makeApiUrl(root, 'upscale'); const url = makeApiUrl(root, 'upscale');
appendModelToURL(url, model); appendModelToURL(url, model);
@ -484,13 +452,11 @@ export function makeClient(root: string, f = fetch): ApiClient {
const body = new FormData(); const body = new FormData();
body.append('source', params.source, 'source'); body.append('source', params.source, 'source');
pending = throttleRequest(url, { // eslint-disable-next-line no-return-await
return await throttleRequest(url, {
body, body,
method: 'POST', method: 'POST',
}); });
// eslint-disable-next-line no-return-await
return await pending;
}, },
async ready(params: ImageResponse): Promise<ReadyResponse> { async ready(params: ImageResponse): Promise<ReadyResponse> {
const path = makeApiUrl(root, 'ready'); const path = makeApiUrl(root, 'ready');