Skip to content

Commit

Permalink
fix some more Resource.allocated code
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-klass committed Nov 25, 2024
1 parent cd1ce53 commit 95167a1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
22 changes: 11 additions & 11 deletions perf-tests/src/main/scala/sttp/tapir/perf/http4s/Http4s.scala
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,17 @@ object server {
def runServer(
router: WebSocketBuilder2[IO] => HttpRoutes[IO]
): IO[ServerRunner.KillSwitch] =
EmberServerBuilder
.default[IO]
.withPort(ip4s.Port.fromInt(Port).get)
.withHttpWebSocketApp(wsb => router(wsb).orNotFound)
.withMaxConnections(maxConnections)
.build
.allocated
.map(_._2)
.map(_.flatTap { _ =>
IO.println("Http4s server closed.")
})
Resource.eval(Deferred[IO, Unit]).flatMap { stopSignal =>
EmberServerBuilder
.default[IO]
.withPort(ip4s.Port.fromInt(Port).get)
.withHttpWebSocketApp(wsb => router(wsb).orNotFound)
.withMaxConnections(maxConnections)
.build
.use(_ => stopSignal.get *> IO.println("Http4s server closed."))
.background
.as(stopSignal.complete(()))
}
}

object TapirServer extends ServerRunner { override def start = server.runServer(Tapir.router(1)) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package sttp.tapir.server.tests

import cats.data.NonEmptyList
import cats.effect.Deferred
import cats.effect.{IO, Resource}
import sttp.tapir.server.ServerEndpoint
import sttp.tapir.server.interceptor.CustomiseInterceptors
import sttp.tapir.tests._
import sttp.tapir.tests.*

import scala.concurrent.duration.FiniteDuration

Expand All @@ -21,8 +22,12 @@ trait TestServerInterpreter[F[_], +R, OPTIONS, ROUTE] {
def serverWithStop(
routes: NonEmptyList[ROUTE],
gracefulShutdownTimeout: Option[FiniteDuration] = None
): Resource[IO, (Port, KillSwitch)] =
Resource.eval(server(routes, gracefulShutdownTimeout).allocated)
): Resource[IO, (Port, KillSwitch)] = for {
stopSignal <- Resource.eval(Deferred[IO, Unit])
portValue <- Resource.eval(Deferred[IO, Port])
_ <- server(routes, gracefulShutdownTimeout).use(port => portValue.complete(port) *> stopSignal.get).background
port <- Resource.eval(portValue.get)
} yield (port, stopSignal.complete(()).void)

def server(routes: NonEmptyList[ROUTE], gracefulShutdownTimeout: Option[FiniteDuration] = None): Resource[IO, Port]
}

0 comments on commit 95167a1

Please sign in to comment.