Skip to content

Commit

Permalink
[FIX] Connect was synchronous
Browse files Browse the repository at this point in the history
 - Blocking threads in Akka actor system is a baaad practice
 - False measurement of connect timings as "connect" was a noop
  • Loading branch information
chibenwa committed Apr 12, 2023
1 parent 222a197 commit a284480
Showing 1 changed file with 27 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import java.util.function.Consumer
import akka.actor.{Props, Stash}
import com.linagora.gatling.imap.protocol.command._
import com.yahoo.imapnio.async.client.ImapAsyncSession.DebugMode
import com.yahoo.imapnio.async.client.{ImapAsyncClient, ImapAsyncSession, ImapAsyncSessionConfig}
import com.yahoo.imapnio.async.client.{ImapAsyncClient, ImapAsyncSession, ImapAsyncSessionConfig, ImapFuture}
import com.yahoo.imapnio.async.internal.ImapAsyncSessionImpl
import io.gatling.core.akka.BaseActor
import io.gatling.core.util.NameGen
import javax.net.ssl.SSLContext
Expand Down Expand Up @@ -57,27 +58,37 @@ private object ImapSession {
private class ImapSession(client: => ImapAsyncClient, protocol: ImapProtocol) extends BaseActor with Stash with NameGen {
val uri = new URI(s"${protocol.protocol}://${protocol.host}:${protocol.port}")
val config: Properties = protocol.config
logger.debug(s"connecting to $uri with $config")
val session: ImapAsyncSession = {
val config = new ImapAsyncSessionConfig
config.setConnectionTimeoutMillis(50000)
config.setReadTimeoutMillis(60000)
val sniNames = null

val localAddress = null
client
.createSession(uri, config, localAddress, sniNames, DebugMode.DEBUG_OFF, "ImapSession", ImapSession.sslContext)
.get()
.getSession
}
var session: ImapAsyncSession = null

override def receive: Receive = disconnected

def disconnected: Receive = {
case Command.Connect(userId) =>
logger.debug(s"got connect request, $userId connecting to $uri")
context.become(connected)
sender() ! Response.Connected(ImapResponses.empty)

val config = new ImapAsyncSessionConfig
config.setConnectionTimeoutMillis(50000)
config.setReadTimeoutMillis(60000)
val sniNames = null
val localAddress = null

val future: ImapFuture[ImapAsyncSessionImpl] = client
.createSession(uri, config, localAddress, sniNames, DebugMode.DEBUG_OFF, "ImapSession", ImapSession.sslContext)
.asInstanceOf[ImapFuture[ImapAsyncSessionImpl]]

future.setDoneCallback(s => {
session = s
context.become(connected)
sender() ! Response.Connected(ImapResponses.empty)
})
val errorCallback: Consumer[Exception] = e => {
logger.error(s"${getClass.getSimpleName} connection failed")
sender ! e
context.stop(self)
}

future.setExceptionCallback(errorCallback)

case Response.Disconnected(_) => ()
case Command.Disconnect(_) => ()
case msg =>
Expand Down

0 comments on commit a284480

Please sign in to comment.