Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Play: Optimize usages of route #3842

Merged
merged 2 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions perf-tests/src/main/scala/sttp/tapir/perf/play/Play.scala
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,19 @@ object Play {

def runServer(routes: ActorSystem => Routes): IO[ServerRunner.KillSwitch] = {
implicit lazy val perfActorSystem: ActorSystem = ActorSystem(s"tapir-play")
val playRouter =
Router.from(
List(routes(perfActorSystem)).reduce((a: Routes, b: Routes) => {
val handler: PartialFunction[RequestHeader, Handler] = { case request =>
a.applyOrElse(request, b)
}
handler
})
)
val components = new DefaultPekkoHttpServerComponents {
override lazy val serverConfig: ServerConfig = ServerConfig(port = Some(Port), address = "127.0.0.1", mode = Mode.Test)
override lazy val actorSystem: ActorSystem = perfActorSystem
override def router: Router =
Router.from(
List(routes(actorSystem)).reduce((a: Routes, b: Routes) => {
val handler: PartialFunction[RequestHeader, Handler] = { case request =>
a.applyOrElse(request, b)
}

handler
})
)
override def router: Router = playRouter
}
IO(components.server).map(server => IO(server.stop()))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class PlayServerWithContextTest(backend: SttpBackend[IO, Fs2Streams[IO] with Web
val components = new DefaultPekkoHttpServerComponents {
override lazy val serverConfig: ServerConfig = ServerConfig(port = Some(0), address = "127.0.0.1", mode = Mode.Test)
override lazy val actorSystem: ActorSystem = ActorSystem("tapir", defaultExecutionContext = Some(_actorSystem.dispatcher))
override def router: Router = Router.from(PlayServerInterpreter().toRoutes(e)).withPrefix("/test")
override lazy val router: Router = Router.from(PlayServerInterpreter().toRoutes(e)).withPrefix("/test")
}
val s = components.server
val r = Future.successful(()).flatMap { _ =>
Expand All @@ -49,7 +49,7 @@ class PlayServerWithContextTest(backend: SttpBackend[IO, Fs2Streams[IO] with Web
val components = new DefaultPekkoHttpServerComponents {
override lazy val serverConfig: ServerConfig = ServerConfig(port = Some(0), address = "127.0.0.1", mode = Mode.Test)
override lazy val actorSystem: ActorSystem = ActorSystem("tapir", defaultExecutionContext = Some(_actorSystem.dispatcher))
override def router: Router = Router.from(PlayServerInterpreter().toRoutes(e)).withPrefix("/test")
override lazy val router: Router = Router.from(PlayServerInterpreter().toRoutes(e)).withPrefix("/test")
}
val s = components.server
val r = basicRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class PlayTestServerInterpreter(implicit actorSystem: ActorSystem)
initialServerConfig.copy(configuration = customConf.withFallback(initialServerConfig.configuration))
override lazy val actorSystem: ActorSystem =
ActorSystem("tapir", defaultExecutionContext = Some(PlayTestServerInterpreter.this.actorSystem.dispatcher))
override def router: Router =
override lazy val router: Router =
Router.from(
routes.reduce((a: Routes, b: Routes) => {
val handler: PartialFunction[RequestHeader, Handler] = { case request =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package sttp.tapir.server.play

import akka.actor.ActorSystem
import enumeratum._
import akka.stream.scaladsl.{Flow, Sink, Source}
import cats.data.NonEmptyList
import cats.effect.{IO, Resource}
Expand All @@ -17,8 +16,6 @@ import sttp.tapir.server.tests._
import sttp.tapir.tests.{Test, TestSuite}

import scala.concurrent.Future
import sttp.tapir.codec.enumeratum.TapirCodecEnumeratum
import sttp.tapir.server.interceptor.decodefailure.DefaultDecodeFailureHandler

class PlayServerTest extends TestSuite {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class PlayServerWithContextTest(backend: SttpBackend[IO, Fs2Streams[IO] with Web
val components = new DefaultAkkaHttpServerComponents {
override lazy val serverConfig: ServerConfig = ServerConfig(port = Some(0), address = "127.0.0.1", mode = Mode.Test)
override lazy val actorSystem: ActorSystem = ActorSystem("tapir", defaultExecutionContext = Some(_actorSystem.dispatcher))
override def router: Router = Router.from(PlayServerInterpreter().toRoutes(e)).withPrefix("/test")
override lazy val router: Router = Router.from(PlayServerInterpreter().toRoutes(e)).withPrefix("/test")
}
val s = components.server
val r = Future.successful(()).flatMap { _ =>
Expand All @@ -49,7 +49,7 @@ class PlayServerWithContextTest(backend: SttpBackend[IO, Fs2Streams[IO] with Web
val components = new DefaultAkkaHttpServerComponents {
override lazy val serverConfig: ServerConfig = ServerConfig(port = Some(0), address = "127.0.0.1", mode = Mode.Test)
override lazy val actorSystem: ActorSystem = ActorSystem("tapir", defaultExecutionContext = Some(_actorSystem.dispatcher))
override def router: Router = Router.from(PlayServerInterpreter().toRoutes(e)).withPrefix("/test")
override lazy val router: Router = Router.from(PlayServerInterpreter().toRoutes(e)).withPrefix("/test")
}
val s = components.server
val r = basicRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ class PlayTestServerInterpreter(implicit actorSystem: ActorSystem)
PlayServerInterpreter(serverOptions).toRoutes(es)
}

import play.core.server.AkkaHttpServer

override def serverWithStop(
routes: NonEmptyList[Routes],
gracefulShutdownTimeout: Option[FiniteDuration]
Expand All @@ -48,7 +46,7 @@ class PlayTestServerInterpreter(implicit actorSystem: ActorSystem)
initialServerConfig.copy(configuration = customConf.withFallback(initialServerConfig.configuration))
override lazy val actorSystem: ActorSystem =
ActorSystem("tapir", defaultExecutionContext = Some(PlayTestServerInterpreter.this.actorSystem.dispatcher))
override def router: Router =
override lazy val router: Router =
Router.from(
routes.reduce((a: Routes, b: Routes) => {
val handler: PartialFunction[RequestHeader, Handler] = { case request =>
Expand Down
Loading