akka.http.scaladsl.settings.ConnectionPoolSettings Scala Examples
The following examples show how to use akka.http.scaladsl.settings.ConnectionPoolSettings.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: ClickhouseHostHealth.scala From clickhouse-scala-client with GNU Lesser General Public License v3.0 | 5 votes |
package com.crobox.clickhouse.balancing.discovery.health import akka.NotUsed import akka.actor.{ActorSystem, Cancellable} import akka.http.scaladsl.Http import akka.http.scaladsl.model._ import akka.http.scaladsl.settings.ConnectionPoolSettings import akka.http.scaladsl.unmarshalling.Unmarshaller import akka.stream.Materializer import akka.stream.scaladsl.{Flow, Source} import com.crobox.clickhouse.internal.ClickhouseResponseParser import scala.concurrent.duration._ import scala.concurrent.{ExecutionContext, Future} import scala.util.{Failure, Success, Try} object ClickhouseHostHealth extends ClickhouseResponseParser { sealed trait ClickhouseHostStatus { val host: Uri val code: String } case class Alive(host: Uri) extends ClickhouseHostStatus { override val code: String = "ok" } case class Dead(host: Uri, reason: Throwable) extends ClickhouseHostStatus { override val code: String = "nok" } def healthFlow(host: Uri)( implicit system: ActorSystem, materializer: Materializer, executionContext: ExecutionContext ): Source[ClickhouseHostStatus, Cancellable] = { val healthCheckInterval: FiniteDuration = system.settings.config .getDuration("connection.health-check.interval") .getSeconds.seconds val healthCheckTimeout: FiniteDuration = system.settings.config .getDuration("connection.health-check.timeout") .getSeconds.seconds val healthCachedPool = Http(system).cachedHostConnectionPool[Int]( host.authority.host.address(), host.effectivePort, settings = ConnectionPoolSettings(system) .withMaxConnections(1) .withMinConnections(1) .withMaxOpenRequests(2) .withMaxRetries(3) .withUpdatedConnectionSettings( _.withIdleTimeout(healthCheckTimeout + healthCheckInterval).withConnectingTimeout(healthCheckTimeout) ) ) Source .tick(0.milliseconds, healthCheckInterval, 0) .map(tick => { (HttpRequest(method = HttpMethods.GET, uri = host), tick) }) .via(healthCachedPool) .via(parsingFlow(host)) } private[health] def parsingFlow[T]( host: Uri )(implicit ec: ExecutionContext, mat: Materializer): Flow[(Try[HttpResponse], T), ClickhouseHostStatus, NotUsed] = Flow[(Try[HttpResponse], T)].mapAsync(1) { case (Success(response @ akka.http.scaladsl.model.HttpResponse(StatusCodes.OK, _, _, _)), _) => Unmarshaller.stringUnmarshaller(decodeResponse(response).entity) .map(splitResponse) .map( stringResponse => if (stringResponse.equals(Seq("Ok."))) { Alive(host) } else { Dead(host, new IllegalArgumentException(s"Got wrong result $stringResponse")) } ) case (Success(response), _) => Future.successful(Dead(host, new IllegalArgumentException(s"Got response with status code ${response.status}"))) case (Failure(ex), _) => Future.successful(Dead(host, ex)) } }
Example 2
Source File: ClusterConnectionFlow.scala From clickhouse-scala-client with GNU Lesser General Public License v3.0 | 5 votes |
package com.crobox.clickhouse.balancing.discovery.cluster import akka.actor.{ActorSystem, Cancellable} import akka.http.scaladsl.Http import akka.http.scaladsl.model.Uri import akka.http.scaladsl.settings.ConnectionPoolSettings import akka.stream.Materializer import akka.stream.scaladsl.Source import com.crobox.clickhouse.balancing.discovery.ConnectionManagerActor.Connections import com.crobox.clickhouse.internal.QuerySettings.ReadQueries import com.crobox.clickhouse.internal.{ClickhouseHostBuilder, ClickhouseQueryBuilder, ClickhouseResponseParser, QuerySettings} import com.typesafe.scalalogging.LazyLogging import scala.concurrent.duration._ import scala.concurrent.{ExecutionContext, Future} private[clickhouse] object ClusterConnectionFlow extends ClickhouseQueryBuilder with ClickhouseResponseParser with LazyLogging { def clusterConnectionsFlow( targetHost: => Future[Uri], scanningInterval: FiniteDuration, cluster: String )(implicit system: ActorSystem, materializer: Materializer, ec: ExecutionContext): Source[Connections, Cancellable] = { val http = Http(system) val settings = ConnectionPoolSettings(system) .withMaxConnections(1) .withMinConnections(1) .withMaxOpenRequests(2) .withMaxRetries(3) .withUpdatedConnectionSettings( _.withIdleTimeout(scanningInterval.plus(1.second)) ) Source .tick(0.millis, scanningInterval, {}) .mapAsync(1)(_ => targetHost) .mapAsync(1)(host => { val query = s"SELECT host_address FROM system.clusters WHERE cluster='$cluster'" val request = toRequest(host, query, None, QuerySettings(readOnly = ReadQueries, idempotent = Some(true)), None)( system.settings.config ) processClickhouseResponse(http.singleRequest(request, settings = settings), query, host, None) .map(splitResponse) .map(_.toSet.filter(_.nonEmpty)) .map(result => { if (result.isEmpty) { throw new IllegalArgumentException( s"Could not determine clickhouse cluster hosts for cluster $cluster and host $host. " + s"This could indicate that you are trying to use the cluster balancer to connect to a non cluster based clickhouse server. " + s"Please use the `SingleHostQueryBalancer` in that case." ) } Connections(result.map(ClickhouseHostBuilder.toHost(_, Some(8123)))) }) }) } }
Example 3
Source File: PoolingRestClient.scala From openwhisk with Apache License 2.0 | 5 votes |
package org.apache.openwhisk.http import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._ import akka.http.scaladsl.marshalling._ import akka.http.scaladsl.model._ import akka.http.scaladsl.settings.ConnectionPoolSettings import akka.http.scaladsl.unmarshalling._ import akka.stream.{ActorMaterializer, OverflowStrategy, QueueOfferResult} import akka.stream.scaladsl.{Flow, _} import spray.json._ import scala.concurrent.{ExecutionContext, Future, Promise} import scala.concurrent.duration._ import scala.util.{Failure, Success, Try} def requestJson[T: RootJsonReader](futureRequest: Future[HttpRequest]): Future[Either[StatusCode, T]] = request(futureRequest).flatMap { response => if (response.status.isSuccess) { Unmarshal(response.entity.withoutSizeLimit).to[T].map(Right.apply) } else { Unmarshal(response.entity).to[String].flatMap { body => val statusCode = response.status val reason = if (body.nonEmpty) s"${statusCode.reason} (details: $body)" else statusCode.reason val customStatusCode = StatusCodes .custom(intValue = statusCode.intValue, reason = reason, defaultMessage = statusCode.defaultMessage) // This is important, as it drains the entity stream. // Otherwise the connection stays open and the pool dries up. response.discardEntityBytes().future.map(_ => Left(customStatusCode)) } } } def shutdown(): Future[Unit] = Future.successful(materializer.shutdown()) } object PoolingRestClient { def mkRequest(method: HttpMethod, uri: Uri, body: Future[MessageEntity] = Future.successful(HttpEntity.Empty), headers: List[HttpHeader] = List.empty)(implicit ec: ExecutionContext): Future[HttpRequest] = { body.map { b => HttpRequest(method, uri, headers, b) } } def mkJsonRequest(method: HttpMethod, uri: Uri, body: JsValue, headers: List[HttpHeader] = List.empty)( implicit ec: ExecutionContext): Future[HttpRequest] = { val b = Marshal(body).to[MessageEntity] mkRequest(method, uri, b, headers) } }
Example 4
Source File: RequestManager.scala From wix-http-testkit with MIT License | 5 votes |
package com.wix.e2e.http.client.internals import akka.http.scaladsl.Http import akka.http.scaladsl.model.TransferEncodings.chunked import akka.http.scaladsl.model.headers.{ProductVersion, `Transfer-Encoding`, `User-Agent`} import akka.http.scaladsl.settings.ConnectionPoolSettings import akka.stream.StreamTcpException import com.wix.e2e.http._ import com.wix.e2e.http.exceptions.ConnectionRefusedException import com.wix.e2e.http.info.HttpTestkitVersion import com.wix.e2e.http.utils._ import scala.concurrent.Future import scala.concurrent.duration._ trait RequestManager[R] { def apply(path: String, but: RequestTransformer = identity, withTimeout: FiniteDuration = 5.seconds)(implicit baseUri: BaseUri): R } class NonBlockingRequestManager(request: HttpRequest) extends RequestManager[Future[HttpResponse]] { def apply(path: String, but: RequestTransformer, withTimeout: FiniteDuration)(implicit baseUri: BaseUri): Future[HttpResponse] = { val transformed = Seq(composeUrlFor(baseUri, path), but) .foldLeft(request) { case (r, tr) => tr(r) } import WixHttpTestkitResources.{executionContext, materializer, system} Http().singleRequest(request = transformed, settings = settingsWith(withTimeout)) .map( recreateTransferEncodingHeader ) .flatMap( _.toStrict(withTimeout) ) .recoverWith( { case _: StreamTcpException => Future.failed(new ConnectionRefusedException(baseUri)) } ) } private def composeUrlFor(baseUri: BaseUri, path: String): RequestTransformer = _.copy(uri = baseUri.asUriWith(path) ) private def recreateTransferEncodingHeader(r: HttpResponse) = if ( !r.entity.isChunked ) r else { val encodings = r.header[`Transfer-Encoding`] .map( _.encodings ) .getOrElse( Seq.empty ) r.removeHeader("Transfer-Encoding") .addHeader(`Transfer-Encoding`(chunked, encodings:_*)) } private def settingsWith(timeout: FiniteDuration) = { val settings = ConnectionPoolSettings(WixHttpTestkitResources.system) settings.withConnectionSettings( settings.connectionSettings .withConnectingTimeout(timeout) .withIdleTimeout(timeout) .withUserAgentHeader(Some(`User-Agent`(ProductVersion("client-http-testkit", HttpTestkitVersion)))) ) .withMaxConnections(32) .withPipeliningLimit(4) .withMaxRetries(0) } } class BlockingRequestManager(request: HttpRequest) extends RequestManager[HttpResponse] { def apply(path: String, but: RequestTransformer, withTimeout: FiniteDuration)(implicit baseUri: BaseUri): HttpResponse = waitFor(nonBlockingRequestManager(path, but, withTimeout))(withTimeout + 1.second) private val nonBlockingRequestManager = new NonBlockingRequestManager(request) }
Example 5
Source File: RequestRunner.scala From aws-spi-akka-http with Apache License 2.0 | 5 votes |
package com.github.matsluni.akkahttpspi import java.util.concurrent.CompletableFuture import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.HttpRequest import akka.http.scaladsl.settings.ConnectionPoolSettings import akka.stream.Materializer import akka.stream.scaladsl.{Keep, Sink} import org.slf4j.LoggerFactory import software.amazon.awssdk.http.SdkHttpFullResponse import software.amazon.awssdk.http.async.SdkAsyncHttpResponseHandler import scala.compat.java8.FutureConverters import scala.concurrent.ExecutionContext import scala.collection.JavaConverters._ class RequestRunner(connectionPoolSettings: ConnectionPoolSettings)(implicit sys: ActorSystem, ec: ExecutionContext, mat: Materializer) { val logger = LoggerFactory.getLogger(this.getClass) def run(httpRequest: HttpRequest, handler: SdkAsyncHttpResponseHandler): CompletableFuture[Void] = { val result = Http() .singleRequest(httpRequest, settings = connectionPoolSettings) .flatMap { response => val sdkResponse = SdkHttpFullResponse.builder() .headers(response.headers.groupBy(_.name()).map{ case (k, v) => k -> v.map(_.value()).asJava }.asJava) .statusCode(response.status.intValue()) .statusText(response.status.reason) .build handler.onHeaders(sdkResponse) val (complete, publisher) = response .entity .dataBytes .map(_.asByteBuffer) .alsoToMat(Sink.ignore)(Keep.right) .toMat(Sink.asPublisher(fanout = false))(Keep.both) .run() handler.onStream(publisher) complete } result.failed.foreach(handler.onError) FutureConverters.toJava(result.map(_ => null: Void)).toCompletableFuture } }
Example 6
Source File: AkkaUtils.scala From CM-Well with Apache License 2.0 | 5 votes |
package cmwell.tools.data.utils.akka import java.net.InetSocketAddress import akka.http.scaladsl.ClientTransport import akka.http.scaladsl.coding.{Deflate, Gzip, NoCoding} import akka.http.scaladsl.model.HttpResponse import akka.http.scaladsl.model.headers.{HttpEncoding, HttpEncodings} import akka.http.scaladsl.settings.{ClientConnectionSettings, ConnectionPoolSettings} import com.typesafe.config.ConfigFactory object AkkaUtils { def generateClientConnectionSettings(userAgent : String) ={ val settings = ClientConnectionSettings( ConfigFactory .parseString(s"akka.http.host-connection-pool.client.user-agent-header=$userAgent") .withFallback(config) ) val proxyHost = config.getString("akka.http.client.proxy.https.host") val proxyPort = config.getInt("akka.http.client.proxy.https.port") if (proxyHost!="") { val httpsProxyTransport = ClientTransport.httpsProxy(InetSocketAddress.createUnresolved(proxyHost, proxyPort)) settings.withTransport(httpsProxyTransport) } else settings } def generateConnectionPoolSettings(userAgent : Option[String] = None) ={ val settings = userAgent.fold{ ConnectionPoolSettings(config) .withConnectionSettings(ClientConnectionSettings(config)) }{userAgentV => ConnectionPoolSettings( ConfigFactory .parseString(s"akka.http.host-connection-pool.client.user-agent-header=$userAgentV") .withFallback(config) ) } val proxyHost = config.getString("akka.http.client.proxy.https.host") val proxyPort = config.getInt("akka.http.client.proxy.https.port") if (proxyHost!="") { val httpsProxyTransport = ClientTransport.httpsProxy(InetSocketAddress.createUnresolved(proxyHost, proxyPort)) settings.withTransport(httpsProxyTransport) } else settings } def decodeResponse(response: HttpResponse) = { val decoder = response.encoding match { case HttpEncodings.gzip ⇒ Gzip case HttpEncodings.deflate ⇒ Deflate case HttpEncodings.identity ⇒ NoCoding case HttpEncoding(_) ⇒ ??? } decoder.decodeMessage(response) } }
Example 7
Source File: JavaConverters.scala From squbs with Apache License 2.0 | 5 votes |
package akka.http.org.squbs.util import java.util.Optional import akka.NotUsed import akka.http.impl.util.JavaMapping import akka.http.javadsl.{model => jm} import akka.http.scaladsl.Http.HostConnectionPool import akka.http.scaladsl.HttpsConnectionContext import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import akka.http.scaladsl.settings.ConnectionPoolSettings import akka.http.{javadsl => jd} import akka.japi.Pair import akka.stream.scaladsl.{BidiFlow, Flow} import akka.stream.{javadsl => js} import scala.util.Try object JavaConverters { def fromJava(connectionContext: Optional[jd.HttpsConnectionContext], settings: Optional[jd.settings.ConnectionPoolSettings]): (Option[HttpsConnectionContext], Option[ConnectionPoolSettings]) = { import scala.compat.java8.OptionConverters._ val cCtx = connectionContext.asScala.asInstanceOf[Option[HttpsConnectionContext]] val sSettings = settings.asScala.asInstanceOf[Option[ConnectionPoolSettings]] (cCtx, sSettings) } def toJava[In1, Out1, In2, Out2, Context](bidiFlow: BidiFlow[(In1, Context), (Out1, Context), (In2, Context), (Out2, Context), NotUsed]): js.BidiFlow[Pair[In1, Context], Pair[Out1, Context], Pair[In2, Context], Pair[Out2, Context], NotUsed] = { implicit val sIn1Mapping = JavaMapping.identity[In1] implicit val sOut1Mapping = JavaMapping.identity[Out1] implicit val sIn2Mapping = JavaMapping.identity[In2] implicit val sOut2Mapping = JavaMapping.identity[Out2] implicit val contextMapping = JavaMapping.identity[Context] val javaToScalaAdapter = JavaMapping.adapterBidiFlow[Pair[In1, Context], (In1, Context), (Out2, Context), Pair[Out2, Context]] val scalaToJavaAdapter = JavaMapping.adapterBidiFlow[Pair[In2, Context], (In2, Context), (Out1, Context), Pair[Out1, Context]].reversed javaToScalaAdapter.atop(bidiFlow).atop(scalaToJavaAdapter).asJava } private def adaptTupleFlow[T](scalaFlow: Flow[(HttpRequest, T), (Try[HttpResponse], T), HostConnectionPool]): js.Flow[Pair[jm.HttpRequest, T], Pair[Try[jm.HttpResponse], T], jd.HostConnectionPool] = { implicit val _ = JavaMapping.identity[T] implicit object HostConnectionPoolMapping extends JavaMapping[jd.HostConnectionPool, HostConnectionPool] { def toScala(javaObject: jd.HostConnectionPool): HostConnectionPool = throw new UnsupportedOperationException("jd.HostConnectionPool cannot be converted to Scala") def toJava(scalaObject: HostConnectionPool): jd.HostConnectionPool = scalaObject.toJava } JavaMapping.toJava(scalaFlow)(JavaMapping.flowMapping[Pair[jm.HttpRequest, T], (HttpRequest, T), Pair[Try[jm.HttpResponse], T], (Try[HttpResponse], T), jd.HostConnectionPool, HostConnectionPool]) } def toJava[T](flow: Flow[(HttpRequest, T), (Try[HttpResponse], T), HostConnectionPool]): js.Flow[Pair[jm.HttpRequest, T], Pair[Try[jm.HttpResponse], T], jd.HostConnectionPool] = { adaptTupleFlow[T](flow) } def toScala(uri: akka.http.javadsl.model.Uri) = JavaMapping.toScala(uri) }
Example 8
Source File: AkkaHttpClient.scala From sttp with Apache License 2.0 | 5 votes |
package sttp.client.akkahttp import akka.actor.ActorSystem import akka.event.LoggingAdapter import akka.http.scaladsl.model.ws.{Message, WebSocketRequest, WebSocketUpgradeResponse} import akka.http.scaladsl.model.{HttpRequest, HttpResponse} import akka.http.scaladsl.server.{ExceptionHandler, RejectionHandler, Route, RoutingLog} import akka.http.scaladsl.settings.{ClientConnectionSettings, ConnectionPoolSettings, ParserSettings, RoutingSettings} import akka.http.scaladsl.{Http, HttpsConnectionContext} import akka.stream.Materializer import akka.stream.scaladsl.Flow import scala.concurrent.{ExecutionContext, ExecutionContextExecutor, Future} trait AkkaHttpClient { def singleRequest( request: HttpRequest, settings: ConnectionPoolSettings ): Future[HttpResponse] def singleWebsocketRequest[WS_RESULT]( request: WebSocketRequest, clientFlow: Flow[Message, Message, WS_RESULT], settings: ClientConnectionSettings )(implicit ec: ExecutionContext, mat: Materializer): Future[(WebSocketUpgradeResponse, WS_RESULT)] } object AkkaHttpClient { def default( system: ActorSystem, connectionContext: Option[HttpsConnectionContext], customLog: Option[LoggingAdapter] ): AkkaHttpClient = new AkkaHttpClient { private val http = Http()(system) override def singleRequest( request: HttpRequest, settings: ConnectionPoolSettings ): Future[HttpResponse] = { http.singleRequest( request, connectionContext.getOrElse(http.defaultClientHttpsContext), settings, customLog.getOrElse(system.log) ) } override def singleWebsocketRequest[WS_RESULT]( request: WebSocketRequest, clientFlow: Flow[Message, Message, WS_RESULT], settings: ClientConnectionSettings )(implicit ec: ExecutionContext, mat: Materializer): Future[(WebSocketUpgradeResponse, WS_RESULT)] = { val (wsResponse, wsResult) = http.singleWebSocketRequest( request, clientFlow, connectionContext.getOrElse(http.defaultClientHttpsContext), None, settings, customLog.getOrElse(system.log) ) wsResponse.map((_, wsResult)) } } def stubFromAsyncHandler(run: HttpRequest => Future[HttpResponse]): AkkaHttpClient = new AkkaHttpClient { def singleRequest(request: HttpRequest, settings: ConnectionPoolSettings): Future[HttpResponse] = run(request) override def singleWebsocketRequest[WS_RESULT]( request: WebSocketRequest, clientFlow: Flow[Message, Message, WS_RESULT], settings: ClientConnectionSettings )(implicit ec: ExecutionContext, mat: Materializer): Future[(WebSocketUpgradeResponse, WS_RESULT)] = Future.failed(new RuntimeException("Websockets are not supported")) } def stubFromRoute(route: Route)(implicit routingSettings: RoutingSettings, parserSettings: ParserSettings, materializer: Materializer, routingLog: RoutingLog, executionContext: ExecutionContextExecutor = null, rejectionHandler: RejectionHandler = RejectionHandler.default, exceptionHandler: ExceptionHandler = null ): AkkaHttpClient = stubFromAsyncHandler(Route.asyncHandler(route)) }
Example 9
Source File: LongPollingPool.scala From skuber with Apache License 2.0 | 5 votes |
package skuber.api.watch import akka.NotUsed import akka.actor.ActorSystem import akka.http.scaladsl.settings.{ClientConnectionSettings, ConnectionPoolSettings} import akka.http.scaladsl.{Http, HttpsConnectionContext} import akka.stream.Materializer import skuber.api.client.Pool import scala.concurrent.duration._ private[api] object LongPollingPool { def apply[T](schema: String, host: String, port: Int, poolIdleTimeout: Duration, httpsConnectionContext: Option[HttpsConnectionContext], clientConnectionSettings: ClientConnectionSettings)(implicit mat: Materializer, system: ActorSystem): Pool[T] = { schema match { case "http" => Http().newHostConnectionPool[T]( host, port, buildHostConnectionPool(poolIdleTimeout, clientConnectionSettings, system) ).mapMaterializedValue(_ => NotUsed) case "https" => Http().newHostConnectionPoolHttps[T]( host, port, httpsConnectionContext.getOrElse(Http().defaultClientHttpsContext), buildHostConnectionPool(poolIdleTimeout, clientConnectionSettings, system) ).mapMaterializedValue(_ => NotUsed) case unsupported => throw new IllegalArgumentException(s"Schema $unsupported is not supported") } } private def buildHostConnectionPool[T](poolIdleTimeout: Duration, clientConnectionSettings: ClientConnectionSettings, system: ActorSystem) = { ConnectionPoolSettings(system) .withMaxConnections(1) // Limit number the of open connections to one .withPipeliningLimit(1) // Limit pipelining of requests to one .withMaxRetries(0) // Disables retries .withIdleTimeout(poolIdleTimeout) // Automatically shutdown connection pool after timeout .withConnectionSettings(clientConnectionSettings) } }