Skip to content

Commit

Permalink
Improve reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
Tibo-lg committed Oct 26, 2020
1 parent 2f4e3ed commit 02aa1d7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
2 changes: 2 additions & 0 deletions integration-test/run-separate/dlcManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,13 @@ describe('dlc-manager', () => {
getDlcMessageStream: () => getDlcMessageStreamMock(localStream),
sendDlcMessage: (message: DlcTypedMessage, dest: string) =>
sendDlcMessageMock(message, dest, localParty, remoteStream),
refreshAuth: () => Promise.resolve(),
}
const remoteDlcMessageService: DlcMessageServiceApi = {
getDlcMessageStream: () => getDlcMessageStreamMock(remoteStream),
sendDlcMessage: (message: DlcTypedMessage, dest: string) =>
sendDlcMessageMock(message, dest, remoteParty, localStream),
refreshAuth: () => Promise.resolve(),
}
const logger = winston.createLogger({
level: 'debug',
Expand Down
5 changes: 5 additions & 0 deletions src/browser/api/grpc/DlcMessageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { AuthenticationService } from './AuthenticationService'
export interface DlcMessageServiceApi {
sendDlcMessage(message: DlcTypedMessage, dest: string): Promise<void>
getDlcMessageStream(): DlcMessageStream
refreshAuth(): Promise<void>
}

export class DlcMessageStream {
Expand Down Expand Up @@ -103,4 +104,8 @@ export class DlcMessageService implements DlcMessageService {
grpcStream.cancel()
)
}

public refreshAuth(): Promise<void> {
return this._authService.refresh()
}
}
26 changes: 22 additions & 4 deletions src/browser/dlc/controller/DlcManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import {
import { DlcService } from '../service/DlcService'
import { DlcError, DlcEventHandler } from '../utils/DlcEventHandler'

const MaxDelayMs = 5000

export class DlcManager {
private readonly timeoutHandle: NodeJS.Timeout
private readonly mutex: Mutex
Expand Down Expand Up @@ -64,18 +66,34 @@ export class DlcManager {
this.dlcMessageStream.cancel()
}

private sleep(delay: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, delay))
}

private async listenToStream(): Promise<void> {
let delay = 20
while (!this.isFinalized) {
try {
for await (const message of this.dlcMessageStream.listen()) {
delay = 20
this.onDlcMessage(message)
}
} catch (error) {
// TODO(tibo): should check if refresh token is still valid, if not,
// need to redirect to login screen.
this.logger.error('Stream stopped with error', { error })
if (!this.isFinalized) {
// TODO(tibo): Better handling. If this throws, server might be
// unreachable, need to propagate error to user.
this.dlcMessageStream = this.dlcMessageService.getDlcMessageStream()
let isConnected = false
while (!isConnected) {
this.logger.info('Retrying in ' + delay + 'ms')
await this.sleep(delay)
delay = Math.min(MaxDelayMs, delay * 2)
try {
this.dlcMessageService.refreshAuth()
this.dlcMessageStream = this.dlcMessageService.getDlcMessageStream()
isConnected = true
} catch {
this.logger.error('Could not get dlc message stream.')
}
}
}
}
Expand Down

0 comments on commit 02aa1d7

Please sign in to comment.