Skip to content

Commit 434bc59

Browse files
ui: per-instance port for HttpRpcEngine (#160)
1 parent dbc3f3f commit 434bc59

File tree

6 files changed

+32
-20
lines changed

6 files changed

+32
-20
lines changed

ui/src/core/app_impl.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,8 +428,8 @@ export class AppImpl implements App {
428428
return this.openTrace({...args, type: 'ARRAY_BUFFER', serializedAppState});
429429
}
430430

431-
openTraceFromHttpRpc() {
432-
return this.openTrace({type: 'HTTP_RPC'});
431+
openTraceFromHttpRpc(port?: string) {
432+
return this.openTrace({type: 'HTTP_RPC', port});
433433
}
434434

435435
private async openTrace(src: TraceSource): Promise<TraceImpl> {

ui/src/core/load_trace.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,19 +128,23 @@ export async function loadTrace(
128128
): Promise<TraceImpl> {
129129
updateStatus(traceSource, app, 'Opening trace');
130130
const engineId = `${++lastEngineId}`;
131-
const engine = await createEngine(app, engineId);
131+
const engine = await createEngine(app, engineId, traceSource);
132132
return await loadTraceIntoEngine(app, traceSource, engine);
133133
}
134134

135135
async function createEngine(
136136
app: AppImpl,
137137
engineId: string,
138+
traceSource: TraceSource,
138139
): Promise<EngineBase> {
139140
// Check if there is any instance of the trace_processor_shell running in
140141
// HTTP RPC mode (i.e. trace_processor_shell -D).
141142
let useRpc = false;
143+
const port =
144+
(traceSource.type === 'HTTP_RPC' && traceSource.port) ||
145+
HttpRpcEngine.defaultRpcPort;
142146
if (app.httpRpc.newEngineMode === 'USE_HTTP_RPC_IF_AVAILABLE') {
143-
useRpc = (await HttpRpcEngine.checkConnection()).connected;
147+
useRpc = (await HttpRpcEngine.checkConnection(port)).connected;
144148
}
145149
const descriptorBlobs: Uint8Array[] = [];
146150
if (app.extraParsingDescriptors.length > 0) {
@@ -151,7 +155,7 @@ async function createEngine(
151155
let engine;
152156
if (useRpc) {
153157
console.log('Opening trace using native accelerator over HTTP+RPC');
154-
engine = new HttpRpcEngine(engineId);
158+
engine = new HttpRpcEngine(engineId, port);
155159
} else {
156160
console.log('Opening trace using built-in WASM engine');
157161
engine = new WasmEngineProxy(engineId);
@@ -487,7 +491,7 @@ async function getTraceInfo(
487491
traceTitle += ` (${arrayBufferSizeMB} MB)`;
488492
break;
489493
case 'HTTP_RPC':
490-
traceTitle = `RPC @ ${HttpRpcEngine.hostAndPort}`;
494+
traceTitle = `RPC @ ${HttpRpcEngine.getHostAndPort()}`;
491495
break;
492496
default:
493497
break;

ui/src/core/trace_source.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export interface TraceStreamSource {
5151

5252
export interface TraceHttpRpcSource {
5353
readonly type: 'HTTP_RPC';
54+
readonly port?: string;
5455
}
5556

5657
export interface TraceArrayBufferSource {

ui/src/frontend/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,11 @@ function onCssLoaded() {
418418
// accidentially clober the state of an open trace processor instance
419419
// otherwise.
420420
maybeChangeRpcPortFromFragment();
421-
checkHttpRpcConnection().then(() => {
421+
const source = AppImpl.instance.trace?.traceInfo.source;
422+
const port =
423+
(source?.type === 'HTTP_RPC' && source?.port) ||
424+
HttpRpcEngine.defaultRpcPort;
425+
checkHttpRpcConnection(port).then(() => {
422426
const route = Router.parseUrl(window.location.href);
423427
if (!AppImpl.instance.embeddedMode) {
424428
installFileDropHandler();
@@ -475,7 +479,7 @@ function maybeChangeRpcPortFromFragment() {
475479
],
476480
});
477481
} else {
478-
HttpRpcEngine.rpcPort = route.args.rpc_port;
482+
HttpRpcEngine.defaultRpcPort = route.args.rpc_port;
479483
}
480484
}
481485
}

ui/src/frontend/rpc_http_dialog.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const CURRENT_API_VERSION =
2525
protos.TraceProcessorApiVersion.TRACE_PROCESSOR_CURRENT_API_VERSION;
2626

2727
function getPromptMessage(tpStatus: protos.StatusResult): string {
28-
return `Trace Processor detected on ${HttpRpcEngine.hostAndPort} with:
28+
return `Trace Processor detected on ${HttpRpcEngine.getHostAndPort()} with:
2929
${tpStatus.loadedTraceName}
3030
3131
YES, use loaded trace:
@@ -47,7 +47,7 @@ Using the native accelerator has some minor caveats:
4747
}
4848

4949
function getIncompatibleRpcMessage(tpStatus: protos.StatusResult): string {
50-
return `The Trace Processor instance on ${HttpRpcEngine.hostAndPort} is too old.
50+
return `The Trace Processor instance on ${HttpRpcEngine.getHostAndPort()} is too old.
5151
5252
This UI requires TraceProcessor features that are not present in the
5353
Trace Processor native accelerator you are currently running.
@@ -69,7 +69,7 @@ Trace processor RPC API: ${tpStatus.apiVersion}
6969
}
7070

7171
function getVersionMismatchMessage(tpStatus: protos.StatusResult): string {
72-
return `The Trace Processor instance on ${HttpRpcEngine.hostAndPort} is a different build from the UI.
72+
return `The Trace Processor instance on ${HttpRpcEngine.getHostAndPort()} is a different build from the UI.
7373
7474
This may cause problems. Where possible it is better to use the matched version of the UI.
7575
You can do this by clicking the button below.
@@ -149,8 +149,8 @@ Trace processor RPC API: ${tpStatus.apiVersion}
149149
// trying to load a new trace. We do this ahead of time just to have a
150150
// consistent UX (i.e. so that the user can tell if the RPC is working without
151151
// having to open a trace).
152-
export async function checkHttpRpcConnection(): Promise<void> {
153-
const state = await HttpRpcEngine.checkConnection();
152+
export async function checkHttpRpcConnection(port: string): Promise<void> {
153+
const state = await HttpRpcEngine.checkConnection(port);
154154
AppImpl.instance.httpRpc.httpRpcAvailable = state.connected;
155155
if (!state.connected) {
156156
// No RPC = exit immediately to the WASM UI.

ui/src/trace_processor/http_rpc_engine.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,20 @@ export class HttpRpcEngine extends EngineBase {
3636
private isProcessingQueue = false;
3737

3838
// Can be changed by frontend/index.ts when passing ?rpc_port=1234 .
39-
static rpcPort = '9001';
39+
static defaultRpcPort = '9001';
4040

41-
constructor(id: string) {
41+
constructor(
42+
id: string,
43+
private port: string,
44+
) {
4245
super();
4346
this.id = id;
4447
}
4548

4649
rpcSendRequestBytes(data: Uint8Array): void {
4750
if (this.websocket === undefined) {
4851
if (this.disposed) return;
49-
const wsUrl = `ws://${HttpRpcEngine.hostAndPort}/websocket`;
52+
const wsUrl = `ws://${HttpRpcEngine.getHostAndPort(this.port)}/websocket`;
5053
this.websocket = new WebSocket(wsUrl);
5154
this.websocket.onopen = () => this.onWebsocketConnected();
5255
this.websocket.onmessage = (e) => this.onWebsocketMessage(e);
@@ -108,8 +111,8 @@ export class HttpRpcEngine extends EngineBase {
108111
this.isProcessingQueue = false;
109112
}
110113

111-
static async checkConnection(): Promise<HttpRpcState> {
112-
const RPC_URL = `http://${HttpRpcEngine.hostAndPort}/`;
114+
static async checkConnection(port: string): Promise<HttpRpcState> {
115+
const RPC_URL = `http://${HttpRpcEngine.getHostAndPort(port)}/`;
113116
const httpRpcState: HttpRpcState = {connected: false};
114117
console.info(
115118
`It's safe to ignore the ERR_CONNECTION_REFUSED on ${RPC_URL} below. ` +
@@ -137,8 +140,8 @@ export class HttpRpcEngine extends EngineBase {
137140
return httpRpcState;
138141
}
139142

140-
static get hostAndPort() {
141-
return `127.0.0.1:${HttpRpcEngine.rpcPort}`;
143+
static getHostAndPort(port = HttpRpcEngine.defaultRpcPort) {
144+
return `127.0.0.1:${port}`;
142145
}
143146

144147
[Symbol.dispose]() {

0 commit comments

Comments
 (0)