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

fix: Improve connection state checks speed #720

Merged
merged 2 commits into from
Nov 16, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ import io.ktor.client.request.head
import io.ktor.network.selector.ActorSelectorManager
import io.ktor.network.sockets.aSocket
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.TimeoutCancellationException
import kotlinx.coroutines.withTimeout
import tech.relaycorp.gateway.BuildConfig
import tech.relaycorp.gateway.common.Logging.logger
import java.io.IOException
import java.util.logging.Level
import javax.inject.Inject
import kotlin.time.Duration.Companion.seconds

class PingRemoteServer
@Inject constructor() {
Expand All @@ -29,7 +32,9 @@ class PingRemoteServer
try {
aSocket(ActorSelectorManager(Dispatchers.IO))
.tcp()
.connect(address, port)
.connect(address, port) {
socketTimeout = TIMEOUT.inWholeMilliseconds
}
.use { true }
} catch (e: IOException) {
logger.log(Level.INFO, "Could not ping $address:$port")
Expand All @@ -38,16 +43,25 @@ class PingRemoteServer

suspend fun pingURL(url: String) =
try {
ktorClient.head<Unit>(url)
true
withTimeout(TIMEOUT) {
ktorClient.head<Unit>(url)
true
}
} catch (e: IOException) {
logger.log(Level.INFO, "Could not ping $url (${e.message})")
false
} catch (e: TimeoutCancellationException) {
logger.log(Level.INFO, "Could not ping $url (${e.message})")
false
} catch (e: ResponseException) {
logger.log(
Level.INFO,
"Successfully pinged $url but got a response exception (${e.message})"
)
true
}

companion object {
private val TIMEOUT = 5.seconds
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package tech.relaycorp.gateway.data.doh

import kotlinx.coroutines.TimeoutCancellationException
import kotlinx.coroutines.withTimeout
import tech.relaycorp.doh.DoHClient
import tech.relaycorp.doh.LookupFailureException
import tech.relaycorp.gateway.data.model.ServiceAddress
import javax.inject.Inject
import kotlin.time.Duration.Companion.seconds

class ResolveServiceAddress
@Inject constructor(
Expand All @@ -13,12 +16,19 @@ class ResolveServiceAddress
suspend fun resolvePoWeb(address: String): ServiceAddress {
val srvRecordName = "_awala-gsc._tcp.$address"
val answer = try {
doHClient.lookUp(srvRecordName, "SRV")
withTimeout(5.seconds) {
doHClient.lookUp(srvRecordName, "SRV")
}
} catch (exc: LookupFailureException) {
throw InternetAddressResolutionException(
"Failed to resolve DNS for PoWeb address",
exc
)
} catch (exc: TimeoutCancellationException) {
throw InternetAddressResolutionException(
"Failed to resolve DNS for PoWeb address",
exc
)
}
val srvRecordData = answer.data.first()
val srvRecordDataFields = srvRecordData.split(" ")
Expand Down
Loading