Skip to content

Commit

Permalink
[Code]: properly reconnect when langserver is down and clean up logs, f…
Browse files Browse the repository at this point in the history
…ix #839, #891 (#30601)
  • Loading branch information
zfy0701 authored Feb 20, 2019
1 parent 0dd40c8 commit 9d36039
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
15 changes: 10 additions & 5 deletions x-pack/plugins/code/server/lsp/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,8 @@ export class LanguageServerProxy implements ILanguageServerHandler {
this.targetPort,
this.targetHost
);
this.onDisconnected(() => setTimeout(() => this.reconnect(), 1000));
});
} else {
this.socket.connect(
this.targetPort,
this.targetHost
);
}
return this.connectingPromise;
}
Expand All @@ -255,6 +251,15 @@ export class LanguageServerProxy implements ILanguageServerHandler {
return Promise.reject('should not hit here');
}

private reconnect() {
if (!this.isClosed) {
this.socket.connect(
this.targetPort,
this.targetHost
);
}
}

private onSocketClosed() {
if (this.clientConnection) {
this.clientConnection.dispose();
Expand Down
37 changes: 20 additions & 17 deletions x-pack/plugins/code/server/lsp/ts_launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { resolve } from 'path';
import { Logger } from '../log';
import { ServerOptions } from '../server_options';
import { LoggerFactory } from '../utils/log_factory';
import { promiseTimeout } from '../utils/timeout';
import { ILanguageServerLauncher } from './language_server_launcher';
import { LanguageServerProxy } from './proxy';
import { RequestExpander } from './request_expander';
Expand Down Expand Up @@ -72,24 +71,28 @@ export class TypescriptServerLauncher implements ILanguageServerLauncher {
};
let child = spawnTs();
log.info(`Launch Typescript Language Server at port ${port}, pid:${child.pid}`);
const reconnect = () => {
log.info('reconnecting');
promiseTimeout(3000, proxy.connect()).then(
() => {
log.info('connected');
},
() => {
log.error('unable to connect within 3s, respawn ts server.');
child.kill();
child = spawnTs();
setTimeout(reconnect, 1000);
}
);
};
// TODO: how to properly implement timeout socket connection? maybe config during socket connection
// const reconnect = () => {
// log.debug('reconnecting');
// promiseTimeout(3000, proxy.connect()).then(
// () => {
// log.info('connected');
// },
// () => {
// log.error('unable to connect within 3s, respawn ts server.');
// child.kill();
// child = spawnTs();
// setTimeout(reconnect, 1000);
// }
// );
// };
proxy.onDisconnected(() => {
if (!proxy.isClosed) {
log.warn('language server disconnected, reconnecting');
setTimeout(reconnect, 1000);
log.info('waiting language server to be connected');
if (!this.isRunning) {
log.error('detect language server killed, respawn ts server.');
child = spawnTs();
}
} else {
child.kill();
}
Expand Down

0 comments on commit 9d36039

Please sign in to comment.