Skip to content

Commit 49b598a

Browse files
committed
Add maximum time for flushing batches
1 parent e1f36dc commit 49b598a

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

packages/protocol/src/common/proxy.ts

+26-18
Original file line numberDiff line numberDiff line change
@@ -92,20 +92,25 @@ interface BatchItem<T, A> {
9292
* Batch remote calls.
9393
*/
9494
export abstract class Batch<T, A> {
95-
/**
96-
* Flush after reaching this count.
97-
*/
98-
private maxCount = 100;
99-
100-
/**
101-
* Flush after not receiving more requests for this amount of time.
102-
*/
103-
private maxTime = 100;
104-
105-
private flushTimeout: number | NodeJS.Timer | undefined;
106-
95+
private idleTimeout: number | NodeJS.Timer | undefined;
96+
private maxTimeout: number | NodeJS.Timer | undefined;
10797
private batch = <BatchItem<T, A>[]>[];
10898

99+
public constructor(
100+
/**
101+
* Flush after this reaching amount of time.
102+
*/
103+
private readonly maxTime = 1000,
104+
/**
105+
* Flush after reaching this count.
106+
*/
107+
private readonly maxCount = 100,
108+
/**
109+
* Flush after not receiving more requests for this amount of time.
110+
*/
111+
private readonly idleTime = 100,
112+
) {}
113+
109114
public add = (args: A): Promise<T> => {
110115
return new Promise((resolve, reject) => {
111116
this.batch.push({
@@ -116,26 +121,29 @@ export abstract class Batch<T, A> {
116121
if (this.batch.length >= this.maxCount) {
117122
this.flush();
118123
} else {
119-
clearTimeout(this.flushTimeout as any);
120-
this.flushTimeout = setTimeout(this.flush, this.maxTime);
124+
clearTimeout(this.idleTimeout as any);
125+
this.idleTimeout = setTimeout(this.flush, this.idleTime);
126+
if (typeof this.maxTimeout === "undefined") {
127+
this.maxTimeout = setTimeout(this.flush, this.maxTime);
128+
}
121129
}
122130
});
123131
}
124132

125133
protected abstract remoteCall(batch: A[]): Promise<(T | Error)[]>;
126134

127135
private flush = (): void => {
128-
clearTimeout(this.flushTimeout as any);
136+
clearTimeout(this.idleTimeout as any);
137+
clearTimeout(this.maxTimeout as any);
138+
this.maxTimeout = undefined;
129139

130140
const batch = this.batch;
131141
this.batch = [];
132142

133143
this.remoteCall(batch.map((q) => q.args)).then((results) => {
134144
batch.forEach((item, i) => {
135145
const result = results[i];
136-
if (!result) {
137-
item.reject(new Error("no response"));
138-
} else if (result instanceof Error) {
146+
if (result && result instanceof Error) {
139147
item.reject(result);
140148
} else {
141149
item.resolve(result);

packages/protocol/src/node/modules/fs.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ export class FsModuleProxy {
157157
}
158158

159159
public async lstatBatch(args: { path: fs.PathLike }[]): Promise<(Stats | Error)[]> {
160-
return Promise.all(args.map((args) => this.lstat(args.path).catch((e) => e)));
160+
return Promise.all(args.map((a) => this.lstat(a.path).catch((e) => e)));
161161
}
162162

163163
public mkdir(path: fs.PathLike, mode: number | string | fs.MakeDirectoryOptions | undefined | null): Promise<void> {
@@ -187,7 +187,7 @@ export class FsModuleProxy {
187187
}
188188

189189
public readdirBatch(args: { path: fs.PathLike, options: IEncodingOptions }[]): Promise<(Buffer[] | fs.Dirent[] | string[] | Error)[]> {
190-
return Promise.all(args.map((args) => this.readdir(args.path, args.options).catch((e) => e)));
190+
return Promise.all(args.map((a) => this.readdir(a.path, a.options).catch((e) => e)));
191191
}
192192

193193
public readlink(path: fs.PathLike, options: IEncodingOptions): Promise<string | Buffer> {
@@ -211,7 +211,7 @@ export class FsModuleProxy {
211211
}
212212

213213
public async statBatch(args: { path: fs.PathLike }[]): Promise<(Stats | Error)[]> {
214-
return Promise.all(args.map((args) => this.stat(args.path).catch((e) => e)));
214+
return Promise.all(args.map((a) => this.stat(a.path).catch((e) => e)));
215215
}
216216

217217
public symlink(target: fs.PathLike, path: fs.PathLike, type?: fs.symlink.Type | null): Promise<void> {

0 commit comments

Comments
 (0)