node-haystack Episode 2: Asynchronous and Threading

Asynchronous

To get the full power of machines, async operation seems the only choice. The only problem is which to choose in so many threading models. Simple answer is: libuv, to better and happily work with nodejs.
The official document of libuv is here.
Also you can g-o-o-g-l-e any articles about libuv if you are lucky enough.

Threading

The essential of threading is performing asynchronous operations. Since libuv already provides the work queue for us. So we should use it for good: post our requests into queue, processing request when our turns come and clean the mass we made.

Batching writing or On-spot writing

One important problem is how to performing the file writing. If we have a lot of small-size files and concerning on the writing performance, a better way is combining data of all requests and write to disk once, lately. The pro is obvious: better writing performance or disk IO. The con is obviously too: the request has to wait for the writing to complete, extra information(mostly, the source/sender who made the request and is waiting for result) must be transferred with data so that the processor knows whom to notify when job done. That makes things more complicated. Another consequence of batching writing is the requests are scatted but all complete events are fired at the same time. Further more, writing queue consumes more memory.

On-spot writing, or immediate writing means launching the writing operation immediately once the request arrived. This way works better for large files but worse for small files. Its algorithm is simple and easy to code.

As usual, the Batching writing, as the harder way, must be chosen. To make better batching, we should check the length of waiting queue when pushing request. Once the queue is long enough, we launch the batching writing. To make sure the data will be writ to disk finally if there are no so many requests in queue, a timer is necessary to do the job periodically.

Needed async components

The libuv provides C style interface. It is definitely necessary that have them the C++ cover. There are three components:
- AsyncFile
- AsyncWorker
- AsyncTimer

AsyncFile

Wrapper of libuv file, perform the asynchronous file access.

AsyncWorker

Wrapper of libuv work queue, perform the asynchronous operations.

AsyncTimer

Wrapper of libuv timer, perform periodical operations.

你可能感兴趣的:(C++,JavaScript,Linux,node.js,Web)