From 5d6b87ed05099621ac18df45a908187bca102ab6 Mon Sep 17 00:00:00 2001 From: Weiwen Zhang Date: Tue, 10 Nov 2026 14:50:33 +0800 Subject: [PATCH] feat(pekko-http-backend): add Pekko 2 artifact Add a separate pekko-http-backend-pekko2 artifact built against Pekko HTTP 2.0.0 and Pekko Stream 2.0.0. Pekko 2 is not binary compatible with Pekko 1.x, so the existing pekko-http-backend artifact remains on Pekko 1.4.2. The new module reuses the existing sources. Version-specific compat shims cover watchTermination() -> watchTermination and the changed Route.seal signature. The deprecated Source.queue overload is kept for now to preserve WebSocket backpressure semantics. The new artifact is published for Scala 2.13 and 3 and targets Java 17+. Tests pass on Scala 2.13/3 (163/163), and the existing module remains green on Scala 2.12/2.13/3 (163/163). --- build.sbt | 41 +++++++++++++++++++ docs/backends/pekko.md | 19 +++++++++ .../PekkoWatchTerminationCompat.scala | 40 ++++++++++++++++++ .../PekkoWatchTerminationCompat.scala | 38 +++++++++++++++++ .../client4/pekkohttp/BodyFromPekko.scala | 7 +++- .../sttp/client4/pekkohttp/FromPekko.scala | 7 +++- .../client4/pekkohttp/PekkoHttpBackend.scala | 3 +- .../client4/pekkohttp/PekkoHttpClient.scala | 2 +- .../pekkohttp/PekkoHttpStreamingTest.scala | 3 +- 9 files changed, 154 insertions(+), 6 deletions(-) create mode 100644 pekko-http-backend-pekko2/src/main/scala/sttp/client4/pekkohttp/PekkoWatchTerminationCompat.scala create mode 100644 pekko-http-backend/src/main/scala-pekko1/sttp/client4/pekkohttp/PekkoWatchTerminationCompat.scala diff --git a/build.sbt b/build.sbt index e93bb4d5bc..0ef1192b89 100644 --- a/build.sbt +++ b/build.sbt @@ -71,6 +71,14 @@ val commonJvmSettings = commonSettings ++ Seq( Test / testOptions += Tests.Argument("-oD") // add test timings; js build specify other options which conflict ) +// Pekko 2 requires Java 17, while the shared Scala 2.13 settings target Java 11. +val commonJvmSettingsPekko2 = commonSettings ++ Seq( + scalacOptions ++= + (if (ScalaArtifacts.isScala3(scalaVersion.value)) Seq("-java-output-version", "17") + else Seq("-release", "17")), + Test / testOptions += Tests.Argument("-oD") +) + val commonJsSettings = commonSettings ++ Seq( scalaJSLinkerConfig ~= { _.withBatchMode(true).withParallel(false) @@ -164,6 +172,10 @@ val pekkoHttp = "org.apache.pekko" %% "pekko-http" % "1.4.2" val pekkoStreamVersion = "1.7.1" val pekkoStreams = "org.apache.pekko" %% "pekko-stream" % pekkoStreamVersion +val pekko2Http = "org.apache.pekko" %% "pekko-http" % "2.0.0" +val pekko2StreamVersion = "2.0.0" +val pekko2Streams = "org.apache.pekko" %% "pekko-stream" % pekko2StreamVersion + val scalaTest = libraryDependencies ++= Seq("freespec", "funsuite", "flatspec", "wordspec", "shouldmatchers").map(m => "org.scalatest" %% s"scalatest-$m" % "3.2.20" % Test ) @@ -251,6 +263,7 @@ lazy val rawAllAggregates = zio.projectRefs ++ akkaHttpBackend.projectRefs ++ pekkoHttpBackend.projectRefs ++ + pekko2HttpBackend.projectRefs ++ okhttpBackend.projectRefs ++ okhttpMonixBackend.projectRefs ++ http4sCe2Backend.projectRefs ++ @@ -639,6 +652,8 @@ lazy val pekkoHttpBackend = (projectMatrix in file("pekko-http-backend")) .settings(testServerSettings) .settings( name := "pekko-http-backend", + Compile / unmanagedSourceDirectories += + (ThisBuild / baseDirectory).value / "pekko-http-backend/src/main/scala-pekko1", libraryDependencies ++= Seq( pekkoHttp, // provided as we don't want to create a transitive dependency on a specific streams version, @@ -652,6 +667,32 @@ lazy val pekkoHttpBackend = (projectMatrix in file("pekko-http-backend")) scalaVersions = scala2And3 ) +// Pekko 2 is not binary compatible with Pekko 1.x, so it is published as a separate artifact. +// The sources are shared with pekkoHttpBackend; only the watchTermination compat shim differs. +lazy val pekko2HttpBackend = (projectMatrix in file("pekko-http-backend-pekko2")) + .settings(commonJvmSettingsPekko2) + .settings(testServerSettings) + .settings( + name := "pekko-http-backend-pekko2", + Compile / unmanagedSourceDirectories ++= Seq( + (ThisBuild / baseDirectory).value / "pekko-http-backend/src/main/scala" + ), + Test / unmanagedSourceDirectories ++= Seq( + (ThisBuild / baseDirectory).value / "pekko-http-backend/src/test/scala" + ), + libraryDependencies ++= Seq( + pekko2Http, + // provided as we don't want to create a transitive dependency on a specific streams version, + // just as pekko-http doesn't + pekko2Streams % "provided", + "com.softwaremill.sttp.shared" %% "pekko" % sttpSharedVersion + ) + ) + .dependsOn(core % compileAndTest) + .jvmPlatform( + scalaVersions = scala2_13And3 + ) + //-- okhttp lazy val okhttpBackend = (projectMatrix in file("okhttp-backend")) .settings(commonJvmSettings) diff --git a/docs/backends/pekko.md b/docs/backends/pekko.md index 6a95f5e444..bcac165c4f 100644 --- a/docs/backends/pekko.md +++ b/docs/backends/pekko.md @@ -6,6 +6,8 @@ This backend is based on [pekko-http](https://pekko.apache.org/docs/pekko-http/c "com.softwaremill.sttp.client4" %% "pekko-http-backend" % "@VERSION@" ``` +For Pekko HTTP 2, use the separate `pekko-http-backend-pekko2` artifact; see [Pekko HTTP 2](#pekko-http-2) below. + A fully **asynchronous** backend. Uses the `Future` effect to return responses. There are also [other `Future`-based backends](future.md), which don't depend on Pekko. Note that you'll also need an explicit dependency on pekko-streams, as pekko-http doesn't depend on any specific pekko-streams version. So you'll also need to add, for example: @@ -121,3 +123,20 @@ basicRequest .response(asStream(PekkoStreams)(stream => processEvents(stream.via(PekkoHttpServerSentEvents.parse)))) ``` + +## Pekko HTTP 2 + +Pekko HTTP 2 is not binary compatible with Pekko HTTP 1.x. Use the separate artifact: + +``` +"com.softwaremill.sttp.client4" %% "pekko-http-backend-pekko2" % "@VERSION@" +``` + +It is published for Scala 2.13 and 3, and requires Java 17+. It is not published for Scala 2.12. +The package and imports are unchanged. Do not put both backends on the same classpath. + +For Pekko 2, use `pekko-stream` 2.0.0: + +``` +"org.apache.pekko" %% "pekko-stream" % "2.0.0" +``` diff --git a/pekko-http-backend-pekko2/src/main/scala/sttp/client4/pekkohttp/PekkoWatchTerminationCompat.scala b/pekko-http-backend-pekko2/src/main/scala/sttp/client4/pekkohttp/PekkoWatchTerminationCompat.scala new file mode 100644 index 0000000000..61cd9e831d --- /dev/null +++ b/pekko-http-backend-pekko2/src/main/scala/sttp/client4/pekkohttp/PekkoWatchTerminationCompat.scala @@ -0,0 +1,40 @@ +package sttp.client4.pekkohttp + +import org.apache.pekko.Done +import org.apache.pekko.http.scaladsl.model.{HttpRequest, HttpResponse} +import org.apache.pekko.http.scaladsl.server.{ExceptionHandler, RejectionHandler, Route, RoutingLog} +import org.apache.pekko.http.scaladsl.settings.{ParserSettings, RoutingSettings} +import org.apache.pekko.stream.Materializer +import org.apache.pekko.stream.scaladsl.{Flow, Source} + +import scala.concurrent.{ExecutionContextExecutor, Future} + +private[pekkohttp] object PekkoWatchTerminationCompat { + implicit final class FlowWatchTerminationCompat[In, Out, Mat](private val flow: Flow[In, Out, Mat]) { + def watchTerminationCompat[Mat2](matF: (Mat, Future[Done]) => Mat2): Flow[In, Out, Mat2] = + flow.watchTermination(matF) + } + + implicit final class SourceWatchTerminationCompat[Out, Mat](private val source: Source[Out, Mat]) { + def watchTerminationCompat[Mat2](matF: (Mat, Future[Done]) => Mat2): Source[Out, Mat2] = + source.watchTermination(matF) + } +} + +private[pekkohttp] object PekkoRouteCompat { + def asyncHandler(route: Route)(implicit + routingSettings: RoutingSettings, + parserSettings: ParserSettings, + materializer: Materializer, + routingLog: RoutingLog, + executionContext: ExecutionContextExecutor, + rejectionHandler: RejectionHandler, + exceptionHandler: ExceptionHandler + ): HttpRequest => Future[HttpResponse] = { + val sealedRoute = Route.seal(route)(routingSettings, rejectionHandler, exceptionHandler) + // Route.toFunction creates its own routing/parser settings; keep the custom handlers + // by sealing the route first. The remaining settings are not part of its public API. + val _ = (parserSettings, routingLog, executionContext) + Route.toFunction(sealedRoute)(materializer.system) + } +} diff --git a/pekko-http-backend/src/main/scala-pekko1/sttp/client4/pekkohttp/PekkoWatchTerminationCompat.scala b/pekko-http-backend/src/main/scala-pekko1/sttp/client4/pekkohttp/PekkoWatchTerminationCompat.scala new file mode 100644 index 0000000000..40acd57073 --- /dev/null +++ b/pekko-http-backend/src/main/scala-pekko1/sttp/client4/pekkohttp/PekkoWatchTerminationCompat.scala @@ -0,0 +1,38 @@ +package sttp.client4.pekkohttp + +import org.apache.pekko.Done +import org.apache.pekko.http.scaladsl.model.{HttpRequest, HttpResponse} +import org.apache.pekko.http.scaladsl.server.{ExceptionHandler, RejectionHandler, Route, RoutingLog} +import org.apache.pekko.http.scaladsl.settings.{ParserSettings, RoutingSettings} +import org.apache.pekko.stream.Materializer +import org.apache.pekko.stream.scaladsl.{Flow, Source} + +import scala.concurrent.{ExecutionContextExecutor, Future} + +private[pekkohttp] object PekkoWatchTerminationCompat { + implicit final class FlowWatchTerminationCompat[In, Out, Mat](private val flow: Flow[In, Out, Mat]) { + def watchTerminationCompat[Mat2](matF: (Mat, Future[Done]) => Mat2): Flow[In, Out, Mat2] = + flow.watchTermination()(matF) + } + + implicit final class SourceWatchTerminationCompat[Out, Mat](private val source: Source[Out, Mat]) { + def watchTerminationCompat[Mat2](matF: (Mat, Future[Done]) => Mat2): Source[Out, Mat2] = + source.watchTermination()(matF) + } +} + +private[pekkohttp] object PekkoRouteCompat { + def asyncHandler(route: Route)(implicit + routingSettings: RoutingSettings, + parserSettings: ParserSettings, + materializer: Materializer, + routingLog: RoutingLog, + executionContext: ExecutionContextExecutor, + rejectionHandler: RejectionHandler, + exceptionHandler: ExceptionHandler + ): HttpRequest => Future[HttpResponse] = { + val sealedRoute = Route.seal(route)(routingSettings, parserSettings, rejectionHandler, exceptionHandler) + val _ = (routingLog, executionContext) + Route.toFunction(sealedRoute)(materializer.system) + } +} diff --git a/pekko-http-backend/src/main/scala/sttp/client4/pekkohttp/BodyFromPekko.scala b/pekko-http-backend/src/main/scala/sttp/client4/pekkohttp/BodyFromPekko.scala index 09730a0463..376c985880 100644 --- a/pekko-http-backend/src/main/scala/sttp/client4/pekkohttp/BodyFromPekko.scala +++ b/pekko-http-backend/src/main/scala/sttp/client4/pekkohttp/BodyFromPekko.scala @@ -15,6 +15,7 @@ import sttp.client4.ws.{GotAWebSocketException, NotAWebSocketException} import sttp.model.{Headers, ResponseMetadata} import sttp.monad.{FutureMonad, MonadError} import sttp.ws.{WebSocket, WebSocketBufferFull, WebSocketClosed, WebSocketFrame} +import sttp.client4.pekkohttp.PekkoWatchTerminationCompat.* import scala.concurrent.{ExecutionContext, Future, Promise} import scala.util.Failure @@ -38,7 +39,7 @@ private[pekkohttp] class BodyFromPekko()(implicit ec: ExecutionContext, mat: Mat case Right(file) => HttpEntity.fromFile(response.entity.contentType, file.toFile) } - Future.successful(response.copy(entity = replayEntity)) + Future.successful(response.withEntity(replayEntity)) } override protected def regularIgnore(response: HttpResponse): Future[Unit] = @@ -115,7 +116,7 @@ private[pekkohttp] class BodyFromPekko()(implicit ec: ExecutionContext, mat: Mat case _ => true } .mapConcat(incoming => frameToMessage(incoming).toList) - .watchTermination() { (notUsed, done) => + .watchTerminationCompat { (notUsed, done) => donePromise.completeWith(done) notUsed } @@ -135,6 +136,8 @@ private[pekkohttp] class BodyFromPekko()(implicit ec: ExecutionContext, mat: Mat .mapMaterializedValue(sinkQueuePromise.success) val sourceQueuePromise = Promise[SourceQueueWithComplete[Message]]() + // Pekko 2 deprecates this overload; keep it for now to preserve backpressure semantics. + // TODO: migrate to BoundedSourceQueue / actorRefWithBackpressure. val source = Source.queue[Message](1, OverflowStrategy.backpressure).mapMaterializedValue(sourceQueuePromise.success) diff --git a/pekko-http-backend/src/main/scala/sttp/client4/pekkohttp/FromPekko.scala b/pekko-http-backend/src/main/scala/sttp/client4/pekkohttp/FromPekko.scala index 76b01e1d64..9f018f4963 100644 --- a/pekko-http-backend/src/main/scala/sttp/client4/pekkohttp/FromPekko.scala +++ b/pekko-http-backend/src/main/scala/sttp/client4/pekkohttp/FromPekko.scala @@ -20,7 +20,12 @@ private[pekkohttp] object FromPekko { def exception(request: GenericRequest[?, ?], e: Exception): Option[Exception] = e match { case e: pekko.stream.ConnectionException => Some(new SttpClientException.ConnectException(request, e)) - case e: pekko.stream.StreamTcpException => + // Pekko 2 surfaces some connect-time failures (for example an unknown host whose + // connection is closed before a response) as this internal exception. It is not + // accessible from user code, so match on the class name. + case e if e.getClass.getName.endsWith("OutgoingConnectionBlueprint$UnexpectedConnectionClosureException") => + Some(new SttpClientException.ConnectException(request, e)) + case e: pekko.stream.StreamTcpException => e.getCause match { case ee: Exception => exception(request, ee).orElse(Some(new SttpClientException.ReadException(request, e))) diff --git a/pekko-http-backend/src/main/scala/sttp/client4/pekkohttp/PekkoHttpBackend.scala b/pekko-http-backend/src/main/scala/sttp/client4/pekkohttp/PekkoHttpBackend.scala index d9a06c815f..2bbed05321 100644 --- a/pekko-http-backend/src/main/scala/sttp/client4/pekkohttp/PekkoHttpBackend.scala +++ b/pekko-http-backend/src/main/scala/sttp/client4/pekkohttp/PekkoHttpBackend.scala @@ -34,6 +34,7 @@ import sttp.model.StatusCode import sttp.monad.FutureMonad import sttp.monad.MonadError import sttp.model.HeaderNames +import sttp.client4.pekkohttp.PekkoWatchTerminationCompat.* import scala.concurrent.ExecutionContext import scala.concurrent.Future @@ -174,7 +175,7 @@ class PekkoHttpBackend private ( callback() response } else { - response.transformEntityDataBytes(Flow[ByteString].watchTermination() { case (mat, doneFuture) => + response.transformEntityDataBytes(Flow[ByteString].watchTerminationCompat { case (mat, doneFuture) => doneFuture.onComplete(t => if (t.isSuccess) callback()) mat }) diff --git a/pekko-http-backend/src/main/scala/sttp/client4/pekkohttp/PekkoHttpClient.scala b/pekko-http-backend/src/main/scala/sttp/client4/pekkohttp/PekkoHttpClient.scala index 34a805aff8..ca77118b27 100644 --- a/pekko-http-backend/src/main/scala/sttp/client4/pekkohttp/PekkoHttpClient.scala +++ b/pekko-http-backend/src/main/scala/sttp/client4/pekkohttp/PekkoHttpClient.scala @@ -84,5 +84,5 @@ object PekkoHttpClient { executionContext: ExecutionContextExecutor = null, rejectionHandler: RejectionHandler = RejectionHandler.default, exceptionHandler: ExceptionHandler = null - ): PekkoHttpClient = stubFromAsyncHandler(Route.asyncHandler(route)) + ): PekkoHttpClient = stubFromAsyncHandler(PekkoRouteCompat.asyncHandler(route)) } diff --git a/pekko-http-backend/src/test/scala/sttp/client4/pekkohttp/PekkoHttpStreamingTest.scala b/pekko-http-backend/src/test/scala/sttp/client4/pekkohttp/PekkoHttpStreamingTest.scala index 5ab0d03051..caeae9457c 100644 --- a/pekko-http-backend/src/test/scala/sttp/client4/pekkohttp/PekkoHttpStreamingTest.scala +++ b/pekko-http-backend/src/test/scala/sttp/client4/pekkohttp/PekkoHttpStreamingTest.scala @@ -9,6 +9,7 @@ import sttp.client4.StreamBackend import sttp.model.sse.ServerSentEvent import sttp.client4.testing.ConvertToFuture import sttp.client4.testing.streaming.StreamingTest +import sttp.client4.pekkohttp.PekkoWatchTerminationCompat.* import scala.concurrent.duration._ import scala.concurrent.{Await, Future} @@ -48,7 +49,7 @@ class PekkoHttpStreamingTest extends StreamingTest[Future, PekkoStreams] { Source .single(ByteString(chunk)) .concat(Source.lazySource { () => onStarted(); Source.never[ByteString] }) - .watchTermination()((_, done) => done.onComplete(_ => onFinalized())) + .watchTerminationCompat((_, done) => done.onComplete(_ => onFinalized())) override def failingBodyProducer(chunk: Array[Byte], error: Throwable): Source[ByteString, Any] = Source.single(ByteString(chunk)).concat(Source.failed(error))