Skip to content

Commit

Permalink
Provide a Scope per server call (#3197)
Browse files Browse the repository at this point in the history
  • Loading branch information
987Nabil committed Jan 20, 2025
1 parent 5418a2a commit 09f624a
Show file tree
Hide file tree
Showing 16 changed files with 115 additions and 69 deletions.
60 changes: 30 additions & 30 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
scala: [2.12.19, 2.13.14, 3.3.3]
scala: [2.12.20, 2.13.16, 3.3.4]
java:
- graal_graalvm@17
- graal_graalvm@21
Expand Down Expand Up @@ -82,8 +82,8 @@ jobs:
apps: sbt

- name: Check formatting
if: matrix.scala == '2.13.14'
run: sbt ++2.13.14 fmtCheck
if: matrix.scala == '2.13.16'
run: sbt ++2.13.16 fmtCheck

- name: Check that workflows are up to date
run: sbt '++ ${{ matrix.scala }}' githubWorkflowCheck
Expand All @@ -97,10 +97,10 @@ jobs:

- name: Check doc generation
if: ${{ github.event_name == 'pull_request' }}
run: sbt ++2.13.14 doc
run: sbt ++2.13.16 doc

- name: zio-http-shaded Tests
if: matrix.scala == '2.13.14'
if: matrix.scala == '2.13.16'
env:
PUBLISH_SHADED: true
run: sbt '++ ${{ matrix.scala }}' zioHttpShadedTests/test
Expand All @@ -121,7 +121,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
scala: [2.13.14]
scala: [2.13.16]
java: [graal_graalvm@17]
runs-on: ${{ matrix.os }}
steps:
Expand Down Expand Up @@ -166,32 +166,32 @@ jobs:
java-version: 21
cache: sbt

- name: Download target directories (2.12.19)
- name: Download target directories (2.12.20)
uses: actions/download-artifact@v4
with:
name: target-${{ matrix.os }}-2.12.19-${{ matrix.java }}
name: target-${{ matrix.os }}-2.12.20-${{ matrix.java }}

- name: Inflate target directories (2.12.19)
- name: Inflate target directories (2.12.20)
run: |
tar xf targets.tar
rm targets.tar
- name: Download target directories (2.13.14)
- name: Download target directories (2.13.16)
uses: actions/download-artifact@v4
with:
name: target-${{ matrix.os }}-2.13.14-${{ matrix.java }}
name: target-${{ matrix.os }}-2.13.16-${{ matrix.java }}

- name: Inflate target directories (2.13.14)
- name: Inflate target directories (2.13.16)
run: |
tar xf targets.tar
rm targets.tar
- name: Download target directories (3.3.3)
- name: Download target directories (3.3.4)
uses: actions/download-artifact@v4
with:
name: target-${{ matrix.os }}-3.3.3-${{ matrix.java }}
name: target-${{ matrix.os }}-3.3.4-${{ matrix.java }}

- name: Inflate target directories (3.3.3)
- name: Inflate target directories (3.3.4)
run: |
tar xf targets.tar
rm targets.tar
Expand Down Expand Up @@ -264,7 +264,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
scala: [2.13.14]
scala: [2.13.16]
java: [temurin@8]
runs-on: ${{ matrix.os }}
steps:
Expand Down Expand Up @@ -300,7 +300,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
scala: [2.13.14]
scala: [2.13.16]
java: [temurin@8]
runs-on: ${{ matrix.os }}
steps:
Expand Down Expand Up @@ -338,7 +338,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
scala: [2.13.14]
scala: [2.13.16]
java: [temurin@8]
runs-on: ${{ matrix.os }}
steps:
Expand Down Expand Up @@ -376,7 +376,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
scala: [2.13.14]
scala: [2.13.16]
java: [temurin@8]
runs-on: ${{ matrix.os }}
steps:
Expand Down Expand Up @@ -414,7 +414,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
scala: [2.13.14]
scala: [2.13.16]
java: [temurin@8]
runs-on: ${{ matrix.os }}
steps:
Expand Down Expand Up @@ -452,7 +452,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
scala: [2.13.14]
scala: [2.13.16]
java: [temurin@8]
runs-on: ${{ matrix.os }}
steps:
Expand Down Expand Up @@ -490,7 +490,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
scala: [2.13.14]
scala: [2.13.16]
java: [temurin@8]
runs-on: ${{ matrix.os }}
steps:
Expand Down Expand Up @@ -528,7 +528,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
scala: [2.13.14]
scala: [2.13.16]
java: [temurin@8]
runs-on: ${{ matrix.os }}
steps:
Expand Down Expand Up @@ -566,7 +566,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
scala: [2.13.14]
scala: [2.13.16]
java: [temurin@8]
runs-on: ${{ matrix.os }}
steps:
Expand Down Expand Up @@ -604,7 +604,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
scala: [2.13.14]
scala: [2.13.16]
java: [temurin@8]
runs-on: ${{ matrix.os }}
steps:
Expand Down Expand Up @@ -642,7 +642,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
scala: [2.13.14]
scala: [2.13.16]
java: [temurin@8]
runs-on: ${{ matrix.os }}
steps:
Expand Down Expand Up @@ -680,7 +680,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
scala: [2.13.14]
scala: [2.13.16]
java: [temurin@8]
runs-on: ${{ matrix.os }}
steps:
Expand Down Expand Up @@ -718,7 +718,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
scala: [2.13.14]
scala: [2.13.16]
java: [temurin@8]
runs-on: ${{ matrix.os }}
steps:
Expand Down Expand Up @@ -870,7 +870,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
scala: [2.13.14]
scala: [2.13.16]
java: [temurin@8]
runs-on: ${{ matrix.os }}
steps:
Expand Down Expand Up @@ -949,7 +949,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
scala: [2.13.14]
scala: [2.13.16]
java: [temurin@8]
runs-on: ${{ matrix.os }}
steps:
Expand Down
2 changes: 1 addition & 1 deletion profiling/project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version = 1.8.3
sbt.version = 1.10.7
6 changes: 3 additions & 3 deletions profiling/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "1.1.0")
addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.11.0")
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.0")
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "1.2.0")
addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.14.0")
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.4")
6 changes: 3 additions & 3 deletions project/BuildHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import xerial.sbt.Sonatype.autoImport.*
import sbtcrossproject.CrossPlugin.autoImport.crossProjectPlatform

object BuildHelper extends ScalaSettings {
val Scala212 = "2.12.19"
val Scala213 = "2.13.14"
val Scala3 = "3.3.3"
val Scala212 = "2.12.20"
val Scala213 = "2.13.16"
val Scala3 = "3.3.4"
val ScoverageVersion = "2.0.12"
val JmhVersion = "0.4.7"

Expand Down
6 changes: 3 additions & 3 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ object Dependencies {
val NettyVersion = "4.1.116.Final"
val NettyIncubatorVersion = "0.0.25.Final"
val ScalaCompactCollectionVersion = "2.12.0"
val ZioVersion = "2.1.11"
val ZioVersion = "2.1.14"
val ZioCliVersion = "0.5.0"
val ZioJsonVersion = "0.7.1"
val ZioParserVersion = "0.1.10"
Expand All @@ -16,8 +16,8 @@ object Dependencies {
val `jwt-core` = "com.github.jwt-scala" %% "jwt-core" % JwtCoreVersion
val `scala-compact-collection` = "org.scala-lang.modules" %% "scala-collection-compat" % ScalaCompactCollectionVersion

val scalafmt = "org.scalameta" %% "scalafmt-dynamic" % "3.8.1"
val scalametaParsers = "org.scalameta" %% "parsers" % "4.9.9"
val scalafmt = "org.scalameta" %% "scalafmt-dynamic" % "3.8.5"
val scalametaParsers = "org.scalameta" %% "parsers" % "4.12.6"

val netty =
Seq(
Expand Down
6 changes: 3 additions & 3 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.12.1")
addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.14.0")
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.2")
addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.4.7")
addSbtPlugin("com.timushev.sbt" % "sbt-updates" % "0.6.4")
Expand All @@ -7,10 +7,10 @@ addSbtPlugin("com.github.sbt" % "sbt-github-actions" % "0.23.0")
addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.5.12")
addSbtPlugin("dev.zio" % "zio-sbt-website" % "0.4.0-alpha.26")
addSbtPlugin("de.heikoseeberger" % "sbt-header" % "5.10.0")
addSbtPlugin("org.scoverage" % "sbt-scoverage" % "2.0.12")
addSbtPlugin("org.scoverage" % "sbt-scoverage" % "2.3.0")
addSbtPlugin("io.get-coursier" % "sbt-shading" % "2.1.4")
addSbtPlugin("com.github.cb372" % "sbt-explicit-dependencies" % "0.3.1")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.16.0")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.18.1")
addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "1.3.2")
addSbtPlugin("com.thesamet" % "sbt-protoc" % "1.0.7")
addSbtPlugin("com.thesamet" % "sbt-protoc-gen-project" % "0.1.8")
Expand Down
2 changes: 1 addition & 1 deletion zio-http-testkit/src/main/scala/zio/http/TestServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ final case class TestServer(driver: Driver, bindPort: Int) extends Server {
_ <- driver.addApp(provided, r)
} yield ()

override def install[R](routes: Routes[R, Response])(implicit
override def install[R](routes: Routes[Scope & R, Response])(implicit
trace: zio.Trace,
tag: EnvironmentTag[R],
): URIO[R, Unit] =
Expand Down
2 changes: 1 addition & 1 deletion zio-http/jvm/src/main/scala/zio/http/netty/NettyBody.scala
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ object NettyBody extends BodyEncoding {
lazy val loop: ZChannel[Any, Any, Any, Any, E, Chunk[A], Unit] =
ZChannel.unwrap(
queue.take
.flatMap(_.done)
.flatMap(_.exit)
.fold(
maybeError =>
ZChannel.fromZIO(queue.shutdown) *>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private[zio] final case class NettyDriver(
)
} yield StartResult(port, serverInboundHandler.inFlightRequests)

def addApp[R](newApp: Routes[R, Response], env: ZEnvironment[R])(implicit trace: Trace): UIO[Unit] =
override def addApp[R](newApp: Routes[Scope & R, Response], env: ZEnvironment[R])(implicit trace: Trace): UIO[Unit] =
ZIO.fiberId.map { fiberId =>
var loop = true
while (loop) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,13 +334,16 @@ private[zio] final case class ServerInboundHandler(
)
}

val program = exit.foldCauseZIO(
_.failureOrCause match {
case Left(resp) => writeResponse(resp)
case Right(c) if c.isInterruptedOnly => closeChannel()
case Right(c) => writeResponse(withDefaultErrorResponse(FiberFailure(c)))
},
writeResponse,
val scope = Scope.unsafe.make
val program = scope.use(
exit.foldCauseZIO(
_.failureOrCause match {
case Left(resp) => writeResponse(resp)
case Right(c) if c.isInterruptedOnly => closeChannel()
case Right(c) => writeResponse(withDefaultErrorResponse(FiberFailure(c)))
},
writeResponse,
),
)

runtime.run(ctx, ensured, preferOnCurrentThread = avoidCtxSwitching)(program)
Expand Down
43 changes: 42 additions & 1 deletion zio-http/jvm/src/test/scala/zio/http/ServerRuntimeSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import zio._
import zio.test.TestAspect._
import zio.test._

import zio.stream.ZStream

import zio.http.codec.HttpContentCodec._
import zio.http.internal.{DynamicServer, RoutesRunnableSpec}
import zio.http.netty.NettyConfig

Expand Down Expand Up @@ -75,10 +78,48 @@ object ServerRuntimeSpec extends RoutesRunnableSpec {
.scoped(serve[Foo](server))
.zipRight(server.deploy.body.run(path = Path.root / "test", method = Method.GET))
.flatMap(_.asString(Charsets.Utf8))
.map(b => assertTrue(b == "1"))
.map(b => assertTrue(b == "2")) // one extra for Scope
} +
test("with scope") {
val ref = Ref.unsafe.make(0)(zio.Unsafe)
val routes = Routes(
Method.GET / "test" -> handler(
ZIO.addFinalizer(ref.set(1)).as(Response.text("ok")),
),
)
serve(routes)
.zipRight(routes.deploy.body.run(path = Path.root / "test", method = Method.GET))
.flatMap(_.asString(Charsets.Utf8))
.map(b => assertTrue(b == "ok")) *> ref.get.map { v => assertTrue(v == 1) }
} +
test("with scope streaming") {
val ref = Ref.unsafe.make(0)(zio.Unsafe)
val routes = Routes(
Method.GET / "test" -> handler(
Body
.fromStreamEnv(
ZStream.fromZIO(
ZIO.addFinalizer(ref.set(1)) *> ref.get.flatMap(v =>
if (v == 0) Exit.succeed(Chunk.fromArray("ok".getBytes)) else Exit.fail(new Exception("error")),
),
),
)
.map(body =>
Response(
body = body,
),
)
.orDie,
),
)
serve(routes)
.zipRight(routes.deploy.body.run(path = Path.root / "test", method = Method.GET))
.flatMap(_.asString(Charsets.Utf8))
.map(b => assertTrue(b == "ok")) *> ref.get.map { v => assertTrue(v == 1) }
}
}
.provide(
Scope.default,
DynamicServer.live,
Server.customized,
ZLayer.succeed(Server.Config.default),
Expand Down
Loading

0 comments on commit 09f624a

Please sign in to comment.