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