scala - How to handle response timeout? -
in akka-http routing can return future
response implicitly converts toresponsemarshaller
.
is there way handle timeout of future? or timeout of connection in route level? or 1 way use await()
function?
right client can wait response forever.
complete { val future = { response <- someiofunc() entity <- someotherfunc() } yield entity future.oncomplete({ case success(result) => httpresponse(entity = httpentity(mediatypes.`text/xml`, result)) case failure(result) => httpresponse(entity = utils.getfault("fault")) }) future }
adding timeout asynchronous operation means creating new future completed either operation or timeout:
import akka.pattern.after val future = ... val futurewithtimeout = future.firstcompletedof( future :: after(1.second, system.scheduler)(future.failed(new timeoutexception)) :: nil )
the second future hold successful result replaces error, depending on want model.
as side note: presented code sample contains dead code, registering oncomplete handler on future makes sense side-effects seem want transform future’s value , create httpentity it. should done using map
, recover
:
future .map(result => httpresponse(entity = httpentity(mediatypes.`text/xml`, result))) .recover { case ex => httpresponse(entity = utils.getfault("fault")) }
this overall return value passed complete
directive.
Comments
Post a Comment