Skip to content

Commit

Permalink
Replace long client timeout with retry count
Browse files Browse the repository at this point in the history
  • Loading branch information
toasterofbread committed May 12, 2024
1 parent a4bcc53 commit cb239a9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
19 changes: 12 additions & 7 deletions src/commonMain/kotlin/spms/server/SpMs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ import kotlin.system.exitProcess
import kotlin.system.getTimeMillis

const val SEND_EVENTS_TO_INSTIGATING_CLIENT: Boolean = true
private const val CLIENT_REPLY_TIMEOUT_MS: Long = 100

@OptIn(ExperimentalForeignApi::class)
class SpMs(
mem_scope: MemScope,
val headless: Boolean = false,
mem_scope: MemScope,
val headless: Boolean = false,
enable_gui: Boolean = false,
enable_media_session: Boolean = false
): ZmqRouter(mem_scope) {
Expand Down Expand Up @@ -86,7 +87,7 @@ class SpMs(
)
) + clients.map { it.info.copy(is_caller = it.id == caller) }

fun poll(client_reply_timeout_ms: Long) {
fun poll(client_reply_attempts: Int) {

// Process stray messages (hopefully client handshakes)
while (true) {
Expand Down Expand Up @@ -126,7 +127,7 @@ class SpMs(
}

// Wait for client to reply
val wait_end: Long = getTimeMillis() + client_reply_timeout_ms
val wait_end: Long = getTimeMillis() + CLIENT_REPLY_TIMEOUT_MS
var client_reply: Message? = null

while (true) {
Expand All @@ -150,10 +151,14 @@ class SpMs(

// Client did not reply to message within timeout
if (client_reply == null) {
onClientTimedOut(client, client_reply_timeout_ms)
if (++client.failed_connection_attempts >= client_reply_attempts) {
onClientTimedOut(client, client_reply_attempts)
}
continue
}

client.failed_connection_attempts = 0

check(executing_client_id == null)
executing_client_id = client.id

Expand Down Expand Up @@ -351,9 +356,9 @@ class SpMs(
}
}

private fun onClientTimedOut(client: SpMsClient, failed_timeout_ms: Long) {
private fun onClientTimedOut(client: SpMsClient, attempts: Int) {
clients.remove(client)
println("$client failed to respond within timeout (${failed_timeout_ms}ms)")
println("$client failed to respond after $attempts attempts")

if (
client.type == SpMsClientType.PLAYER
Expand Down
1 change: 1 addition & 0 deletions src/commonMain/kotlin/spms/server/SpMsClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ internal class SpMsClient(

val id: SpMsClientID = id_bytes.contentHashCode()
var ready_to_play: Boolean = false
var failed_connection_attempts: Int = 0

override fun toString(): String =
"Client(id=$id, name=$name, type=$type, language=$language, event_head=$event_head)"
Expand Down
4 changes: 2 additions & 2 deletions src/commonMain/kotlin/spms/server/SpMsCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const val BUG_REPORT_URL: String = PROJECT_URL + "/issues"

const val DEFAULT_ADDRESS: String = "127.0.0.1"
private const val POLL_INTERVAL_MS: Long = 100
private const val CLIENT_REPLY_TIMEOUT_MS: Long = 10000
private const val CLIENT_REPLY_ATTEMPTS: Int = 10

@Suppress("OPT_IN_USAGE")
@OptIn(ExperimentalForeignApi::class)
Expand Down Expand Up @@ -151,7 +151,7 @@ class SpMsCommand: Command(

println("--- ${localisation.server.polling_started} ---")
while (!stop) {
server.poll(CLIENT_REPLY_TIMEOUT_MS)
server.poll(CLIENT_REPLY_ATTEMPTS)
delay(POLL_INTERVAL_MS)
}
println("--- ${localisation.server.polling_ended} ---")
Expand Down

0 comments on commit cb239a9

Please sign in to comment.