From 9b855fc55e35f35ec4368d1387f2526b4245aab5 Mon Sep 17 00:00:00 2001 From: Thanh Le Date: Sun, 17 Nov 2024 10:40:35 +0100 Subject: [PATCH] Use http4s.Uri in Http4sClient This is to avoid conversion between ElasticNodeEndpoint to http4s.Uri for every request. Which'll save us some allocation, hence more performance. This also allows users to pass http4s.Uri directly to Http4sClient. --- .../elastic4s/http4s/Http4sClient.scala | 33 ++++++++++++++++++- .../http4s/RequestResponseConverters.scala | 11 ++----- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/elastic4s-client-http4s/src/main/scala/com/sksamuel/elastic4s/http4s/Http4sClient.scala b/elastic4s-client-http4s/src/main/scala/com/sksamuel/elastic4s/http4s/Http4sClient.scala index 185936f2f..c644780e3 100644 --- a/elastic4s-client-http4s/src/main/scala/com/sksamuel/elastic4s/http4s/Http4sClient.scala +++ b/elastic4s-client-http4s/src/main/scala/com/sksamuel/elastic4s/http4s/Http4sClient.scala @@ -24,6 +24,21 @@ object Http4sClient { override def run[A](fa: IO[A], cb: Either[Throwable, A] => Unit): Unit = fa.unsafeRunAsync(cb) } + Http4sClient( + client = client, + endpoint = endpoint, + runner = ioRunner + ) + } + + def usingIO( + client: http4s.client.Client[IO], + endpoint: http4s.Uri, + )(implicit runtime: IORuntime): Http4sClient[IO] = { + val ioRunner = new CallbackRunner[IO] { + override def run[A](fa: IO[A], cb: Either[Throwable, A] => Unit): Unit = fa.unsafeRunAsync(cb) + } + new Http4sClient( client = client, endpoint = endpoint, @@ -31,11 +46,27 @@ object Http4sClient { ) } + def apply[F[_] : Async : Files]( + client: http4s.client.Client[F], + endpoint: ElasticNodeEndpoint, + runner: CallbackRunner[F], + ): Http4sClient[F] = { + val uri = http4s.Uri( + scheme = Some(http4s.Uri.Scheme.http), + authority = Some(http4s.Uri.Authority(host = http4s.Uri.RegName(endpoint.host), port = Some(endpoint.port))), + ) + new Http4sClient( + client = client, + endpoint = uri, + runner = runner + ) + } + } class Http4sClient[F[_] : Async : Files]( client: http4s.client.Client[F], - endpoint: ElasticNodeEndpoint, + endpoint: http4s.Uri, runner: CallbackRunner[F], ) extends elastic4s.HttpClient with RequestResponseConverters { diff --git a/elastic4s-client-http4s/src/main/scala/com/sksamuel/elastic4s/http4s/RequestResponseConverters.scala b/elastic4s-client-http4s/src/main/scala/com/sksamuel/elastic4s/http4s/RequestResponseConverters.scala index eaf56b1b3..6d018b6b3 100644 --- a/elastic4s-client-http4s/src/main/scala/com/sksamuel/elastic4s/http4s/RequestResponseConverters.scala +++ b/elastic4s-client-http4s/src/main/scala/com/sksamuel/elastic4s/http4s/RequestResponseConverters.scala @@ -3,7 +3,6 @@ package com.sksamuel.elastic4s.http4s import cats.effect.Async import cats.syntax.all._ import com.sksamuel.elastic4s -import com.sksamuel.elastic4s.ElasticNodeEndpoint import fs2.io.file.Files import org.http4s @@ -12,15 +11,11 @@ import scala.language.higherKinds trait RequestResponseConverters extends Elastic4sEntityEncoders { def elasticRequestToHttp4sRequest[F[_] : Async : Files]( - endpoint: ElasticNodeEndpoint, + endpoint: http4s.Uri, request: elastic4s.ElasticRequest, ): http4s.Request[F] = { - val uri = http4s.Uri( - scheme = http4s.Uri.Scheme.fromString(endpoint.protocol).toOption, - authority = http4s.Uri.Authority( - host = http4s.Uri.RegName(endpoint.host), - port = endpoint.port.some - ).some, + val uri = + endpoint.copy( path = http4s.Uri.Path(request.endpoint.stripPrefix("/").split('/').map(http4s.Uri.Path.Segment(_)).toVector), query = http4s.Query.fromPairs(request.params.toList: _*) )