You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pekko.apache.org by jr...@apache.org on 2022/11/03 11:24:00 UTC

[incubator-pekko-http] 30/47: caching: fix cached execution and test (#4099)

This is an automated email from the ASF dual-hosted git repository.

jrudolph pushed a commit to branch scala-3
in repository https://gitbox.apache.org/repos/asf/incubator-pekko-http.git

commit 2293252797f0aef0dcf44f8b97d86475e97d202b
Author: Johannes Rudolph <jo...@gmail.com>
AuthorDate: Mon Apr 11 15:06:02 2022 +0200

    caching: fix cached execution and test (#4099)
    
    The `asyncRoute & ...` dconstruction id not work as expected and also using
    `check` in tests starved threads themselves.
    
    Refs #4092, update to #4093
---
 .../server/directives/CachingDirectives.scala      | 24 +++++++---------------
 .../server/directives/CachingDirectivesSpec.scala  | 16 +++++++--------
 2 files changed, 14 insertions(+), 26 deletions(-)

diff --git a/akka-http-caching/src/main/scala/akka/http/scaladsl/server/directives/CachingDirectives.scala b/akka-http-caching/src/main/scala/akka/http/scaladsl/server/directives/CachingDirectives.scala
index a57765a04..f18f8fcbf 100644
--- a/akka-http-caching/src/main/scala/akka/http/scaladsl/server/directives/CachingDirectives.scala
+++ b/akka-http-caching/src/main/scala/akka/http/scaladsl/server/directives/CachingDirectives.scala
@@ -49,13 +49,13 @@ trait CachingDirectives {
     // Do directive processing asynchronously to avoid locking the cache accidentally (#4092)
     // This will be slightly slower, but the rational here is that caching is used for slower kind of processing
     // anyway so the performance hit should be acceptable.
-    CachingDirectives.asyncRoute &
-      mapInnerRoute { route => ctx =>
-        keyer.lift(ctx) match {
-          case Some(key) => cache.apply(key, () => route(ctx))
-          case None      => route(ctx)
-        }
+    Directive { inner => ctx =>
+      import ctx.executionContext
+      keyer.lift(ctx) match {
+        case Some(key) => cache.apply(key, () => Future(inner(())(ctx)).flatten)
+        case None      => inner(())(ctx)
       }
+    }
 
   /**
    * Creates an [[LfuCache]] with default settings obtained from the system's configuration.
@@ -70,14 +70,4 @@ trait CachingDirectives {
     LfuCache[K, RouteResult](settings)
 }
 
-object CachingDirectives extends CachingDirectives {
-  /**
-   * Run all route processing asynchronously.
-   */
-  private[CachingDirectives] def asyncRoute: Directive0 =
-    Directive { inner => ctx =>
-      import ctx.executionContext
-      Future { inner(()) }
-        .flatMap(route => route(ctx))
-    }
-}
+object CachingDirectives extends CachingDirectives
diff --git a/akka-http-caching/src/test/scala/akka/http/scaladsl/server/directives/CachingDirectivesSpec.scala b/akka-http-caching/src/test/scala/akka/http/scaladsl/server/directives/CachingDirectivesSpec.scala
index 8c7eb1fea..6fe074597 100644
--- a/akka-http-caching/src/test/scala/akka/http/scaladsl/server/directives/CachingDirectivesSpec.scala
+++ b/akka-http-caching/src/test/scala/akka/http/scaladsl/server/directives/CachingDirectivesSpec.scala
@@ -6,18 +6,17 @@ package akka.http.scaladsl.server.directives
 
 import akka.http.caching.scaladsl.{ CachingSettings, LfuCacheSettings }
 import akka.http.impl.util._
-import akka.http.scaladsl.model.{ HttpResponse, Uri }
-import akka.http.scaladsl.model.headers._
+import akka.http.scaladsl.model.HttpMethods.GET
 import akka.http.scaladsl.model.headers.CacheDirectives._
+import akka.http.scaladsl.model.headers._
+import akka.http.scaladsl.model.{ HttpResponse, Uri }
 import akka.http.scaladsl.server.Directives._
-import akka.http.scaladsl.server.{ ExceptionHandler, RequestContext }
+import akka.http.scaladsl.server.{ ExceptionHandler, RequestContext, RouteResult }
 import akka.http.scaladsl.testkit.ScalatestRouteTest
-import akka.http.scaladsl.model.HttpMethods.GET
+import akka.testkit._
 import org.scalatest.matchers.should.Matchers
 import org.scalatest.wordspec.AnyWordSpec
 
-import akka.testkit._
-
 import scala.concurrent.Future
 import scala.concurrent.duration._
 
@@ -101,11 +100,10 @@ class CachingDirectivesSpec extends AnyWordSpec with Matchers with ScalatestRout
       }
 
       implicit val executor = system.dispatcher
+      val routeFunc = RouteResult.routeToFunction(route)
 
       Future.traverse(1 to 1000) { i =>
-        Future {
-          Get(s"/$i") ~> route ~> check {} // check blocks to wait for result
-        }
+        routeFunc(Get(s"/$i"))
       }.awaitResult(10.second.dilated)
     }
   }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pekko.apache.org
For additional commands, e-mail: commits-help@pekko.apache.org