Skip to content

Commit cb91e7b

Browse files
fix(document): align remote rpc timeout with wait policy (#603)
1 parent 3f16953 commit cb91e7b

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

packages/programs/data/document/document/src/search.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2283,6 +2283,15 @@ export class DocumentIndex<
22832283
// this will lead to bad UX as you usually want to list/expore whats going on before doing any replication work
22842284
remote.priority = 2;
22852285
}
2286+
if (remote && remote.timeout == null && options?.remote) {
2287+
const waitPolicy =
2288+
typeof options.remote === "object" ? options.remote.wait : undefined;
2289+
const waitTimeout =
2290+
typeof waitPolicy === "object" ? waitPolicy.timeout : undefined;
2291+
if (waitTimeout != null) {
2292+
remote.timeout = waitTimeout;
2293+
}
2294+
}
22862295

22872296
if (!local && !remote) {
22882297
throw new Error(

packages/programs/data/document/document/test/index.spec.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3611,6 +3611,53 @@ describe("index", () => {
36113611
expect(result?.id).to.equal("1");
36123612
expect(t1 - t0).to.be.lessThan(500); // Should return quickly, not wait for timeout
36133613
});
3614+
3615+
it("uses wait timeout as remote rpc timeout when timeout is omitted", async () => {
3616+
session = await TestSession.connected(1);
3617+
3618+
const store = await session.peers[0].open(
3619+
new TestStore({
3620+
docs: new Documents<Document>(),
3621+
}),
3622+
{
3623+
args: {
3624+
replicate: false,
3625+
},
3626+
},
3627+
);
3628+
3629+
const wantedTimeout = 12_345;
3630+
let observedTimeout: number | undefined = undefined;
3631+
const requestFn = store.docs.index._query.request.bind(
3632+
store.docs.index._query,
3633+
);
3634+
store.docs.index._query.request = async (request, options) => {
3635+
if (
3636+
request instanceof SearchRequest ||
3637+
request instanceof SearchRequestIndexed ||
3638+
request instanceof IterationRequest
3639+
) {
3640+
observedTimeout = options?.timeout;
3641+
return [];
3642+
}
3643+
return requestFn(request, options);
3644+
};
3645+
3646+
await store.docs.index["queryCommence"](
3647+
new SearchRequest({ fetch: 1 }),
3648+
{
3649+
local: false,
3650+
remote: {
3651+
from: ["missing-peer"],
3652+
wait: {
3653+
timeout: wantedTimeout,
3654+
},
3655+
},
3656+
},
3657+
);
3658+
3659+
expect(observedTimeout).to.equal(wantedTimeout);
3660+
});
36143661
});
36153662
});
36163663

0 commit comments

Comments
 (0)