Skip to content

Commit 9f23c18

Browse files
committed
Beautify
1 parent b46adb4 commit 9f23c18

File tree

2 files changed

+16
-27
lines changed

2 files changed

+16
-27
lines changed

src/main/scala/akkahttp/ReverseProxy.scala

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ import scala.concurrent.duration.DurationInt
2424
import scala.util.{Failure, Success}
2525

2626
/**
27-
* Conceptual PoC inspired by:
27+
* This conceptual all-in-one PoC is inspired by:
2828
* https://github.com/mathieuancelin/akka-http-reverse-proxy
2929
*
30-
* HTTP reverse proxy server echo PoC with:
30+
* Features ReverseProxy:
3131
* - Weighted round robin load balancing
3232
* - Retry on HTTP 5xx from target servers
3333
* - CircuitBreaker per target server to avoid overload
@@ -40,8 +40,8 @@ import scala.util.{Failure, Success}
4040
* HTTP client(s) --> ReverseProxy --> remote target server(s)
4141
*
4242
* Remarks:
43-
* - The target server selection works via the "Host" HTTP header
44-
* - Local/Remote target servers are designed to be flaky to show retry/circuit breaker behavior
43+
* - The target server selection is via the "Host" HTTP header
44+
* - Local/Remote target servers are designed to be flaky to show Retry/CircuitBreaker behavior
4545
* - On top of the built in client, you may also try other clients
4646
* - This PoC may not scale well, possible bottlenecks are:
4747
* - Combination of Retry/CircuitBreaker
@@ -278,29 +278,17 @@ object Retry {
278278
private[this] def retryPromise[T](times: Int, promise: Promise[T], failure: Option[Throwable],
279279
f: => Future[T])(implicit ec: ExecutionContext): Unit = {
280280
(times, failure) match {
281-
case (0, Some(e)) =>
282-
promise.tryFailure(e)
283-
case (0, None) =>
284-
promise.tryFailure(new RuntimeException("Failure, but lost track of exception"))
281+
case (0, Some(e)) => promise.tryFailure(e)
282+
case (0, None) => promise.tryFailure(new RuntimeException("Failure, but lost track of exception"))
285283
case (_, _) =>
286284
f.onComplete {
287-
case Success(t) =>
288-
t match {
289-
case httpResponse: HttpResponse =>
290-
val code = httpResponse.status.intValue()
291-
val id = httpResponse.getHeader("X-Correlation-ID").orElse(RawHeader("X-Correlation-ID", "N/A")).value()
292-
if (code >= 500) {
293-
logger.info(s"ReverseProxy got 5xx server error for id: $id. Retries left: ${times - 1}")
294-
retryPromise[T](times - 1, promise, Some(new RuntimeException(s"Received: $code from target server")), f)
295-
} else {
296-
promise.trySuccess(t)
297-
}
298-
case _ =>
299-
promise.tryFailure(new RuntimeException("This should not happen: Expected type HttpResponse, but got sth else"))
300-
}
301-
case Failure(e) =>
302-
retryPromise[T](times - 1, promise, Some(e), f)
303-
}(ec)
285+
case Success(httpResponse: HttpResponse) if httpResponse.status.intValue() >= 500 =>
286+
val id = httpResponse.getHeader("X-Correlation-ID").orElse(RawHeader("X-Correlation-ID", "N/A")).value()
287+
logger.info(s"ReverseProxy got 5xx server error for id: $id. Retries left: ${times - 1}")
288+
retryPromise[T](times - 1, promise, Some(new RuntimeException(s"Received: ${httpResponse.status.intValue()} from target server")), f)
289+
case Success(t) => promise.trySuccess(t)
290+
case Failure(e) => retryPromise[T](times - 1, promise, Some(e), f)
291+
}
304292
}
305293
}
306294
}

src/test/scala/ReverseProxySimulation.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import scala.concurrent.duration.*
66

77
/**
88
* Start [[akkahttp.ReverseProxy]]
9-
* Run this simulation with the cmd:
9+
* Run this simulation from cmd shell with:
1010
* sbt 'Gatling/testOnly ReverseProxySimulation'
1111
*/
1212
class ReverseProxySimulation extends Simulation {
@@ -26,10 +26,11 @@ class ReverseProxySimulation extends Simulation {
2626
.header("Host", "local")
2727
.header("X-Correlation-ID", session => s"1-${session("correlationId").as[Int]}")
2828
.check(status.is(200))
29+
.check(status.saveAs("responseStatus"))
2930
.check(header("X-Correlation-ID").saveAs("responseCorrelationId"))
3031
)
3132
.exec(session => {
32-
println(s"Got response for id: ${session("responseCorrelationId").as[String]}")
33+
println(s"Got: ${session.status} response with HTTP status: ${session("responseStatus").as[String]} for id: ${session("responseCorrelationId").as[String]}")
3334
session
3435
})
3536
.exec(session => session.set("correlationId", session("correlationId").as[Int] + 1))

0 commit comments

Comments
 (0)