Timeout while using StreamConverters.asInputStream

401 views
Skip to first unread message

Konstantin Shaposhnikov

unread,
Jan 7, 2016, 11:47:45 PM1/7/16
to Akka User List
Hi,

In the application that I am working on I need to pass content of the uploaded via HTTP files to Java library that accepts InputStreams. I am trying to use new StreamConvertes.asInputStream method from akka-http 2.0.1 (akka 2.3.11) However the server always responds with timeouts. I wonder if I am using this functionality correctly.

Here is very simple application that shows the problem (https://gist.github.com/kostya-sh/70f4b6561ee908604050#file-mysandboxakkahttpserver-scala):

import java.nio.file.Files
import scala.concurrent.duration._
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.Multipart
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.RouteResult.route2HandlerFlow
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.StreamConverters
import java.nio.file.CopyOption
import java.nio.file.StandardCopyOption

object MySandboxAkkaHttpServer extends App {
implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
implicit val executor = system.dispatcher

val route = encodeResponse {
(post & path("test") & entity(as[Multipart.FormData])) { entity =>
complete {
val sources = entity.parts.map(e => e.entity.dataBytes)
val f = sources.runForeach { source =>
val tmp = Files.createTempFile("a", "b")
println(s"writing to $tmp")
val inputStream = source.runWith(StreamConverters.asInputStream(5.second))
Files.copy(inputStream, tmp, StandardCopyOption.REPLACE_EXISTING)
Files.delete(tmp)
}
f.map(_ => "ok")
}
}
}

Http().bindAndHandle(route, "0.0.0.0", 7777)
}

When testing it with curl (curl -F"a=@a.dat" -F"b=@b.dat" http://localhost:7777/test) I always get timeout (full stack trace at https://gist.github.com/kostya-sh/70f4b6561ee908604050#file-stack-trace)

writing to C:\Users\user\AppData\Local\Temp\a8494608437979025074b
2016-01-08 12:15:44,462 - [ERROR] - [akka.actor.ActorSystemImpl] [undefined] [default-akka.actor.default-dispatcher-8] - Error during processing of request HttpRequest(HttpMethod(POST),http://localhost:7777/test,List(User-Agent: curl/7.30.0, Host: localhost:7777, Accept: */*, Expect: 100-continue),HttpEntity.Default(multipart/form-data; boundary=----------------------------0e5cabf20511; charset=UTF-8,22500,akka.stream.scaladsl.Source@ece5b80),HttpProtocol(HTTP/1.1))
java.io.IOException: Timeout on waiting for new data
at akka.stream.impl.io.InputStreamAdapter$$anonfun$read$1.apply$mcI$sp(InputStreamSinkStage.scala:139) ~[akka-stream-experimental_2

Any help will be appreciated.

Thank you,
Konstantin

Konstantin Shaposhnikov

unread,
Jan 7, 2016, 11:52:32 PM1/7/16
to Akka User List
I've just realized that I didn't close input stream after Files.copy.

After fixing this timeouts still happen but not every time.

Konstantin Shaposhnikov

unread,
Jan 11, 2016, 2:10:07 AM1/11/16
to Akka User List
Reply all
Reply to author
Forward
0 new messages