send emails with blocking SMTP using distinct dispatcher

pull/8606/head
Thibault Duplessis 2021-04-08 09:51:51 +02:00
parent bbbdf86c7f
commit aaa73a878b
2 changed files with 23 additions and 22 deletions

View File

@ -69,6 +69,13 @@ akka {
}
}
}
blocking-smtp-dispatcher {
executor = "thread-pool-executor"
thread-pool-executor {
core-pool-size-min = 16
core-pool-size-max = 64
}
}
}
app {
renderer.name = "renderer"

View File

@ -9,20 +9,16 @@ import scala.concurrent.{ blocking, Future }
import scalatags.Text.all._
import lila.common.config.Secret
import lila.common.{ Chronometer, EmailAddress, ThreadLocalRandom }
import lila.common.String.html.{ escapeHtml, nl2brUnsafe }
import lila.common.{ Chronometer, EmailAddress, ThreadLocalRandom }
import lila.i18n.I18nKeys.{ emails => trans }
final class Mailer(
config: Mailer.Config,
getSecondaryPermille: () => Int
)(implicit
ec: scala.concurrent.ExecutionContext,
system: ActorSystem
) {
)(implicit system: ActorSystem) {
private val workQueue =
new lila.hub.DuctSequencer(maxSize = 512, timeout = Mailer.timeout * 2, name = "mailer")
implicit private val blockingExecutionContext = system.dispatchers.lookup("blocking-smtp-dispatcher")
private val primaryClient = new SMTPMailer(config.primary.toClientConfig)
private val secondaryClient = new SMTPMailer(config.secondary.toClientConfig)
@ -36,22 +32,20 @@ final class Mailer(
logger.warn(s"Can't send ${msg.subject} to noreply email ${msg.to}")
funit
} else
workQueue {
Future {
Chronometer.syncMon(_.email.send.time) {
blocking {
randomClient()
.send(
Email(
subject = msg.subject,
from = config.sender,
to = Seq(msg.to.value),
bodyText = msg.text.some,
bodyHtml = msg.htmlBody map { body => Mailer.html.wrap(msg.subject, body).render }
)
Future {
Chronometer.syncMon(_.email.send.time) {
blocking {
randomClient()
.send(
Email(
subject = msg.subject,
from = config.sender,
to = Seq(msg.to.value),
bodyText = msg.text.some,
bodyHtml = msg.htmlBody map { body => Mailer.html.wrap(msg.subject, body).render }
)
.unit
}
)
.unit
}
}
}