akka http with actors : error 503 timeout

3.3k Views Asked by At

I m trying to implement AKKA http and some actors on Scala. I created a web app with akka. But i have this error on one or 2 differents routes /16. (it s apparently random ) :

The server was not able to produce a timely response to your request. Please try again in a short while!

Could you explain to me why and how to fix it? I m really novice with Akka.

The Main class :

object WebServer extends App {

    implicit val system = ActorSystem("app-1")
    implicit val materializer = ActorMaterializer()
    // needed for the future flatMap/onComplete in the end
    implicit val executionContext = system.dispatcher
    val routes = SessionRoute.route
     val bindingFuture = Http().bindAndHandle(routes, ipServer, configApplication.getInt("spray.can.client.proxy.http.port"))
    println("serv http launch")
    StdIn.readLine
    bindingFuture
        .flatMap(_.unbind()) // trigger unbinding from the port
        .onComplete(_ => {
        cluster.close()
        system.terminate()
    })
    bindingFuture.onFailure {
        case ex: Exception =>
            println(ex, "Failed to bind to {}:{}!", ipServer, configApplication.getInt("spray.can.client.proxy.http.port"))
    }
    }

My route is :

object SessionRoute extends TokenValidator {
implicit val formats = org.json4s.DefaultFormats

val sessionHandler = WebServer.system.actorOf(SessionHandler.props(), "SessionHandler")
implicit val timeout = Timeout(60.seconds)


val route: Route = get {
    authenticated(doAuthPublisher) { app =>
         getActiveUserPublisher 
    }
}
def getActiveUserPublisher =
    path("session" / JavaUUID / "active_user") { publisher_id =>
        parameters('date_start.as[Long], 'date_end.as[Long]) {
            (date_start, date_end) => {
                onSuccess(sessionHandler ? SessionActiveUser(SessionRequest(publisher_id, date_start, date_end, null))) {
                    case response: Answer =>
                        complete(StatusCodes.OK, response.result)
                    case _ =>
                        complete(StatusCodes.InternalServerError, "Error on the page")
                }
            }
        }

    }
}

My Actor is :

    object SessionHandler {
        def props(): Props = {
            Props(classOf[SessionHandler])
        }
    }
    class SessionService(implicit actorSystem: ActorSystem) extends toolService {
     def activeUser(sessionRequest: SessionRequest): Map[String, Any] = {
    ....
      }
    }

class SessionHandler extends Actor with ActorLogging {
    implicit val system = ActorSystem("session")
    implicit val formats = org.json4s.DefaultFormats

    def receive: Receive = {
     case request: SessionActiveUser =>
     sender() ! Answer(Serialization.write(new SessionService().activeUser(request.sessionRequest)))
    }}

And my case class used :

final case class Answer(result: String)
case class SessionActiveUser(sessionRequest: SessionRequest)
case class SessionRequest(publisher_id: UUID = null, date_start: Long, date_end: Long, app_id: String = null)

My configuration.conf :

    akka {
  loglevel = INFO
  stdout-loglevel = INFO
  loggers = ["akka.event.slf4j.Slf4jLogger"]
  default-dispatcher {
    fork-join-executor {
      parallelism-min = 8
    }
  }
 // event-handlers = ["akka.event.slf4j.Slf4jLogger"]
}
1

There are 1 best solutions below

0
On

The error you see is caused by your route not being able to produce a response within the configured request-timeout. If you haven't explicitly set it, it defaults to 20 seconds. See here for more info on the request timeout.

About the cause of this happening, could you detail what happens in the activeUser function? Any significant blocking happening there? If so, all your incoming requests will be sequentialized and blocked against activeUser, eventually causing the request timeouts to kill your requests.

Possible solutions are:

  • make your service async/non-blocking
  • follow the docs on how to deal with blocking calls inside Akka-HTTP routes