Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[sttp-finagle] Adds concurrent cache to prevent Finagle client leak #1393

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ project/metals.sbt

# scala-native
lowered.hnir

# VSCode
.vscode/
4 changes: 4 additions & 0 deletions .jvmopts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-Xms512M
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately this config should be removed as it interfere with our CI JVM settings.
To solve problems with OOME in tests I would advise to change sbt memory settings, maybe sbt -mem 4096 is enought

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, I've removed this file.

-Xmx4096M
-Xss2M
-XX:MaxMetaspaceSize=1024M
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.twitter.io.Buf
import com.twitter.io.Buf.{ByteArray, ByteBuffer}
import com.twitter.util
import com.twitter.util.{Duration, Future => TFuture}
import java.util.concurrent.ConcurrentHashMap
import sttp.capabilities.Effect
import sttp.client3.internal.{BodyFromResponseAs, FileHelpers, SttpFile, Utf8}
import sttp.client3.testing.SttpBackendStub
Expand All @@ -38,11 +39,12 @@ import sttp.client3.{
import sttp.model._
import sttp.monad.MonadError
import sttp.monad.syntax._

import scala.io.Source
import scala.jdk.CollectionConverters._

class FinagleBackend(client: Option[Client] = None) extends SttpBackend[TFuture, Any] {
type PE = Any with Effect[TFuture]
private[this] val clientCache = new ConcurrentHashMap[String, Service[http.Request, FResponse]].asScala
override def send[T, R >: PE](request: Request[T, R]): TFuture[Response[T]] =
adjustExceptions(request) {
val service = getClient(client, request)
Expand Down Expand Up @@ -204,9 +206,13 @@ class FinagleBackend(client: Option[Client] = None) extends SttpBackend[TFuture,
case _ => Http.client
}
}
client
.withRequestTimeout(Duration.fromMilliseconds(request.options.readTimeout.toMillis))
.newService(uriToFinagleDestination(request.uri))
val finagleDest = uriToFinagleDestination(request.uri)
clientCache.getOrElseUpdate(
finagleDest,
client
.withRequestTimeout(Duration.fromMilliseconds(request.options.readTimeout.toMillis))
.newService(finagleDest)
)
}

private def uriToFinagleDestination(uri: Uri): String = {
Expand Down