Skip to content

Commit 1815a65

Browse files
committed
(GH-240) Add TCP Retry to client
This commit adds retry functionality to the languageclient when connecting to the languagserver over TCP. It will try 5 times while waiting 5 times between attempts.
1 parent d4bbd72 commit 1815a65

File tree

2 files changed

+54
-7
lines changed

2 files changed

+54
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
2121
- (maint) Automatically download languageserver on build and watch
2222
- (maint) Improve issue templates
2323
- ([GH-241](https://github.com/lingua-pupuli/puppet-vscode/issues/241)) Honor specified tcp port
24+
- ([GH-240](https://github.com/lingua-pupuli/puppet-vscode/issues/240)) Add TCP retry functionality
2425

2526
## 0.10.0 - 2018-03-29
2627

src/connection.ts

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -228,19 +228,65 @@ export class ConnectionManager implements IConnectionManager {
228228
): ServerOptions {
229229
let serverOptions: ServerOptions = function() {
230230
return new Promise((resolve, reject) => {
231+
const retries = 5;
232+
var attempt = 0;
231233
var client = new net.Socket();
234+
235+
const rconnect = () => { client.connect(port, address) };
236+
232237
client.connect(port, address, function() {
233-
logger.error(`[Puppet Lang Server Client] tcp connected`);
238+
logger.debug(`[Puppet Lang Server Client] tcp connected`);
234239
resolve({ reader: client, writer: client });
235240
});
241+
236242
client.on('error', function(err) {
237-
logger.error(`[Puppet Lang Server Client] ` + err);
238-
connectionManager.setConnectionStatus(
239-
`Could not start language client: ${err.message}`,
240-
ConnectionStatus.Failed
241-
);
242-
return null;
243+
244+
if(attempt === retries){
245+
logger.error(`[Puppet Lang Server Client] ` + `Could not start language client: ${err.message}`);
246+
connectionManager.setConnectionStatus(
247+
`Could not start language client: ${err.message}`,
248+
ConnectionStatus.Failed
249+
);
250+
251+
vscode.window.showErrorMessage(
252+
`Could not start language client: ${err.message}. Please click 'Troubleshooting Information' for resolution steps`,
253+
{ modal: false },
254+
{ title: 'Troubleshooting Information' }
255+
).then((item)=>{
256+
if (item === undefined){
257+
return;
258+
}
259+
if(item.title === 'Troubleshooting Information'){
260+
vscode.commands.executeCommand(
261+
'vscode.open',
262+
vscode.Uri.parse('https://github.com/lingua-pupuli/puppet-vscode#experience-a-problem')
263+
);
264+
}
265+
}
266+
);
267+
268+
return null;
269+
}else{
270+
attempt = attempt + 1;
271+
var message = `Timed out connecting to language server. Is the server running at ${address}:${port} ? Will wait timeout value before trying again`;
272+
switch(err.code){
273+
case 'ETIMEDOUT':
274+
message = `Timed out connecting to language server. Is the server running at ${address}:${port} ? Will wait timeout value before trying again`;
275+
break;
276+
case 'ECONNREFUSED':
277+
message = `Connect refused to language server. Is the server running at ${address}:${port} ? Will wait for 5 seconds before trying again`;
278+
break;
279+
default:
280+
message = `Connect refused to language server. Is the server running at ${address}:${port} ? Will wait for 5 seconds before trying again`;
281+
break;
282+
}
283+
vscode.window.showWarningMessage(message);
284+
logger.warning(message);
285+
setTimeout(rconnect, 5000);
286+
}
287+
243288
});
289+
244290
});
245291
};
246292
return serverOptions;

0 commit comments

Comments
 (0)