-
Notifications
You must be signed in to change notification settings - Fork 82
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
Use gateway url provided in Ready
event for resuming
#666
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9493638
Add `resume_gateway_url` to models
lukellmann d1deb98
Use `resume_gateway_url` when resuming
lukellmann b7e4329
Use exhaustive `when`
lukellmann 7f33285
Merge branch '0.8.x' into feature/resume-gateway-url
lukellmann 41986d8
apiDump
lukellmann 2812afc
Merge branch '0.8.x' into feature/resume-gateway-url
lukellmann 9890d0c
Merge branch '0.8.x' into feature/resume-gateway-url
lukellmann 138f5f5
log gateway url
lukellmann fe4f0be
Merge branch '0.8.x' into feature/resume-gateway-url
lukellmann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,13 +4,13 @@ import dev.kord.common.ratelimit.RateLimiter | |
import dev.kord.common.ratelimit.consume | ||
import dev.kord.gateway.* | ||
import dev.kord.gateway.retry.Retry | ||
import kotlinx.atomicfu.AtomicRef | ||
import io.ktor.http.* | ||
import kotlinx.atomicfu.atomic | ||
import kotlinx.atomicfu.update | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
internal class HandshakeHandler( | ||
flow: Flow<Event>, | ||
private val initialUrl: Url, | ||
private val send: suspend (Command) -> Unit, | ||
private val sequence: Sequence, | ||
private val identifyRateLimiter: RateLimiter, | ||
|
@@ -19,31 +19,37 @@ internal class HandshakeHandler( | |
|
||
lateinit var configuration: GatewayConfiguration | ||
|
||
private val session: AtomicRef<String?> = atomic(null) | ||
// see https://discord.com/developers/docs/topics/gateway#resuming | ||
private class ResumeContext(val sessionId: String, val resumeUrl: Url) | ||
|
||
private val identify | ||
get() = configuration.identify | ||
private val resumeContext = atomic<ResumeContext?>(initial = null) | ||
val gatewayUrl get() = resumeContext.value?.resumeUrl ?: initialUrl | ||
|
||
private val resume | ||
get() = Resume(configuration.token, session.value!!, sequence.value ?: 0) | ||
|
||
private val sessionStart get() = session.value == null | ||
|
||
override fun start() { | ||
on<Ready> { event -> | ||
session.update { event.data.sessionId } | ||
private val resumeOrIdentify | ||
get() = when (val sessionId = resumeContext.value?.sessionId) { | ||
null -> configuration.identify | ||
else -> Resume(configuration.token, sessionId, sequence.value ?: 0) | ||
Comment on lines
+28
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
override fun start() { | ||
on<Hello> { | ||
reconnectRetry.reset() //connected and read without problems, resetting retry counter | ||
reconnectRetry.reset() // connected and read without problems, resetting retry counter | ||
identifyRateLimiter.consume { | ||
if (sessionStart) send(identify) | ||
else send(resume) | ||
send(resumeOrIdentify) | ||
} | ||
lukellmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
on<Ready> { event -> | ||
// keep custom query params | ||
val resumeUrl = URLBuilder(event.data.resumeGatewayUrl) | ||
.apply { parameters.appendMissing(initialUrl.parameters) } | ||
.build() | ||
|
||
resumeContext.value = ResumeContext(event.data.sessionId, resumeUrl) | ||
} | ||
|
||
on<Close.SessionReset> { | ||
session.update { null } | ||
resumeContext.value = null | ||
} | ||
lukellmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
non-exhaustive when statements over sealed types are an error now, so this is no longer needed