akka/akka-http

Merge stream elements into one http chunk

Offen

#1.792 geöffnet am 17.01.2018

 (4 Kommentare) (0 Reaktionen) (0 zugewiesene Personen)Scala (598 Forks)batch import
1 - triageddiscusshelp wantedt:http:marshallingt:routingt:stream

Repository-Metriken

Stars
 (1.311 Sterne)
PR-Merge-Metriken
 (Durchschn. Merge 1T 10h) (2 gemergte PRs in 30 T)

Beschreibung

Streams are great but I got a stream of thousand elements. Unfortunately every element is emitted in one http chunk (and the elements are small). Would be nice if the framework supports "merging" of multiple elements into one chunk and return a valid json array of elements.

My workaround

                      def toJsonBytes(source: Seq[T]): ByteString =
                        ByteString(source.map(t => t.asJson.noSpaces).mkString(","))
                      // emit 100 elements in one http chunk
                      val chunk = t.groupedWithin(100, 100.millis)
                      val fst   = Flow[Seq[T]].map(toJsonBytes)
                      val rst   = Flow[Seq[T]].map(ByteString(",") ++ toJsonBytes(_))
                      val together = chunk.prefixAndTail(1).flatMapConcat {
                        case (head, tail) =>
                          Source(head).via(fst).concat(tail.via(rst))
                      }
                      complete(
                        HttpEntity(
                          ContentTypes.`application/json`,
                          Source.single(ByteString("[")) ++ together ++ Source.single(ByteString("]"))
                        )
                      )

Contributor Guide