Skip to content
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

terminal: buffer the output before sending #11449

Merged
merged 3 commits into from
Jul 27, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
46 changes: 46 additions & 0 deletions packages/terminal/src/node/buffering-stream.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// *****************************************************************************
// Copyright (C) 2022 Ericsson and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
// *****************************************************************************

import { wait } from '@theia/core/lib/common/promise-util';
import { expect } from 'chai';
import { BufferingStream } from './buffering-stream';

describe('BufferringStream', () => {

it('should emit whatever data was buffered before the timeout', async () => {
const buffer = new BufferingStream({ emitInterval: 1000 });
const chunkPromise = waitForChunk(buffer);
buffer.push(Buffer.from([0]));
await wait(100);
buffer.push(Buffer.from([1]));
await wait(100);
buffer.push(Buffer.from([2, 3, 4]));
const chunk = await chunkPromise;
expect(chunk).deep.eq(Buffer.from([0, 1, 2, 3, 4]));
});

it('should not emit chunks bigger than maxChunkSize', async () => {
const buffer = new BufferingStream({ maxChunkSize: 2 });
buffer.push(Buffer.from([0, 1, 2, 3, 4, 5]));
expect(await waitForChunk(buffer)).deep.eq(Buffer.from([0, 1]));
expect(await waitForChunk(buffer)).deep.eq(Buffer.from([2, 3]));
expect(await waitForChunk(buffer)).deep.eq(Buffer.from([4, 5]));
});

function waitForChunk(buffer: BufferingStream): Promise<Buffer> {
return new Promise(resolve => buffer.onData(resolve));
}
});
78 changes: 78 additions & 0 deletions packages/terminal/src/node/buffering-stream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// *****************************************************************************
// Copyright (C) 2022 Ericsson and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
// *****************************************************************************

import { Emitter, Event } from '@theia/core/lib/common/event';

export interface BufferingStreamOptions {
/**
* Max size of the chunks being emitted.
*/
maxChunkSize?: number
/**
* Amount of time to wait between the moment we start buffering data
* and when we emit the buffered chunk.
*/
emitInterval?: number
}

/**
* This component will buffer whatever is pushed to it and emit chunks back
* every {@link BufferingStreamOptions.emitInterval}. It will also ensure that
* the emitted chunks never exceed {@link BufferingStreamOptions.maxChunkSize}.
*/
export class BufferingStream {

protected buffer?: Buffer;
protected timeout?: NodeJS.Timeout;
protected maxChunkSize: number;
protected emitInterval: number;

protected onDataEmitter = new Emitter<Buffer>();

constructor(options?: BufferingStreamOptions) {
this.emitInterval = options?.emitInterval ?? 16; // ms
this.maxChunkSize = options?.maxChunkSize ?? 16384; // bytes
paul-marechal marked this conversation as resolved.
Show resolved Hide resolved
}

get onData(): Event<Buffer> {
return this.onDataEmitter.event;
}

push(chunk: Buffer): void {
if (this.buffer) {
this.buffer = Buffer.concat([this.buffer, chunk]);
} else {
this.buffer = chunk;
this.timeout = setTimeout(() => this.emitBufferedChunk(), this.emitInterval);
}
}

dispose(): void {
clearTimeout(this.timeout);
this.buffer = undefined;
this.onDataEmitter.dispose();
}

protected emitBufferedChunk(): void {
this.onDataEmitter.fire(this.buffer!.slice(0, this.maxChunkSize));
if (this.buffer!.byteLength <= this.maxChunkSize) {
this.buffer = undefined;
} else {
this.buffer = this.buffer!.slice(this.maxChunkSize);
this.timeout = setTimeout(() => this.emitBufferedChunk(), this.emitInterval);
}
}
}
10 changes: 7 additions & 3 deletions packages/terminal/src/node/terminal-backend-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { TerminalProcess, ProcessManager } from '@theia/process/lib/node';
import { terminalsPath } from '../common/terminal-protocol';
import { MessagingService } from '@theia/core/lib/node/messaging/messaging-service';
import { RpcProtocol } from '@theia/core/';
import { BufferingStream } from './buffering-stream';

@injectable()
export class TerminalBackendContribution implements MessagingService.Contribution {
Expand Down Expand Up @@ -47,10 +48,13 @@ export class TerminalBackendContribution implements MessagingService.Contributio
};

const rpc = new RpcProtocol(channel, requestHandler);
output.on('data', data => {
rpc.sendNotification('onData', [data]);
const buffer = new BufferingStream();
buffer.onData(chunk => rpc.sendNotification('onData', [chunk.toString('utf8')]));
output.on('data', chunk => buffer.push(Buffer.from(chunk, 'utf8')));
channel.onClose(() => {
buffer.dispose();
output.dispose();
});
channel.onClose(() => output.dispose());
}
});
}
Expand Down