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

[PoolManager] - Synchronized -> Semaphore, wrap side-effects in Effect[F].delay #1934

Merged
merged 22 commits into from Aug 17, 2018
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -3,6 +3,7 @@ package client
package blaze

import cats.effect._
import cats.implicits._
import fs2.Stream

/** Create a HTTP1 client which will attempt to recycle connections */
Expand All @@ -14,24 +15,24 @@ object Http1Client {
*/
def apply[F[_]](config: BlazeClientConfig = BlazeClientConfig.defaultConfig)(
implicit F: Effect[F]): F[Client[F]] =
F.delay(mkClient(config))
mkClient(config)

def stream[F[_]: Effect](
config: BlazeClientConfig = BlazeClientConfig.defaultConfig): Stream[F, Client[F]] =
Stream.bracket(apply(config))(_.shutdown)

private[blaze] def mkClient[F[_]: Effect](config: BlazeClientConfig): Client[F] = {
private[blaze] def mkClient[F[_]: Effect](config: BlazeClientConfig): F[Client[F]] = {
val http1: ConnectionBuilder[F, BlazeConnection[F]] = Http1Support(config)
val pool = ConnectionManager.pool(

ConnectionManager.pool(
builder = http1,
maxTotal = config.maxTotalConnections,
maxWaitQueueLimit = config.maxWaitQueueLimit,
maxConnectionsPerRequestKey = config.maxConnectionsPerRequestKey,
responseHeaderTimeout = config.responseHeaderTimeout,
requestTimeout = config.requestTimeout,
executionContext = config.executionContext
)
BlazeClient(pool, config, pool.shutdown())
).map(pool => BlazeClient(pool, config, pool.shutdown()))
}

}
Expand Up @@ -18,10 +18,12 @@ object PooledHttp1Client {
maxWaitQueueLimit: Int = bits.DefaultMaxWaitQueueLimit,
maxConnectionsPerRequestKey: RequestKey => Int = _ => bits.DefaultMaxTotalConnections,
config: BlazeClientConfig = BlazeClientConfig.defaultConfig): Client[F] =
Http1Client.mkClient(
config.copy(
maxTotalConnections = maxTotalConnections,
maxWaitQueueLimit = maxWaitQueueLimit,
maxConnectionsPerRequestKey = maxConnectionsPerRequestKey
))
Effect[F].toIO {
Http1Client.mkClient(
config.copy(
maxTotalConnections = maxTotalConnections,
maxWaitQueueLimit = maxWaitQueueLimit,
maxConnectionsPerRequestKey = maxConnectionsPerRequestKey
))
}.unsafeRunSync()
Copy link
Contributor

Choose a reason for hiding this comment

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

If we're going to force toIO, might as well make the whole call return F[Client[F]] instead. There's no need to maintain source compat on a deprecated method since M7 of 0.18.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jmcardon Is it okay to just remove the PooledHttp1Client then? I only did this so that we could keep it around, but figured I'd get questions about it.

}
24 changes: 14 additions & 10 deletions client/src/main/scala/org/http4s/client/ConnectionManager.scala
Expand Up @@ -2,7 +2,8 @@ package org.http4s
package client

import cats.effect._

import cats.effect.concurrent.Semaphore
import cats.implicits._
import scala.concurrent.ExecutionContext
import scala.concurrent.duration.Duration

Expand Down Expand Up @@ -63,13 +64,16 @@ object ConnectionManager {
maxConnectionsPerRequestKey: RequestKey => Int,
responseHeaderTimeout: Duration,
requestTimeout: Duration,
executionContext: ExecutionContext): ConnectionManager[F, A] =
new PoolManager[F, A](
builder,
maxTotal,
maxWaitQueueLimit,
maxConnectionsPerRequestKey,
responseHeaderTimeout,
requestTimeout,
executionContext)
executionContext: ExecutionContext): F[ConnectionManager[F, A]] =
Semaphore.uncancelable(1).map { semaphore =>
new PoolManager[F, A](
builder,
maxTotal,
maxWaitQueueLimit,
maxConnectionsPerRequestKey,
responseHeaderTimeout,
requestTimeout,
semaphore,
executionContext)
}
}