@@ -22,6 +22,7 @@ object RestServlet {
2222 final val DefaultHandleTimeout = 30 .seconds
2323 final val DefaultMaxPayloadSize = 16 * 1024 * 1024L // 16MB
2424 final val CookieHeader = " Cookie"
25+ final val DefaultStreamingBatchSize = 1
2526 private final val BufferSize = 8192
2627
2728 /**
@@ -31,21 +32,29 @@ object RestServlet {
3132 * @param handleTimeout maximum time the servlet will wait for results returned by REST API implementation
3233 * @param maxPayloadSize maximum acceptable incoming payload size, in bytes;
3334 * if exceeded, `413 Payload Too Large` response will be sent back
35+ * @param defaultStreamingBatchSize default batch size for [[StreamedRestResponse ]]
3436 */
3537 @ explicitGenerics def apply [RestApi : RawRest .AsRawRpc : RestMetadata ](
3638 apiImpl : RestApi ,
3739 handleTimeout : FiniteDuration = DefaultHandleTimeout ,
3840 maxPayloadSize : Long = DefaultMaxPayloadSize ,
41+ defaultStreamingBatchSize : Int = DefaultStreamingBatchSize ,
3942 )(implicit
4043 scheduler : Scheduler
4144 ): RestServlet =
42- new RestServlet (RawRest .asHandleRequestWithStreaming[RestApi ](apiImpl), handleTimeout, maxPayloadSize)
45+ new RestServlet (
46+ handleRequest = RawRest .asHandleRequestWithStreaming[RestApi ](apiImpl),
47+ handleTimeout = handleTimeout,
48+ maxPayloadSize = maxPayloadSize,
49+ defaultStreamingBatchSize = defaultStreamingBatchSize,
50+ )
4351}
4452
4553class RestServlet (
4654 handleRequest : RawRest .HandleRequestWithStreaming ,
4755 handleTimeout : FiniteDuration = DefaultHandleTimeout ,
4856 maxPayloadSize : Long = DefaultMaxPayloadSize ,
57+ defaultStreamingBatchSize : Int = DefaultStreamingBatchSize ,
4958)(implicit
5059 scheduler : Scheduler
5160) extends HttpServlet with LazyLogging {
@@ -120,14 +129,16 @@ class RestServlet(
120129 Task .eval(writeNonEmptyBody(response, single.body))
121130 case binary : StreamedBody .RawBinary =>
122131 response.setContentType(binary.contentType)
123- binary.content.bufferTumbling(stream.batchSize).consumeWith(Consumer .foreach { batch =>
124- batch.foreach(e => response.getOutputStream.write(e))
125- response.getOutputStream.flush()
126- })
132+ binary.content
133+ .bufferTumbling(stream.customBatchSize.getOrElse(defaultStreamingBatchSize))
134+ .consumeWith(Consumer .foreach { batch =>
135+ batch.foreach(e => response.getOutputStream.write(e))
136+ response.getOutputStream.flush()
137+ })
127138 case jsonList : StreamedBody .JsonList =>
128139 response.setContentType(jsonList.contentType)
129140 jsonList.elements
130- .bufferTumbling(stream.batchSize )
141+ .bufferTumbling(stream.customBatchSize.getOrElse(defaultStreamingBatchSize) )
131142 .switchIfEmpty(Observable (Seq .empty))
132143 .zipWithIndex
133144 .consumeWith(Consumer .foreach { case (batch, idx) =>
0 commit comments