-
Notifications
You must be signed in to change notification settings - Fork 30
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
GLSP-77: Allow WebSocket connections to reconnect after interrupt #269
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
packages/protocol/src/client-server-protocol/jsonrpc/ws-connection-provider.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/******************************************************************************** | ||
* Copyright (c) 2023 EclipseSource 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 { Logger, MessageConnection } from 'vscode-jsonrpc'; | ||
import { MaybePromise } from '../../utils/type-util'; | ||
import { createWebSocketConnection, wrap } from './websocket-connection'; | ||
|
||
export interface GLSPWebSocketOptions { | ||
/** | ||
* Allow automatic reconnect of WebSocket connections | ||
* @default true | ||
*/ | ||
reconnecting?: boolean; | ||
/** | ||
* Max attempts of reconnects | ||
* @default Infinity | ||
*/ | ||
reconnectAttempts?: number; | ||
/** | ||
* The time delay in milliseconds between reconnect attempts | ||
* @default 1000 | ||
*/ | ||
reconnectDelay?: number; | ||
} | ||
|
||
export const GLSPConnectionHandler = Symbol('GLSPConnectionHandler'); | ||
export interface GLSPConnectionHandler { | ||
onConnection?(connection: MessageConnection): MaybePromise<void>; | ||
onReconnect?(connection: MessageConnection): MaybePromise<void>; | ||
logger?: Logger; | ||
} | ||
|
||
export class GLSPWebSocketProvider { | ||
protected webSocket: WebSocket; | ||
protected reconnectTimer: NodeJS.Timer; | ||
protected reconnectAttempts = 0; | ||
|
||
protected options: GLSPWebSocketOptions = { | ||
// default values | ||
reconnecting: true, | ||
reconnectAttempts: Infinity, | ||
reconnectDelay: 1000 | ||
}; | ||
|
||
constructor(protected url: string, options?: GLSPWebSocketOptions) { | ||
this.options = Object.assign(this.options, options); | ||
} | ||
|
||
protected createWebSocket(url: string): WebSocket { | ||
return new WebSocket(url); | ||
} | ||
|
||
listen(handler: GLSPConnectionHandler, isReconnecting = false): Promise<MessageConnection> { | ||
this.webSocket = this.createWebSocket(this.url); | ||
|
||
this.webSocket.onerror = (): void => { | ||
handler.logger?.error('GLSPWebSocketProvider Connection to server errored. Please make sure that the server is running!'); | ||
clearInterval(this.reconnectTimer); | ||
this.webSocket.close(); | ||
}; | ||
|
||
return new Promise(resolve => { | ||
this.webSocket.onopen = (): void => { | ||
clearInterval(this.reconnectTimer); | ||
const wrappedSocket = wrap(this.webSocket); | ||
const wsConnection = createWebSocketConnection(wrappedSocket, handler.logger); | ||
|
||
this.webSocket.onclose = (): void => { | ||
const { reconnecting, reconnectAttempts, reconnectDelay } = this.options; | ||
if (reconnecting) { | ||
if (this.reconnectAttempts >= reconnectAttempts!) { | ||
handler.logger?.error( | ||
'GLSPWebSocketProvider WebSocket reconnect failed - maximum number reconnect attempts ' + | ||
`(${reconnectAttempts}) was exceeded!` | ||
); | ||
} else { | ||
this.reconnectTimer = setInterval(() => { | ||
handler.logger?.warn('GLSPWebSocketProvider reconnecting...'); | ||
this.listen(handler, true); | ||
this.reconnectAttempts++; | ||
}, reconnectDelay!); | ||
} | ||
} else { | ||
handler.logger?.error('GLSPWebSocketProvider WebSocket will not reconnect - closing the connection now!'); | ||
} | ||
}; | ||
|
||
if (isReconnecting) { | ||
handler.logger?.warn('GLSPWebSocketProvider Reconnecting!'); | ||
handler.onReconnect?.(wsConnection); | ||
} else { | ||
handler.logger?.warn('GLSPWebSocketProvider Initializing!'); | ||
handler.onConnection?.(wsConnection); | ||
} | ||
resolve(wsConnection); | ||
}; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Offtopic: Dispatching
ServerStatusActions
orServerMessageActions
on the client side seems a bit weird.In general, these actions are just plain status or message notifications and don't have to necessarily come from the server. Maybe we should consider a rename here to a more generic
StatusAction
andMessageAction
or at the very least make it clear in the documentation that this actions can also be dispatched from the client side.@planger WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True, I'm in favor of a rename to
StatusAction
andMessageAction
. However, I don't consider this a high prio.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Me neither, nevertheless we should probably do it as part of the 2.0 Release. It' an API break after all, even if its just a minor one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good, I'll create an issue for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See eclipse-glsp/glsp#1071