Skip to content

Commit

Permalink
TSK-1152: Fix connections mess
Browse files Browse the repository at this point in the history
Signed-off-by: Andrey Sobolev <haiodo@gmail.com>
  • Loading branch information
haiodo committed Apr 13, 2023
1 parent 93dd448 commit f0f5871
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
26 changes: 17 additions & 9 deletions plugins/client-resources/src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// limitations under the License.
//

import client, { ClientSocket } from '@hcengineering/client'
import client, { ClientSocket, ClientSocketReadyState } from '@hcengineering/client'
import core, {
Class,
ClientConnection,
Expand All @@ -24,27 +24,28 @@ import core, {
Domain,
FindOptions,
FindResult,
generateId,
Ref,
Tx,
TxApplyIf,
TxHandler,
TxResult,
TxWorkspaceEvent,
WorkspaceEvent
WorkspaceEvent,
generateId
} from '@hcengineering/core'
import {
getMetadata,
PlatformError,
readResponse,
ReqId,
serialize,
UNAUTHORIZED,
getMetadata,
readResponse,
serialize,
unknownError
} from '@hcengineering/platform'

const SECOND = 1000
const pingTimeout = 10 * SECOND
const hangTimeout = 5 * 60 * SECOND
const dialTimeout = 20 * SECOND

class RequestPromise {
Expand Down Expand Up @@ -81,7 +82,7 @@ class Connection implements ClientConnection {
this.interval = setInterval(() => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises

if (this.pingResponse !== 0 && Date.now() - this.pingResponse > 3 * pingTimeout) {
if (this.pingResponse !== 0 && Date.now() - this.pingResponse > hangTimeout) {
// No ping response from server.
const s = this.websocket

Expand Down Expand Up @@ -116,12 +117,19 @@ class Connection implements ClientConnection {
}

delay = 1
pending: Promise<ClientSocket> | undefined

private async waitOpenConnection (): Promise<ClientSocket> {
while (true) {
try {
const conn = await this.openConnection()
const socket = await this.pending
if (socket != null && socket.readyState === ClientSocketReadyState.OPEN) {
return socket
}
this.pending = this.openConnection()
await this.pending
this.delay = 5
return conn
return await this.pending
} catch (err: any) {
console.log('failed to connect', err)
if (err?.code === UNAUTHORIZED.code) {
Expand Down
12 changes: 12 additions & 0 deletions plugins/client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ export interface ClientSocket {
send: (data: string | ArrayBufferLike | Blob | ArrayBufferView) => void

close: (code?: number) => void

readyState: ClientSocketReadyState
}

/**
* @public
*/
export enum ClientSocketReadyState {
CONNECTING = 0,
OPEN = 1,
CLOSING = 2,
CLOSED = 3
}

/**
Expand Down

0 comments on commit f0f5871

Please sign in to comment.