Skip to content

Commit

Permalink
Backport http4s#3330: Add executionContext as explicit parameter to B…
Browse files Browse the repository at this point in the history
…lazeBuilder.apply
  • Loading branch information
albertoadami authored and rossabaker committed Apr 27, 2020
1 parent 4c26c36 commit eaaf50b
Show file tree
Hide file tree
Showing 15 changed files with 59 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ class BlazeServerBuilder[F[_]](
}

object BlazeServerBuilder {
@deprecated("Use BlazeServerBuilder.apply with explicit executionContext instead", "0.20.22")
def apply[F[_]](implicit F: ConcurrentEffect[F], timer: Timer[F]): BlazeServerBuilder[F] =
new BlazeServerBuilder(
socketAddress = defaults.SocketAddress,
Expand All @@ -427,6 +428,30 @@ object BlazeServerBuilder {
channelOptions = ChannelOptions(Vector.empty)
)

def apply[F[_]](executionContext: ExecutionContext)(
implicit F: ConcurrentEffect[F],
timer: Timer[F]): BlazeServerBuilder[F] =
new BlazeServerBuilder(
socketAddress = defaults.SocketAddress,
executionContext = executionContext,
responseHeaderTimeout = defaults.ResponseTimeout,
idleTimeout = defaults.IdleTimeout,
isNio2 = false,
connectorPoolSize = DefaultPoolSize,
bufferSize = 64 * 1024,
selectorThreadFactory = defaultThreadSelectorFactory,
enableWebSockets = true,
sslBits = None,
isHttp2Enabled = false,
maxRequestLineLen = 4 * 1024,
maxHeadersLen = 40 * 1024,
chunkBufferMaxSize = 1024 * 1024,
httpApp = defaultApp[F],
serviceErrorHandler = DefaultServiceErrorHandler[F],
banner = defaults.Banner,
channelOptions = ChannelOptions(Vector.empty)
)

private def defaultApp[F[_]: Applicative]: HttpApp[F] =
Kleisli(_ => Response[F](Status.NotFound).pure[F])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.http4s.{Http4sSpec, HttpApp}
import scala.concurrent.duration._
import scala.io.Source
import scala.util.Try
import scala.concurrent.ExecutionContext.global

/**
* Test cases for mTLS support in blaze server
Expand All @@ -29,9 +30,8 @@ class BlazeServerMtlsSpec extends Http4sSpec {
}

def builder: BlazeServerBuilder[IO] =
BlazeServerBuilder[IO]
BlazeServerBuilder[IO](global)
.withResponseHeaderTimeout(1.second)
.withExecutionContext(testExecutionContext)

val service: HttpApp[IO] = HttpApp {
case req @ GET -> Root / "dummy" =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import scala.concurrent.duration._
import scala.io.Source
import org.specs2.execute.Result
import org.http4s.multipart.Multipart
import scala.concurrent.ExecutionContext.global

class BlazeServerSpec extends Http4sSpec {

def builder =
BlazeServerBuilder[IO]
BlazeServerBuilder[IO](global)
.withResponseHeaderTimeout(1.second)
.withExecutionContext(testExecutionContext)

val service: HttpApp[IO] = HttpApp {
case GET -> Root / "thread" / "routing" =>
Expand Down
4 changes: 3 additions & 1 deletion docs/src/main/tut/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,14 @@ implicit val timer: Timer[IO] = IO.timer(global)
Finish setting up our server:

```tut:book
import scala.concurrent.ExecutionContext.global
val app = HttpRoutes.of[IO] {
case GET -> Root / "hello" / name =>
Ok(s"Hello, $name.")
}.orNotFound
val server = BlazeServerBuilder[IO].bindHttp(8080, "localhost").withHttpApp(app).resource
val server = BlazeServerBuilder[IO](global).bindHttp(8080, "localhost").withHttpApp(app).resource
```

We'll start the server in the background. The `IO.never` keeps it
Expand Down
4 changes: 3 additions & 1 deletion docs/src/main/tut/json.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,9 @@ implicit val cs: ContextShift[IO] = IO.contextShift(global)
implicit val timer: Timer[IO] = IO.timer(global)
import org.http4s.server.blaze._
val server = BlazeServerBuilder[IO].bindHttp(8080).withHttpApp(jsonApp).resource
import scala.concurrent.ExecutionContext.global
val server = BlazeServerBuilder[IO](global).bindHttp(8080).withHttpApp(jsonApp).resource
val fiber = server.use(_ => IO.never).start.unsafeRunSync()
```

Expand Down
9 changes: 6 additions & 3 deletions docs/src/main/tut/service.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ import org.http4s.server.Router
```tut:book
val services = tweetService <+> helloWorldService
val httpApp = Router("/" -> helloWorldService, "/api" -> services).orNotFound
val serverBuilder = BlazeServerBuilder[IO].bindHttp(8080, "localhost").withHttpApp(httpApp)
val serverBuilder = BlazeServerBuilder[IO](global).bindHttp(8080, "localhost").withHttpApp(httpApp)
```

The `bindHttp` call isn't strictly necessary as the server will be set to run
Expand Down Expand Up @@ -180,6 +180,7 @@ import org.http4s.syntax._
import org.http4s.dsl.io._
import org.http4s.implicits._
import org.http4s.server.blaze._
import scala.concurrent.ExecutionContext.global
```

```tut:book
Expand All @@ -191,7 +192,7 @@ object Main extends IOApp {
}.orNotFound
def run(args: List[String]): IO[ExitCode] =
BlazeServerBuilder[IO]
BlazeServerBuilder[IO](global)
.bindHttp(8080, "localhost")
.withHttpApp(helloWorldService)
.serve
Expand All @@ -204,10 +205,12 @@ object Main extends IOApp {
You may also create the server within an `IOApp` using resource:

```tut:book
import scala.concurrent.ExecutionContext.global
object MainWithResource extends IOApp {
def run(args: List[String]): IO[ExitCode] =
BlazeServerBuilder[IO]
BlazeServerBuilder[IO](global)
.bindHttp(8080, "localhost")
.withHttpApp(Main.helloWorldService)
.resource
Expand Down
3 changes: 2 additions & 1 deletion docs/src/main/tut/static.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ import cats.implicits._
import org.http4s.server.blaze.BlazeServerBuilder
import org.http4s.server.staticcontent._
import org.http4s.syntax.kleisli._
import scala.concurrent.ExecutionContext.global
object SimpleHttpServer extends IOApp {
override def run(args: List[String]): IO[ExitCode] =
BlazeServerBuilder[IO]
BlazeServerBuilder[IO](global)
.bindHttp(8080)
.withHttpApp(fileService[IO](FileService.Config(".")).orNotFound)
.serve
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.http4s.HttpApp
import org.http4s.server.Router
import org.http4s.server.blaze.BlazeServerBuilder
import org.http4s.syntax.kleisli._
import scala.concurrent.ExecutionContext.global

object BlazeExample extends IOApp {

Expand All @@ -24,9 +25,8 @@ object BlazeExampleApp {
).orNotFound

def stream[F[_]: ConcurrentEffect: Timer: ContextShift]: Stream[F, ExitCode] =
BlazeServerBuilder[F]
BlazeServerBuilder[F](global)
.bindHttp(8080)
.withHttpApp(httpApp[F])
.serve

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.http4s.metrics.dropwizard._
import org.http4s.server.{HttpMiddleware, Router}
import org.http4s.server.blaze.BlazeServerBuilder
import org.http4s.server.middleware.Metrics
import scala.concurrent.ExecutionContext.global

class BlazeMetricsExample(implicit timer: Timer[IO], ctx: ContextShift[IO])
extends BlazeMetricsExampleApp[IO]
Expand All @@ -28,7 +29,7 @@ class BlazeMetricsExampleApp[F[_]: ConcurrentEffect: ContextShift: Timer] {
).orNotFound

def stream: fs2.Stream[F, ExitCode] =
BlazeServerBuilder[F]
BlazeServerBuilder[F](global)
.bindHttp(8080)
.withHttpApp(app)
.serve
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import cats.implicits._
import com.example.http4s.ssl
import fs2._
import org.http4s.server.blaze.BlazeServerBuilder
import scala.concurrent.ExecutionContext.global

object BlazeSslClasspathExample extends IOApp {

Expand All @@ -19,7 +20,7 @@ object BlazeSslClasspathExampleApp {
for {
context <- Stream.eval(
ssl.loadContextFromClasspath[F](ssl.keystorePassword, ssl.keyManagerPassword))
exitCode <- BlazeServerBuilder[F]
exitCode <- BlazeServerBuilder[F](global)
.bindHttp(8443)
.withSSLContext(context)
.withHttpApp(BlazeExampleApp.httpApp[F])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package blaze
import cats.effect._
import cats.implicits._
import org.http4s.server.blaze.BlazeServerBuilder
import scala.concurrent.ExecutionContext.global

object BlazeSslExample extends IOApp {
override def run(args: List[String]): IO[ExitCode] =
Expand All @@ -13,7 +14,7 @@ object BlazeSslExample extends IOApp {
object BlazeSslExampleApp {

def builder[F[_]: ConcurrentEffect: ContextShift: Timer]: BlazeServerBuilder[F] =
BlazeServerBuilder[F]
BlazeServerBuilder[F](global)
.bindHttp(8443)
.withSSL(ssl.storeInfo, ssl.keyManagerPassword)
.withHttpApp(BlazeExampleApp.httpApp[F])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import cats.effect._
import cats.implicits._
import fs2._
import org.http4s.server.blaze.BlazeServerBuilder
import scala.concurrent.ExecutionContext.global

object BlazeSslExampleWithRedirect extends IOApp {
import BlazeSslExampleWithRedirectApp._
Expand All @@ -21,7 +22,7 @@ object BlazeSslExampleWithRedirect extends IOApp {
object BlazeSslExampleWithRedirectApp {

def redirectStream[F[_]: ConcurrentEffect: Timer]: Stream[F, ExitCode] =
BlazeServerBuilder[F]
BlazeServerBuilder[F](global)
.bindHttp(8080)
.withHttpApp(ssl.redirectApp(8443))
.serve
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.http4s.server.websocket._
import org.http4s.websocket.WebSocketFrame
import org.http4s.websocket.WebSocketFrame._
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.global

object BlazeWebSocketExample extends IOApp {

Expand Down Expand Up @@ -65,7 +66,7 @@ class BlazeWebSocketExampleApp[F[_]](implicit F: ConcurrentEffect[F], timer: Tim
}

def stream: Stream[F, ExitCode] =
BlazeServerBuilder[F]
BlazeServerBuilder[F](global)
.bindHttp(8080)
.withHttpApp(routes.orNotFound)
.serve
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import org.http4s.client.blaze.BlazeClientBuilder
import org.http4s.server.Router
import org.http4s.server.blaze.BlazeServerBuilder
import org.http4s.syntax.kleisli._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.ExecutionContext.global

object Server extends IOApp {

Expand All @@ -31,7 +31,7 @@ object HttpServer {
for {
client <- BlazeClientBuilder[F](global).stream
ctx <- Stream(new Module[F](client))
exitCode <- BlazeServerBuilder[F]
exitCode <- BlazeServerBuilder[F](global)
.bindHttp(8080)
.withHttpApp(httpApp(ctx))
.serve
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.http4s._
import org.http4s.dsl.Http4sDsl
import org.http4s.syntax.kleisli._
import org.http4s.server.blaze.BlazeServerBuilder
import scala.concurrent.ExecutionContext.global

object Main extends IOApp {
def run(args: List[String]): IO[ExitCode] =
Expand All @@ -16,7 +17,7 @@ object Main extends IOApp {
object ExampleApp {

def serverStream[F[_]: ConcurrentEffect: Timer]: Stream[F, ExitCode] =
BlazeServerBuilder[F]
BlazeServerBuilder[F](global)
.bindHttp(port = 8080, host = "0.0.0.0")
.withHttpApp(ExampleRoutes[F]().routes.orNotFound)
.serve
Expand Down

0 comments on commit eaaf50b

Please sign in to comment.