Skip to content

Commit

Permalink
Merge pull request #116 from kordlib/changes/0.7.x/reconnect-reset
Browse files Browse the repository at this point in the history
Move reconnect retry to handshake handler
  • Loading branch information
BartArys authored Nov 23, 2020
2 parents bbbc2c3 + d96c1f8 commit 70cb4d9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 23 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@

* Fixed Color bug when converting from java.awt.Color with alpha. #114

## Changes

* Gateway reconnect retries reset on handshake. #68

# 0.7.0-M1

## Changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@ import com.gitlab.kordlib.common.ratelimit.RateLimiter
import com.gitlab.kordlib.gateway.GatewayCloseCode.*
import com.gitlab.kordlib.gateway.handler.*
import com.gitlab.kordlib.gateway.retry.Retry
import io.ktor.client.HttpClient
import io.ktor.client.features.websocket.DefaultClientWebSocketSession
import io.ktor.client.features.websocket.webSocketSession
import io.ktor.client.request.url
import io.ktor.http.URLBuilder
import io.ktor.http.cio.websocket.CloseReason
import io.ktor.http.cio.websocket.Frame
import io.ktor.http.cio.websocket.close
import io.ktor.util.error
import io.ktor.client.*
import io.ktor.client.features.websocket.*
import io.ktor.client.request.*
import io.ktor.http.*
import io.ktor.http.cio.websocket.*
import io.ktor.util.*
import kotlinx.atomicfu.AtomicRef
import kotlinx.atomicfu.atomic
import kotlinx.atomicfu.update
Expand Down Expand Up @@ -91,7 +88,7 @@ class DefaultGateway(private val data: DefaultGatewayData) : Gateway {
init {
val sequence = Sequence()
SequenceHandler(events, sequence)
handshakeHandler = HandshakeHandler(events, ::trySend, sequence, data.identifyRateLimiter)
handshakeHandler = HandshakeHandler(events, ::trySend, sequence, data.identifyRateLimiter, data.reconnectRetry)
HeartbeatHandler(events, ::trySend, { restart(Close.ZombieConnection) }, { _ping.value = it }, sequence)
ReconnectHandler(events) { restart(Close.Reconnecting) }
InvalidSessionHandler(events) { restart(it) }
Expand Down Expand Up @@ -122,7 +119,6 @@ class DefaultGateway(private val data: DefaultGatewayData) : Gateway {

try {
readSocket()
data.reconnectRetry.reset() //connected and read without problems, resetting retry counter
} catch (exception: Exception) {
defaultGatewayLogger.error(exception)
}
Expand Down Expand Up @@ -277,8 +273,7 @@ class DefaultGateway(private val data: DefaultGatewayData) : Gateway {
val copy = command.copy(token = "token")
"Gateway >>> ${Json.encodeToString(Command.Companion, copy)}"
}
}
else defaultGatewayLogger.trace { "Gateway >>> $json" }
} else defaultGatewayLogger.trace { "Gateway >>> $json" }
socket.send(Frame.Text(json))
}

Expand All @@ -294,15 +289,16 @@ class DefaultGateway(private val data: DefaultGatewayData) : Gateway {
}
}

internal val GatewayConfiguration.identify get() = Identify(
token,
IdentifyProperties(os, name, name),
false.optional(),
50.optionalInt(),
shard.optional(),
presence,
intents
)
internal val GatewayConfiguration.identify
get() = Identify(
token,
IdentifyProperties(os, name, name),
false.optional(),
50.optionalInt(),
shard.optional(),
presence,
intents
)


internal val os: String get() = System.getProperty("os.name")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.gitlab.kordlib.gateway.handler
import com.gitlab.kordlib.common.ratelimit.RateLimiter
import com.gitlab.kordlib.common.ratelimit.consume
import com.gitlab.kordlib.gateway.*
import com.gitlab.kordlib.gateway.retry.Retry
import kotlinx.atomicfu.AtomicRef
import kotlinx.atomicfu.atomic
import kotlinx.atomicfu.update
Expand All @@ -12,7 +13,8 @@ internal class HandshakeHandler(
flow: Flow<Event>,
private val send: suspend (Command) -> Unit,
private val sequence: Sequence,
private val identifyRateLimiter: RateLimiter
private val identifyRateLimiter: RateLimiter,
private val reconnectRetry: Retry
) : Handler(flow, "HandshakeHandler") {

lateinit var configuration: GatewayConfiguration
Expand All @@ -33,6 +35,7 @@ internal class HandshakeHandler(
}

on<Hello> {
reconnectRetry.reset() //connected and read without problems, resetting retry counter
identifyRateLimiter.consume {
if (sessionStart) send(identify)
else send(resume)
Expand Down

0 comments on commit 70cb4d9

Please sign in to comment.