Skip to content

Commit

Permalink
problem: reconnects WS too often when the only response is an error
Browse files Browse the repository at this point in the history
  • Loading branch information
splix committed Aug 25, 2022
1 parent 58524de commit 48d650b
Showing 1 changed file with 11 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ open class WsConnection(

private var reconnectBackoff: BackOff = ExponentialBackOff().also {
it.initialInterval = Duration.ofMillis(100).toMillis()
it.maxInterval = Duration.ofMinutes(1).toMillis()
it.maxInterval = Duration.ofMinutes(5).toMillis()
}
private var currentBackOff = reconnectBackoff.start()

Expand Down Expand Up @@ -225,18 +225,20 @@ open class WsConnection(
val consumer = inbound
.aggregateFrames(msgSizeLimit)
.receiveFrames()
.doOnNext {
if (!read) {
// restart backoff only after a successful read from the connection,
// otherwise it may restart it even if the connection is faulty
currentBackOff = reconnectBackoff.start()
read = true
}
}
.map { ByteBufInputStream(it.content()).readAllBytes() }
.flatMap {
try {
val msg = parser.parse(it)
if (!read) {
if (msg.error != null) {
log.warn("Received error ${msg.error.code} from $uri: ${msg.error.message}")
} else {
// restart backoff only after a successful read from the connection,
// otherwise it may restart it even if the connection is faulty or responds only with error
currentBackOff = reconnectBackoff.start()
read = true
}
}
if (msg.type == ResponseWSParser.Type.SUBSCRIPTION) {
onSubscription(msg)
} else {
Expand Down

0 comments on commit 48d650b

Please sign in to comment.