4

I'm trying to respond back with a dummy response if http4s's blaze client cannot connect to a service, but I'm unable to figure out how to handle pooling errors.

Setting up a connection pool

import java.util.concurrent._

private val uncaughtExceptionHandler = new Thread.UncaughtExceptionHandler {
   // This should have handled it ideally? But this log never gets printed
   override def uncaughtException(t: Thread, e: Throwable) = {
      logger.error("Uncaught exception")
    }
  }

val executor: ExecutorService = new ForkJoinPool(
    Runtime.getRuntime().availableProcessors(),
    ForkJoinPool.defaultForkJoinWorkerThreadFactory,
    uncaughtExceptionHandler,
    true
)

and using this pool executor to call REST APIs –

Fetching from a URI

  val client = PooledHttp1Client(
        maxTotalConnections = 20,
        config = BlazeClientConfig.defaultConfig.copy(customExecutor = Some(Context.executor)) // same as the executor defined above
      )

    // Make a call
    client.fetch(Request(uri = uri)) { res =>
      {
        if (res.status.code === Status.Ok.code)
          // do something
        else
          // do something else
      }

However, if the uri specified in client.fetch is a non-reachable endpoint that I'm not able to catch.

Error

ERROR org.http4s.client.PoolManager Error establishing client connection for key RequestKey(http,localhost:3000)
java.net.ConnectException: Connection refused

As a result, I cannot respond back with a fallback where client.fetch is called because the connection pool itself throws an error. How do I handle these?

Shubham Kanodia
  • 6,036
  • 3
  • 32
  • 46

0 Answers0