diff --git a/otoroshi/app/utils/letsencrypt.scala b/otoroshi/app/utils/letsencrypt.scala index 6e2547b2f..c96fd3be2 100644 --- a/otoroshi/app/utils/letsencrypt.scala +++ b/otoroshi/app/utils/letsencrypt.scala @@ -12,6 +12,7 @@ import otoroshi.events.{Alerts, CertRenewalAlert} import otoroshi.ssl.DynamicSSLEngineProvider.base64Decode import otoroshi.ssl.{Cert, PemHeaders} import otoroshi.utils.RegexPool +import otoroshi.utils.syntax.implicits.BetterFiniteDuration import play.api.Logger import play.api.libs.json._ @@ -309,8 +310,11 @@ object LetsEncryptHelper { s"${env.storageRoot}:letsencrypt:challenges:$domain:${challenge.getToken}", ByteString(challenge.getAuthorization), Some(10.minutes.toMillis) - ) - authorizeOrder(domain, authorization.getStatus, challenge) + ).flatMap { _ => + 3.seconds.timeout.flatMap { _ => + authorizeOrder(domain, authorization.getStatus, challenge) + } + } } .toMat(Sink.seq)(Keep.right) .run() @@ -333,7 +337,7 @@ object LetsEncryptHelper { } else { challenge.trigger() Source - .tick(1.seconds, 3.seconds, ()) + .tick(3.seconds, 3.seconds, ()) .mapAsync(1) { _ => Future { challenge.update() @@ -364,7 +368,7 @@ object LetsEncryptHelper { order.execute(csr) }(blockingEc).flatMap { _ => Source - .tick(1.seconds, 5.seconds, ()) + .tick(3.seconds, 5.seconds, ()) .mapAsync(1) { _ => Future { order.update() diff --git a/otoroshi/app/utils/syntax.scala b/otoroshi/app/utils/syntax.scala index 129da2f39..5245b865c 100644 --- a/otoroshi/app/utils/syntax.scala +++ b/otoroshi/app/utils/syntax.scala @@ -8,6 +8,7 @@ import com.auth0.jwt.interfaces.DecodedJWT import com.github.blemale.scaffeine.Cache import com.typesafe.config.{ConfigFactory, ConfigRenderOptions} import org.apache.commons.codec.binary.{Base64, Hex} +import otoroshi.env.Env import otoroshi.models.WSProxyServerJson import otoroshi.next.utils.JsonHelpers import otoroshi.ssl.DynamicSSLEngineProvider @@ -28,7 +29,7 @@ import java.util.concurrent.TimeUnit import java.util.concurrent.atomic.{AtomicInteger, AtomicLong, AtomicReference} import scala.collection.TraversableOnce import scala.collection.concurrent.TrieMap -import scala.concurrent.duration.Duration +import scala.concurrent.duration.{Duration, FiniteDuration} import scala.concurrent.{Await, ExecutionContext, Future, Promise} import scala.reflect.ClassTag import scala.util.control.{NoStackTrace, NonFatal} @@ -595,6 +596,16 @@ object implicits { } } } + implicit class BetterFiniteDuration(val duration: FiniteDuration) extends AnyVal { + def timeout(implicit env: Env, ec: ExecutionContext): Future[Unit] = await(env, ec) + def await(implicit env: Env, ec: ExecutionContext): Future[Unit] = { + val promise = Promise.apply[Unit]() + env.otoroshiScheduler.scheduleOnce(duration) { + promise.trySuccess(()) + } + promise.future + } + } implicit class BetterDuration(val duration: Duration) extends AnyVal { final def toHumanReadable: String = { val units = Seq(