简体   繁体   English

需要用例示例在ChicagoBoss中进行流响应

[英]Need an use case example for stream response in ChicagoBoss

ChicageBoss controller API has this ChicageBoss控制器API拥有此

{stream, Generator::function(), Acc0} {stream,Generator :: function(),Acc0}

Stream a response to the client using HTTP chunked encoding. 使用HTTP分块编码将响应流式传输到客户端。 For each chunk, the Generator function is passed an accumulator (initally Acc0) and should return either {output, Data, Acc1} or done. 对于每个块,Generator函数将传递一个累加器(最初为Acc0),并且应返回{output,Data,Acc1}或done。

I am wondering what is the use case for this? 我想知道这是什么用例? There are others like Json, output. 还有其他类似Json的输出。 When will this stream be useful? 当将这种stream是有用的?

Can someone present an use case in real world? 有人可以在现实世界中提出用例吗?

Serving large files for download might be the most straight-forward use case. 提供大文件供下载可能是最直接的用例。

You could argue that there are also other ways to serve files so that users can download them, but these might have other disadvantages: 您可能会争辩说,还有其他提供文件的方式,以便用户可以下载文件,但是这些方式可能还有其他缺点:

  • By streaming the file, you don't have to read the entire file into memory before starting to send the response to the client. 通过流式传输文件,您无需在开始将响应发送到客户端之前将整个文件读入内存。 For small files, you could just read the content of the file, and return it as {output, BinaryContent, CustomHeader} . 对于小文件,您可以读取文件的内容,然后将其返回为{output, BinaryContent, CustomHeader} But that might become tricky if you want to serve large files like disk images. 但是,如果要提供磁盘映像之类的大文件,这可能会变得棘手。
  • People often suggest to serve downloadable files as static files (eg here ). 人们常常认为服务下载的文件为静态文件(例如这里 )。 However, these downloads bypass all controllers, which might be an issue if you want things like download counters or access restrictions. 但是,这些下载会绕过所有控制器,如果您想要下载计数器或访问限制之类的东西,则可能会遇到问题。 Caching might be an issue, too. 缓存也可能是一个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM