Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move reconnect retry to handshake handler #116

Merged
merged 1 commit into from
Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
* Re-added `values` in `Intent` and `Intents`.
* Added support for inline replies and `MessageBehavior#reply` to quickly create a reply to a message. #110

## 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