Skip to content

Commit

Permalink
Reduce WS connection rate
Browse files Browse the repository at this point in the history
  • Loading branch information
HitomaruKonpaku committed Dec 9, 2023
1 parent e42e7d1 commit 88dc0f8
Showing 1 changed file with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import { TwitCastingUserService } from '../data/twitcasting-user.service'
export class TwitCastingLiveTrackingService {
private readonly logger = baseLogger.child({ context: TwitCastingLiveTrackingService.name })

private closeCount = 0

constructor(
@Inject(ConfigService)
private readonly configService: ConfigService,
Expand Down Expand Up @@ -62,14 +64,27 @@ export class TwitCastingLiveTrackingService {
const url = this.twitCastingApiService.getRealtimeLivesUrl()
const socket = new WebSocket(url)
socket.on('error', (error) => this.logger.error(`[WS] error: ${error.message}`))
socket.on('open', () => this.logger.debug('[WS] open'))
socket.on('open', () => this.onWsOpen())
socket.on('close', () => this.onWsClose())
socket.on('message', (payload) => this.onWsMessage(String(payload)))
}

private onWsOpen() {
this.logger.debug('[WS] open')
this.closeCount = 0
}

private onWsClose() {
this.logger.debug('[WS] close')
this.initRealtimeApi()

const ms = ([1, 5, 10, 20, 30, 45][this.closeCount] || 60) * 1000
this.logger.debug(`[WS] retry in ${ms}ms`)

setTimeout(() => {
this.initRealtimeApi()
}, ms)

this.closeCount += 1
}

private onWsMessage(payload: string) {
Expand Down

0 comments on commit 88dc0f8

Please sign in to comment.