加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

playframework – Play Framework [2.2-scala]:从缓慢的InputSt

发布时间:2020-12-16 18:30:27 所属栏目:安全 来源:网络整理
导读:我正在实施AWS S3文件传递API.我被迫将S3的S3ObjectInputStream中的字节流式传输到浏览器. 我们有一个用例用云端服务文件不是一个选项(主要是本地开发) 我有一个InputStream,所以最明显的事情是使用带有Enumerator.fromStream()的Ok.chunked,但Enumerator.fr
我正在实施AWS S3文件传递API.我被迫将S3的S3ObjectInputStream中的字节流式传输到浏览器.
我们有一个用例用云端服务文件不是一个选项(主要是本地开发)

我有一个InputStream,所以最明显的事情是使用带有Enumerator.fromStream()的Ok.chunked,但Enumerator.fromStream()有一个非常明确的警告,即流不应该很慢.我假设AWS S3ObjectInputStream可能是最慢的流之一.

http://www.playframework.com/documentation/2.2.x/api/scala/index.html#play.api.libs.iteratee.Enumerator$

def fromStream(input: InputStream,chunkSize: Int = 1024 * 8)
              (implicit ec: ExecutionContext): Enumerator[Array[Byte]]

Create an enumerator from the given input stream.

This enumerator will block on reading the input stream,in the default iteratee
thread pool. Care must therefore be taken to ensure that this isn't a 
slow stream. If using this with slow input streams,consider setting the value
of iteratee-threadpool-size to a value appropriate for handling the blocking.

所以我想知道最安全的方法是避免线程饥饿并将文件流式传输到浏览器而不将整个文件保存在内存中.

是否有其他方法可以从InputStream获取枚举器(或我们可以在结果中发送的内容)?

解决方法

我实际上在某种程度上误读了文档. Enumerator.fromStream具有您可以提供的隐式ExecutionContext.
如果为此特定类型的操作创建专用上下文,您仍然可以遇到线程饥饿,但您可以控制哪个线程池获得该问题.

我们正在玩游戏!所以我们可以在application.conf中配置akka threadpools:

# this is a root value in the application.conf,but you can put it anywhere
# as long as you provide the full path to the .lookup() function
my-contexts {
  s3-streaming {
    fork-join-executor {
      parallelism-min = 50
      parallelism-max = 50
    }
  }
}

并在代码中使用它们,如下所示:

object MyContexts {
  val s3Streaming: ExecutionContext = 
    Akka.system.dispatchers.lookup("my-contexts.s3-streaming")
}

...

Enumerator.fromStream(stream)(MyContexts.s3Streaming)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读