add retries inside the getLiveKitJWTWithDelayDelegation and

`getLiveKitJWT` functions.
This commit is contained in:
Timo K
2026-01-07 17:38:29 +01:00
parent 5556d363b9
commit a5a4bb2b82

View File

@@ -6,7 +6,6 @@ Please see LICENSE in the repository root for full details.
*/
import {
HTTPError,
retryNetworkOperation,
type IOpenIDToken,
type MatrixClient,
@@ -116,7 +115,6 @@ export async function getSFUConfigWithOpenID(
// since we are not sending the new matrix2.0 sticky events (no hashed identity in the event)
if (forceOldJwtEndpoint === false) {
try {
await retryNetworkOperation(4, async () => {
sfuConfig = await getLiveKitJWTWithDelayDelegation(
membership,
serviceUrl,
@@ -126,7 +124,6 @@ export async function getSFUConfigWithOpenID(
delayId,
);
logger?.info(`Got JWT from call's active focus URL.`);
});
} catch (e) {
if (e instanceof NotSupportedError) {
logger?.warn(
@@ -146,14 +143,13 @@ export async function getSFUConfigWithOpenID(
// DEPRECATED
// Either forceOldJwtEndpoint = true or getLiveKitJWTWithDelayDelegation throws -> reset sfuConfig = undefined
if (sfuConfig === undefined) {
await retryNetworkOperation(4, async () => {
sfuConfig = await getLiveKitJWT(
membership.deviceId,
serviceUrl,
roomId,
openIdToken,
);
});
logger?.info(`Got JWT from call's active focus URL.`);
}
@@ -175,14 +171,16 @@ export async function getSFUConfigWithOpenID(
livekitIdentity: payload.sub,
};
}
const RETRIES = 4;
async function getLiveKitJWT(
deviceId: string,
livekitServiceURL: string,
matrixRoomId: string,
openIDToken: IOpenIDToken,
): Promise<{ url: string; jwt: string }> {
const res = await fetch(livekitServiceURL + "/sfu/get", {
let res: Response | undefined;
await retryNetworkOperation(RETRIES, async () => {
res = await fetch(livekitServiceURL + "/sfu/get", {
method: "POST",
headers: {
"Content-Type": "application/json",
@@ -194,6 +192,12 @@ async function getLiveKitJWT(
device_id: deviceId,
}),
});
});
if (!res) {
throw new Error(
`Network error while connecting to jwt service after ${RETRIES} retries`,
);
}
if (!res.ok) {
throw new Error("SFU Config fetch failed with status code " + res.status);
}
@@ -240,13 +244,23 @@ export async function getLiveKitJWTWithDelayDelegation(
};
}
const res = await fetch(livekitServiceURL + "/get_token", {
let res: Response | undefined;
await retryNetworkOperation(RETRIES, async () => {
res = await fetch(livekitServiceURL + "/get_token", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ ...body, ...bodyDalayParts }),
});
});
if (!res) {
throw new Error(
`Network error while connecting to jwt service after ${RETRIES} retries`,
);
}
if (!res.ok) {
const msg = "SFU Config fetch failed with status code " + res.status;
if (res.status === 404) {