开发者

How does main stream web server implement this feature?

开发者 https://www.devze.com 2023-03-13 03:45 出处:网络
This means, for example, a module can start compressing the response from a backend server and stream it to the

This means, for example, a module can start compressing the response from a backend server and stream it to the client before the module has received the entire response from开发者_JAVA技巧 the backend. Nice!

I know it's some kind of asynchronous IO but simple like that isn't enough.

Anyone knows?


Without looking at the source code of an actual implementation, I'm speculating here:

It's most likely some kind of stream (abstract buffered IO) that is passed from one module to the other ("chaining"). One module (maybe a servlet container) writes to a stream that is read by another module (the compression module in your example), which then writes its output to another stream. The contents of that stream may then be processed further or transmitted to the client.


The backend may need to wait on IO before it can fully produce the page. Modules can begin compressing the start of the message before the backend is entirely done writing it.

To understand why this is useful, you need to understand how ngnix is structured. ngninx is a server that relies on non-blocking input and output. Normally, a server will use blocking input and output: it will listen on a connection, and when a connection is found, it will process the page. In order to increase throughput, multiple threads are spawned, called 'workers'.

Contrast this to ngnix: It continually asks the kernel, "Are any of my IO requests ready?" This allows it to handle the same amount of pages with 1) less overhead from all the different processes, and 2) lower memory usage. It has some downsides, however. For extremely low-volume applications, ngnix may use more CPU than a blocking server. Second, it's much less portable. Windows uses an entirely different model for non-blocking IO.

Getting back to your original question, compressing the beginning of a page is useful because it can be ready for the rest of the page when it's done accessing a database or reading from a disk or what-have-you.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号