Skip to content

Commit 09beae6

Browse files
wip
1 parent fcc19c1 commit 09beae6

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

index.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use strict'
2+
const { setTimeout: wait } = require('node:timers/promises')
23
const From = require('@fastify/reply-from')
34
const { ServerResponse } = require('node:http')
45
const WebSocket = require('ws')
@@ -77,10 +78,32 @@ function proxyWebSockets (source, target) {
7778
/* c8 ignore stop */
7879
}
7980

80-
function reconnect (logger, source, options, targetParams) {
81+
async function reconnect (logger, source, options, targetParams) {
82+
// TODO retry, maxReconnectionRetries
8183
const { url, subprotocols, optionsWs } = targetParams
82-
const target = new WebSocket(url, subprotocols, optionsWs)
83-
logger.info({ target: targetParams.url }, 'proxy ws reconnected on broken connection')
84+
85+
let attempts = 0
86+
let target
87+
do {
88+
const reconnectWait = options.wsReconnect.reconnectInterval * options.wsReconnect.reconnectDecay
89+
logger.info({ target: targetParams.url, attempts }, `proxy ws reconnecting in ${reconnectWait} ms`)
90+
await wait(reconnectWait)
91+
target = new WebSocket(url, subprotocols, optionsWs)
92+
// TODO connectionTimeout
93+
if (!target) {
94+
attempts++
95+
continue
96+
}
97+
break
98+
} while (attempts < options.wsReconnect.maxReconnectAttempts)
99+
100+
if (!target) {
101+
logger.error({ target: targetParams.url }, 'proxy ws failed to reconnect')
102+
// TODO onError hook?
103+
return
104+
}
105+
106+
logger.info({ target: targetParams.url }, 'proxy ws reconnected')
84107
proxyWebSocketsWithReconnection(logger, source, target, options, targetParams)
85108
}
86109

@@ -96,6 +119,8 @@ function proxyWebSocketsWithReconnection (logger, source, target, options, targe
96119
return
97120
}
98121

122+
// TODO if reconnectOnClose
123+
99124
logger.info({ msg: 'proxy ws close link' })
100125
closeWebSocket(source, code, reason)
101126
closeWebSocket(target, code, reason)

src/options.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ function validateOptions (options) {
3333
}
3434
wsReconnect.maxReconnectionRetries = wsReconnect.maxReconnectionRetries ?? DEFAULT_MAX_RECONNECTION_RETRIES
3535

36-
if (wsReconnect.reconnectInterval !== undefined && (typeof wsReconnect.reconnectInterval !== 'number' || wsReconnect.reconnectInterval < 0)) {
37-
throw new Error('wsReconnect.reconnectInterval must be a non-negative number')
36+
if (wsReconnect.reconnectInterval !== undefined && (typeof wsReconnect.reconnectInterval !== 'number' || wsReconnect.reconnectInterval < 100)) {
37+
throw new Error('wsReconnect.reconnectInterval (ms) must be a number greater than or equal to 100')
3838
}
3939
wsReconnect.reconnectInterval = wsReconnect.reconnectInterval ?? DEFAULT_RECONNECT_INTERVAL
4040

0 commit comments

Comments
 (0)