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

KTOR-7908 Add heartbeat to SSE #4543

Merged
merged 1 commit into from
Dec 12, 2024

Conversation

marychatte
Copy link
Member

Subsystem
Server, SSE

Motivation
KTOR-7908

@marychatte marychatte self-assigned this Dec 10, 2024
@marychatte marychatte force-pushed the marychatte/KTOR-7908-Add-heartbeat-to-SSE branch from 7fce919 to 32ed5b0 Compare December 10, 2024 15:13
@marychatte marychatte requested a review from bjhham December 11, 2024 12:11
Copy link
Contributor

@bjhham bjhham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple a minor things, looks nice 👍

Comment on lines 301 to 336
@Test
fun testHeartbeat() = testApplication {
install(SSE)
routing {
sse {
heartbeat {
duration = 100.milliseconds
event = ServerSentEvent("heartbeat")
}

send("Hello")
delay(200.milliseconds)
}
}

val client = createSseClient()
val events = mutableListOf<ServerSentEvent>()
client.sse {
incoming.collect { events.add(it) }
}
assertTrue { events.size > 1 }
assertTrue { events.filter { it.data == "Hello" }.size == 1 }
assertTrue { events.any { it.data == "heartbeat" } }
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe instead of using delays and asserts here, you could use some async processing and count a few of each to cover receiving multiple...

Something like:

    @Test
    fun testHeartbeat() = testApplication {
        install(SSE)
        routing {
            sse {
                heartbeat {
                    duration = 10.milliseconds
                    event = ServerSentEvent("heartbeat")
                }

                while(true) {
                    send("Hello")
                    delay(10.milliseconds)
                }
            }
        }

        val client = createSseClient()
        
        var hellos = 0
        var heartbeats = 0
        withTimeout(1_000) {
            client.sse {
                incoming.collect { event ->
                    when(event.data) {
                        "Hello" -> hellos++
                        "heartbeat" -> heartbeats++
                    }
                    if (hellos > 3 && heartbeats > 3)
                        cancel()
                }
            }
        }
    }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks better, thanks!

*/
public fun ServerSSESession.heartbeat(heartbeatConfig: Heartbeat.() -> Unit) {
val heartbeat = Heartbeat().apply(heartbeatConfig)
val heartbeatJob = Job()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use Job(call.coroutineContext[Job]) here to attach it to the call?

public fun ServerSSESession.heartbeat(heartbeatConfig: Heartbeat.() -> Unit) {
val heartbeat = Heartbeat().apply(heartbeatConfig)
val heartbeatJob = Job()
launch(heartbeatJob) {
Copy link
Member

@osipxd osipxd Dec 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wondering why we don't add a CoroutineName to launched coroutines? In theory, this would simplify further debugging. Both, for us and for users.
@marychatte, @bjhham, WDYT?

@osipxd osipxd force-pushed the 3.1.0-eap branch 2 times, most recently from f8ee92a to 2206098 Compare December 11, 2024 16:42
@marychatte marychatte force-pushed the marychatte/KTOR-7908-Add-heartbeat-to-SSE branch 3 times, most recently from bc4a92e to 5d495eb Compare December 11, 2024 18:01
@osipxd osipxd force-pushed the 3.1.0-eap branch 3 times, most recently from d50a66e to cc3f8f8 Compare December 12, 2024 10:34
@marychatte marychatte force-pushed the marychatte/KTOR-7908-Add-heartbeat-to-SSE branch from 5d495eb to e0e75db Compare December 12, 2024 13:30
@marychatte marychatte merged commit 78af9c7 into 3.1.0-eap Dec 12, 2024
14 of 16 checks passed
@marychatte marychatte deleted the marychatte/KTOR-7908-Add-heartbeat-to-SSE branch December 12, 2024 21:07
Copy link
Member

@osipxd osipxd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it's a bit late, but I left a coupe of comments 😅

*
* @param heartbeatConfig a lambda that configures the [Heartbeat] object used for the heartbeat.
*/
public fun ServerSSESession.heartbeat(heartbeatConfig: Heartbeat.() -> Unit) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add a default value here?

Suggested change
public fun ServerSSESession.heartbeat(heartbeatConfig: Heartbeat.() -> Unit) {
public fun ServerSSESession.heartbeat(heartbeatConfig: Heartbeat.() -> Unit = {}) {

* @property event the [ServerSentEvent] to be sent as the heartbeat, default is a [ServerSentEvent] with the comment "heartbeat".
*/
public class Heartbeat {
public var duration: Duration = 30.seconds
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably period would be better?

osipxd pushed a commit that referenced this pull request Dec 19, 2024
osipxd pushed a commit that referenced this pull request Dec 19, 2024
Stexxe pushed a commit that referenced this pull request Dec 24, 2024
e5l pushed a commit that referenced this pull request Jan 8, 2025
osipxd pushed a commit that referenced this pull request Jan 8, 2025
osipxd pushed a commit that referenced this pull request Jan 9, 2025
osipxd pushed a commit that referenced this pull request Jan 9, 2025
osipxd pushed a commit that referenced this pull request Jan 9, 2025
osipxd pushed a commit that referenced this pull request Jan 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants