Skip to content
Open
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
41 changes: 41 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
)
Expand Down Expand Up @@ -251,6 +263,7 @@ lazy val rawAllAggregates =
zio.projectRefs ++
akkaHttpBackend.projectRefs ++
pekkoHttpBackend.projectRefs ++
pekko2HttpBackend.projectRefs ++
okhttpBackend.projectRefs ++
okhttpMonixBackend.projectRefs ++
http4sCe2Backend.projectRefs ++
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions docs/backends/pekko.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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"
```
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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] =
Expand Down Expand Up @@ -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
}
Expand All @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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))
Expand Down
Loading