0
votes

I am new to scala. I am trying to timeout api request. I am using spray to make API request. I have spray client to get response from some other server. In my application.conf i've specified timeout of request in spray.can something like:

spray.can {
  server {
    idle-timeout = ${idle-timeout}
    request-timeout = ${request-timeout}
    request-chunk-aggregation-limit = 20m
    parsing {
      max-content-length = 21m
    }
  }
  client {
    idle-timeout = ${idle-timeout}
    request-timeout = ${request-timeout}
    response-chunk-aggregation-limit = 20m
  }
}

Now, I want to override this request-timeout in one my api. I have written api something like:

def completeService(jwttoken: String, completeRequest: String): Future[HttpResponse] = {
    val pipeline: HttpRequest => Future[HttpResponse] = sendReceive ~> unmarshal[HttpResponse]
        val response: Future[HttpResponse] = pipeline(Post(some-remote-url, completeRequest)
          ~> addHeader("FROM", jwttoken))
    response
}

So, how can I put request-timeout here in this method? By overriding application.conf

I tried

 implicit val timeoutVal: Timeout = Timeout(scala.concurrent.duration.Duration(100, MILLISECONDS).asInstanceOf[FiniteDuration])

and I got this:

  test 2020-03-06 08:48:12.147 GMT [WARN] a.k.i.AskPatternInstrumentation - Timeout triggered for ask pattern to actor [IO-HTTP] at [pipelining.scala:38]
  test 2020-03-06 08:48:12.573 GMT [INFO] akka.actor.DeadLetterActorRef - Message [spray.http.HttpResponse] from Actor[akka://test-app/user/IO-HTTP/host-connector-1/1#-1105043312] to Actor[akka://test-app/deadLetters] was not deliver
1

1 Answers

0
votes

I think you can add an implicit parameter requestTimeout. The data type of that parameter is akka.util.Timeout. You can checkout this link for more details or this link

val _pipeline: Future[SendReceive] =
for (
  Http.HostConnectorInfo(connector, _) <-
  IO(Http) ? Http.HostConnectorSetup("www.spray.io", port = 80, settings = Some(new HostConnectorSettings(maxConnections = 3, maxRetries = 3, maxRedirects = 0, pipelining = false, idleTimeout = 5 seconds, connectionSettings = ClientConnectionSettings(...))))
) yield sendReceive(connector)

Let me know if it helps!!