Skip to content

Custom timeout for delayed event restarts #4921

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions spec/unit/matrix-client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,32 @@ describe("MatrixClient", function () {

await client._unstable_updateDelayedEvent(delayId, action);
});

it("uses custom timeout for restart delayed event", async () => {
// make another client so we can pass creation opts
makeClient({ delayedEventRestartLocalTimeoutMS: 2300 });
const delayId = "id";
const action = UpdateDelayedEventAction.Restart;
httpLookups = [
{
method: "POST",
prefix: unstableMSC4140Prefix,
path: `/delayed_events/${encodeURIComponent(delayId)}`,
data: {
action,
},
},
];
await client._unstable_updateDelayedEvent(delayId, action);

expect(client.http.authedRequest).toHaveBeenLastCalledWith(
"POST",
"/delayed_events/id",
undefined,
{ action: "restart" },
{ localTimeoutMs: 2300, prefix: "/_matrix/client/unstable/org.matrix.msc4140" },
);
});
});

describe("extended profiles", () => {
Expand Down
21 changes: 20 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,19 @@ export interface ICreateClientOpts {
*/
localTimeoutMs?: number;

/**
* The maximum amount of time to wait before timing out the `POST /_matrix/client/v1/delayed_events/{delay_id}` with `action = "restart"` requests.
* If not specified, it uses `localTimeoutMs` if set, otherwise there is no timeout.
*
* This setting is used in the context of MatrixRTC. We need to restart the dealyed events to make sure
* the HomeServer is sending the delayed rtc leave event. In bad network environments we might end up
* waiting for too long for the event to arrive and we will not send another restart event until the local timeout is reached.
*
* In those scenarios chances for success are higher if we use a lower local timeout to increase the tries we do instead of waiting
* for responses on requests which are stuck.
*/
delayedEventRestartLocalTimeoutMS?: number;

/**
* Set to false to send the access token to the server via a query parameter rather
* than the Authorization HTTP header.
Expand Down Expand Up @@ -1272,6 +1285,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
protected txnCtr = 0;
protected mediaHandler = new MediaHandler(this);
protected sessionId: string;
protected delayedEventRestartLocalTimeoutMS: number | undefined;

/** IDs of events which are currently being encrypted.
*
Expand Down Expand Up @@ -1311,6 +1325,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
this.store = opts.store || new StubStore();
this.deviceId = opts.deviceId || null;
this.sessionId = secureRandomString(10);
this.delayedEventRestartLocalTimeoutMS = opts.delayedEventRestartLocalTimeoutMS;

const userId = opts.userId || null;
this.credentials = { userId };
Expand Down Expand Up @@ -3473,8 +3488,12 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
const data = {
action,
};
return await this.http.authedRequest(Method.Post, path, undefined, data, {
const opts = {
localTimeoutMs: action === "restart" ? this.delayedEventRestartLocalTimeoutMS : undefined,
...requestOptions,
};
return await this.http.authedRequest(Method.Post, path, undefined, data, {
...opts,
prefix: `${ClientPrefix.Unstable}/${UNSTABLE_MSC4140_DELAYED_EVENTS}`,
});
}
Expand Down