Skip to content

Commit

Permalink
fix: Handle socket timeout (#306)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdsantos authored Oct 11, 2023
1 parent 81eb2e0 commit 6274d53
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/main/kotlin/tech/relaycorp/poweb/PoWebClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ import tech.relaycorp.relaynet.messages.control.PrivateNodeRegistrationRequest
import tech.relaycorp.relaynet.wrappers.x509.Certificate
import java.io.EOFException
import java.io.IOException
import java.net.SocketTimeoutException
import java.net.UnknownHostException
import java.security.MessageDigest
import java.security.PublicKey
import java.time.Duration
import java.util.logging.Logger
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.seconds
import kotlin.time.ExperimentalTime

Expand Down Expand Up @@ -319,6 +321,9 @@ public class PoWebClient internal constructor(
// E.g., the Internet connection was lost or the network changed.
logger.info { "WebSocket connection ended abruptly (${exc.message}). Will retry." }
delay(ABRUPT_DISCONNECT_RETRY_DELAY)
} catch (exc: SocketTimeoutException) {
logger.info { "WebSocket connection timed out (${exc.message}). Will retry." }
delay(TIMEOUT_RETRY_DELAY)
} catch (exc: IOException) {
throw ServerConnectionException("Server is unreachable", exc)
}
Expand All @@ -341,7 +346,9 @@ public class PoWebClient internal constructor(
private val PNR_CONTENT_TYPE = ContentType.parse(ContentTypes.NODE_REGISTRATION.value)

private val PING_INTERVAL = Duration.ofSeconds(5)

private val ABRUPT_DISCONNECT_RETRY_DELAY = 3.seconds
private val TIMEOUT_RETRY_DELAY = 500.milliseconds

/**
* Connect to a private gateway from a private endpoint.
Expand Down
23 changes: 23 additions & 0 deletions src/test/kotlin/tech/relaycorp/poweb/PoWebClientTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ import java.io.EOFException
import java.net.ConnectException
import java.net.ProtocolException
import java.net.SocketException
import java.net.SocketTimeoutException
import java.net.UnknownHostException
import java.time.Duration
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNull
import kotlin.test.assertTrue
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.seconds
import kotlin.time.toJavaDuration

Expand Down Expand Up @@ -383,6 +385,27 @@ class PoWebClientTest {
assertTrue(delta <= expectedDelta)
}

@Test
fun `Client should reconnect if the connection timed-out`(): Unit = runBlocking {
val connection1 = CloseConnectionAction()
addServerConnection(connection1)
val connection2 = CloseConnectionAction()
addServerConnection(connection2)

var connectionLossReplicated = false
mockWSConnect {
if (!connectionLossReplicated) {
connectionLossReplicated = true
throw SocketTimeoutException("Timeout")
}
}

waitForConnectionClosure()
val expectedDelta = 500.milliseconds.toJavaDuration()
val delta = Duration.between(connection2.runDate!!, connection1.runDate!!)
assertTrue(delta <= expectedDelta)
}

@Test
fun `Client should use WS if TLS is not required`() = runBlocking {
val client = PoWebClient(hostName, mockWebServer.port, false)
Expand Down

0 comments on commit 6274d53

Please sign in to comment.