-
Notifications
You must be signed in to change notification settings - Fork 426
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix cleanup in Netty handler after a request timeout (#4156)
- Loading branch information
Showing
11 changed files
with
265 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
OrganizeImports.groupedImports = AggressiveMerge | ||
OrganizeImports.targetDialect = Scala3 | ||
OrganizeImports.targetDialect = Scala3 | ||
OrganizeImports.removeUnused = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
.../netty-server/src/test/scala/sttp/tapir/server/netty/NettyFutureRequestTimeoutTests.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package sttp.tapir.server.netty | ||
|
||
import sttp.tapir._ | ||
import sttp.tapir.tests.Test | ||
import scala.concurrent.Future | ||
import java.util.concurrent.atomic.AtomicInteger | ||
import scala.concurrent.duration.DurationInt | ||
import sttp.tapir.server.interceptor.metrics.MetricsRequestInterceptor | ||
import sttp.tapir.server.metrics.Metric | ||
import sttp.tapir.server.metrics.EndpointMetric | ||
import io.netty.channel.EventLoopGroup | ||
import cats.effect.IO | ||
import cats.effect.kernel.Resource | ||
import scala.concurrent.ExecutionContext | ||
import sttp.client3._ | ||
import sttp.capabilities.fs2.Fs2Streams | ||
import sttp.capabilities.WebSockets | ||
import org.scalatest.matchers.should.Matchers._ | ||
import cats.effect.unsafe.implicits.global | ||
import sttp.model.StatusCode | ||
|
||
class NettyFutureRequestTimeoutTests(eventLoopGroup: EventLoopGroup, backend: SttpBackend[IO, Fs2Streams[IO] with WebSockets])(implicit | ||
ec: ExecutionContext | ||
) { | ||
def tests(): List[Test] = List( | ||
Test("properly update metrics when a request times out") { | ||
val e = endpoint.post | ||
.in(stringBody) | ||
.out(stringBody) | ||
.serverLogicSuccess[Future] { body => | ||
Thread.sleep(2000); Future.successful(body) | ||
} | ||
|
||
val activeRequests = new AtomicInteger() | ||
val totalRequests = new AtomicInteger() | ||
val customMetrics: List[Metric[Future, AtomicInteger]] = List( | ||
Metric( | ||
metric = activeRequests, | ||
onRequest = (_, metric, me) => | ||
me.eval { | ||
EndpointMetric() | ||
.onEndpointRequest { _ => me.eval { val _ = metric.incrementAndGet(); } } | ||
.onResponseBody { (_, _) => me.eval { val _ = metric.decrementAndGet(); } } | ||
.onException { (_, _) => me.eval { val _ = metric.decrementAndGet(); } } | ||
} | ||
), | ||
Metric( | ||
metric = totalRequests, | ||
onRequest = (_, metric, me) => me.eval(EndpointMetric().onEndpointRequest { _ => me.eval { val _ = metric.incrementAndGet(); } }) | ||
) | ||
) | ||
|
||
val config = | ||
NettyConfig.default | ||
.eventLoopGroup(eventLoopGroup) | ||
.randomPort | ||
.withDontShutdownEventLoopGroupOnClose | ||
.noGracefulShutdown | ||
.requestTimeout(1.second) | ||
val options = NettyFutureServerOptions.customiseInterceptors | ||
.metricsInterceptor(new MetricsRequestInterceptor[Future](customMetrics, Seq.empty)) | ||
.options | ||
val bind = IO.fromFuture(IO.delay(NettyFutureServer(options, config).addEndpoints(List(e)).start())) | ||
|
||
Resource | ||
.make(bind)(server => IO.fromFuture(IO.delay(server.stop()))) | ||
.map(_.port) | ||
.use { port => | ||
basicRequest.post(uri"http://localhost:$port").body("test").send(backend).map { response => | ||
response.body should matchPattern { case Left(_) => } | ||
response.code shouldBe StatusCode.ServiceUnavailable | ||
// the metrics will only be updated when the endpoint's logic completes, which is 1 second after receiving the timeout response | ||
Thread.sleep(1100) | ||
activeRequests.get() shouldBe 0 | ||
totalRequests.get() shouldBe 1 | ||
} | ||
} | ||
.unsafeToFuture() | ||
} | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
...erver/sync/src/test/scala/sttp/tapir/server/netty/sync/NettySyncRequestTimeoutTests.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package sttp.tapir.server.netty | ||
|
||
import cats.effect.IO | ||
import cats.effect.unsafe.implicits.global | ||
import io.netty.channel.EventLoopGroup | ||
import org.scalatest.matchers.should.Matchers.* | ||
import ox.* | ||
import sttp.capabilities.WebSockets | ||
import sttp.capabilities.fs2.Fs2Streams | ||
import sttp.client3.* | ||
import sttp.model.StatusCode | ||
import sttp.tapir.* | ||
import sttp.tapir.server.interceptor.metrics.MetricsRequestInterceptor | ||
import sttp.tapir.server.metrics.{EndpointMetric, Metric} | ||
import sttp.tapir.server.netty.sync.{NettySyncServer, NettySyncServerOptions} | ||
import sttp.tapir.tests.Test | ||
|
||
import java.util.concurrent.atomic.AtomicInteger | ||
import scala.concurrent.Future | ||
import scala.concurrent.duration.DurationInt | ||
import org.slf4j.LoggerFactory | ||
|
||
class NettySyncRequestTimeoutTests(eventLoopGroup: EventLoopGroup, backend: SttpBackend[IO, Fs2Streams[IO] with WebSockets]): | ||
val logger = LoggerFactory.getLogger(getClass.getName) | ||
|
||
def tests(): List[Test] = List( | ||
Test("properly update metrics when a request times out") { | ||
val e = endpoint.post | ||
.in(stringBody) | ||
.out(stringBody) | ||
.serverLogicSuccess[Identity]: body => | ||
Thread.sleep(2000) | ||
body | ||
|
||
val activeRequests = new AtomicInteger() | ||
val totalRequests = new AtomicInteger() | ||
val customMetrics: List[Metric[Identity, AtomicInteger]] = List( | ||
Metric( | ||
metric = activeRequests, | ||
onRequest = (_, metric, me) => | ||
me.eval: | ||
EndpointMetric() | ||
.onEndpointRequest: _ => | ||
val _ = metric.incrementAndGet(); | ||
(): Identity[Unit] | ||
.onResponseBody: (_, _) => | ||
val _ = metric.decrementAndGet(); | ||
.onException: (_, _) => | ||
val _ = metric.decrementAndGet(); | ||
), | ||
Metric( | ||
metric = totalRequests, | ||
onRequest = (_, metric, me) => | ||
me.eval(EndpointMetric().onEndpointRequest: _ => | ||
val _ = metric.incrementAndGet(); | ||
) | ||
) | ||
) | ||
|
||
val config = | ||
NettyConfig.default | ||
.eventLoopGroup(eventLoopGroup) | ||
.randomPort | ||
.withDontShutdownEventLoopGroupOnClose | ||
.noGracefulShutdown | ||
.requestTimeout(1.second) | ||
val options = NettySyncServerOptions.customiseInterceptors | ||
.metricsInterceptor(new MetricsRequestInterceptor[Identity](customMetrics, Seq.empty)) | ||
.options | ||
|
||
Future.successful: | ||
supervised: | ||
val port = useInScope(NettySyncServer(options, config).addEndpoint(e).start())(_.stop()).port | ||
basicRequest | ||
.post(uri"http://localhost:$port") | ||
.body("test") | ||
.send(backend) | ||
.map: response => | ||
response.body should matchPattern { case Left(_) => } | ||
response.code shouldBe StatusCode.ServiceUnavailable | ||
// unlike in NettyFutureRequestTimeoutTest, here interruption works properly, and the metrics should be updated quickly | ||
Thread.sleep(100) | ||
activeRequests.get() shouldBe 0 | ||
totalRequests.get() shouldBe 1 | ||
.unsafeRunSync() | ||
} | ||
) |
Oops, something went wrong.