Skip to content

Commit

Permalink
bug: context switching on blocking socket operations
Browse files Browse the repository at this point in the history
  • Loading branch information
lost-illusi0n committed Nov 26, 2023
1 parent 22f4d3d commit 994dd33
Show file tree
Hide file tree
Showing 18 changed files with 33 additions and 14 deletions.
2 changes: 1 addition & 1 deletion mailserver/imap-agent/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ kotlin {
implementation(project(":utils"))
implementation("io.github.microutils:kotlin-logging:3.0.2")

implementation("dev.sitar:kio:1.1.1")
implementation("dev.sitar:kio:1.1.2")
implementation("dev.sitar:keystone:0.1-SNAPSHOT")
implementation("io.ktor:ktor-network:2.1.3")
implementation("io.ktor:ktor-network-tls:2.1.3")
Expand Down
2 changes: 1 addition & 1 deletion mailserver/imap/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ kotlin {
api(project(":sasl"))
implementation("io.github.microutils:kotlin-logging:3.0.2")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")
implementation("dev.sitar:kio:1.1.1")
implementation("dev.sitar:kio:1.1.2")
implementation("io.ktor:ktor-network:2.1.3")
}
}
2 changes: 1 addition & 1 deletion mailserver/message-format/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ kotlin {

sourceSets["commonMain"].dependencies {
api("org.jetbrains.kotlinx:kotlinx-datetime:0.4.0")
implementation("dev.sitar:kio:1.1.1")
implementation("dev.sitar:kio:1.1.2")
}

sourceSets["commonTest"].dependencies {
Expand Down
2 changes: 1 addition & 1 deletion mailserver/pop3-agent/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ kotlin {
implementation("io.github.microutils:kotlin-logging:3.0.2")

implementation("dev.sitar:keystone:0.1-SNAPSHOT")
implementation("dev.sitar:kio:1.1.1")
implementation("dev.sitar:kio:1.1.2")
implementation("io.ktor:ktor-network:2.1.3")
implementation("io.ktor:ktor-network-tls:2.1.3")
implementation("dev.sitar:dnskotlin:0.2.1")
Expand Down
2 changes: 1 addition & 1 deletion mailserver/pop3/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ kotlin {
implementation(project(":message-format"))
implementation("io.github.microutils:kotlin-logging:3.0.2")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")
implementation("dev.sitar:kio:1.1.1")
implementation("dev.sitar:kio:1.1.2")
implementation("io.ktor:ktor-network:2.1.3")
}
}
1 change: 1 addition & 0 deletions mailserver/runner/src/main/kotlin/imap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import dev.sitar.kmail.runner.storage.formats.MailboxFolder
import dev.sitar.kmail.runner.storage.formats.MailboxMessage
import dev.sitar.kmail.sasl.SaslChallenge
import dev.sitar.kmail.utils.server.ServerSocketFactory
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import kotlinx.datetime.Clock
Expand Down
6 changes: 6 additions & 0 deletions mailserver/runner/src/main/kotlin/launcher.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package dev.sitar.kmail.runner

import dev.sitar.kmail.imap.agent.ImapAgent
import dev.sitar.kmail.imap.agent.ImapLayer
import dev.sitar.kmail.imap.agent.ImapMailbox
import dev.sitar.kmail.imap.agent.ImapServer
import dev.sitar.kmail.imap.agent.transports.ImapServerTransport
import dev.sitar.kmail.runner.storage.mailbox
import dev.sitar.kmail.sasl.SaslChallenge
import dev.sitar.kmail.smtp.InternetMessage
import dev.sitar.kmail.utils.connection.ConnectionFactory
import dev.sitar.kmail.utils.server.ServerSocketFactory
Expand Down
2 changes: 1 addition & 1 deletion mailserver/runner/src/main/kotlin/launcherJvm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import dev.sitar.kmail.utils.connection.TlsCapableConnectionFactory
import dev.sitar.kmail.utils.server.TlsCapableServerSocketFactory
import kotlinx.coroutines.coroutineScope

suspend fun main(): Unit = coroutineScope {
suspend fun main() {
dns()

// System.setProperty("javax.net.debug", "all")
Expand Down
1 change: 1 addition & 0 deletions mailserver/runner/src/main/kotlin/pop3.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import dev.sitar.kmail.runner.storage.formats.Mailbox
import dev.sitar.kmail.runner.storage.formats.MailboxMessage
import dev.sitar.kmail.runner.storage.StorageLayer
import dev.sitar.kmail.utils.server.ServerSocketFactory
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import mu.KotlinLogging
Expand Down
1 change: 1 addition & 0 deletions mailserver/runner/src/main/kotlin/transfer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import dev.sitar.kmail.agents.smtp.transfer.*
import dev.sitar.kmail.smtp.InternetMessage
import dev.sitar.kmail.utils.connection.ConnectionFactory
import dev.sitar.kmail.utils.server.ServerSocketFactory
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
Expand Down
2 changes: 1 addition & 1 deletion mailserver/sasl/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ kotlin {

sourceSets["commonMain"].dependencies {
api("org.jetbrains.kotlinx:kotlinx-datetime:0.4.0")
implementation("dev.sitar:kio:1.1.1")
implementation("dev.sitar:kio:1.1.2")
}
}
1 change: 0 additions & 1 deletion mailserver/smtp-agent/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ kotlin {
implementation("io.github.microutils:kotlin-logging:3.0.2")

implementation("dev.sitar:keystone:0.1-SNAPSHOT")
implementation("dev.sitar:kio:1.1.1")
implementation("io.ktor:ktor-network:2.1.3")
implementation("io.ktor:ktor-network-tls:2.1.3")
implementation("dev.sitar:dnskotlin:0.2.1")
Expand Down
2 changes: 1 addition & 1 deletion mailserver/smtp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ kotlin {
sourceSets["commonMain"].dependencies {
api(project(":message-format"))
api(project(":utils"))
api("dev.sitar:kio:1.1.1")
api("dev.sitar:kio:1.1.2")
api("dev.sitar:dnskotlin:0.2.1")
implementation("io.ktor:ktor-utils:2.2.4")
}
Expand Down
2 changes: 1 addition & 1 deletion mailserver/utils/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ kotlin {
sourceSets["commonMain"].dependencies {
implementation("io.github.microutils:kotlin-logging:3.0.2")

api("dev.sitar:kio:1.1.1")
api("dev.sitar:kio:1.1.2")
api("io.ktor:ktor-io:2.2.4")
api("io.ktor:ktor-network:2.1.3")
api("io.ktor:ktor-network-tls:2.1.3")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import dev.sitar.kio.Pool
import dev.sitar.kio.Slice
import dev.sitar.kio.buffers.Buffer
import dev.sitar.kio.buffers.DefaultBufferPool
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.OutputStream

class OutputStreamAsyncWriterStream(val outputStream: OutputStream) : AsyncWriterStream {
Expand All @@ -14,7 +16,7 @@ class OutputStreamAsyncWriterStream(val outputStream: OutputStream) : AsyncWrite

override suspend fun writeBytes(slice: Slice): Int {
val (arr, off, len) = slice
outputStream.write(arr, off, len)
withContext(Dispatchers.IO) { outputStream.write(arr, off, len) }
return len
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package dev.sitar.kmail.utils.connection

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import javax.net.SocketFactory
import javax.net.ssl.SSLContext

actual class TlsCapableConnectionFactory(val sslContext: SSLContext) : ConnectionFactory {
override suspend fun connect(host: String, port: Int): Connection {
val socket = SocketFactory.getDefault().createSocket(host, port)
val socket = withContext(Dispatchers.IO) {
SocketFactory.getDefault().createSocket(host, port)
}
return TlsCapableConnection(socket, sslContext, isSecure = false)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package dev.sitar.kmail.utils.server

import dev.sitar.kmail.utils.connection.Connection
import dev.sitar.kmail.utils.connection.TlsCapableConnection
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import javax.net.ssl.SSLContext

actual class TlsCapableServerSocket(val socket: java.net.ServerSocket, val sslContext: SSLContext) : ServerSocket {
override suspend fun accept(): Connection {
return TlsCapableConnection(socket.accept(), sslContext, isSecure = false)
val socket = withContext(Dispatchers.IO) { socket.accept() }
return TlsCapableConnection(socket, sslContext, isSecure = false)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dev.sitar.kmail.utils.server

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import mu.KotlinLogging
import javax.net.ssl.SSLContext

Expand All @@ -8,7 +10,7 @@ private val logger = KotlinLogging.logger { }
actual class TlsCapableServerSocketFactory(val sslContext: SSLContext) : ServerSocketFactory {
override suspend fun bind(port: Int): ServerSocket {
logger.debug { "Creating a secure server socket on port $port." }
val serverSocket = javax.net.ServerSocketFactory.getDefault().createServerSocket(port)
val serverSocket = withContext(Dispatchers.IO) { javax.net.ServerSocketFactory.getDefault().createServerSocket(port) }
logger.debug { "Created a secure server socket on $port." }
return TlsCapableServerSocket(serverSocket, sslContext)
}
Expand Down

0 comments on commit 994dd33

Please sign in to comment.