1.0.2 • Published 6 years ago

xrtlibrary-coroutine v1.0.2

Weekly downloads
1
License
BSD-3-Clause
Repository
github
Last release
6 years ago

XRTLibrary-Coroutine

Introduction

In the development of network-security related products, we found that the Node.JS was always blocked during processing large data and caused many problems. In order to solve this problem, we decided to use the co-routine mechanism.

This library is quite different from traditional async/await model in JavaScript. It is more like a task scheduler. We call each task a "co-routine", the scheduler assigns time slice to each co-routine, if one's time slice was totally used but its task wasn't finished, it must concede the CPU resource and let the scheduler re-queue it (just like the process scheduler in operating system kernel).

Installation

To install this package, type following command in your terminal:

npm install xrtlibrary-coroutine --save

And then, you can import this package in your NodeJS environment with following "require" statement.

var XRTLibCoroutine = require("xrtlibrary-coroutine");

Typically, you also need our xrtlibrary-timestamp library, please install it in your project.

Usage

First, you have to create a scheduler, use following code:

//  Create scheduler.
var scheduler = new XRTLibCoroutine.CoroutineScheduler();

Then you can create tasks (co-routines), the code template is:

//  Create a task (co-routine).
var task = XRTLibCoroutine.MakeCoroutineContext(function() {
    //
    //  ... The context of the co-routine ...
    //

    //  You have to return a function as the co-routine.
    return function(expireTime) {
        //  The scheduler will call this function when it assigns time slice to this co-routine. And you must finish your job before expire time or concede the CPU resource.
        while(XRTLibTimestamp.GetAccurateTimestamp() < expireTime) {
            //
            //  ... Processing ...
            //
            if (/*  The job has finished.  */) {
                this.finish(/*  The return value.  */);
                break;
            }
        }
        //  After returned, if the co-routine was not finished, the scheduler will re-queue the co-routine.
    };
});

After created the co-routine, you can insert the co-routine to the scheduler, the scheduler will handle it automatically. With following code:

//  Process the task.
scheduler.process(task).addCallback(function(returnValue) {
    //
    //  ... Things to do after the job finished ...
    //
});

Finally, you can close the scheduler and wait for all tasks. With following code:

//  Close the scheduler (and wait for tasks to be finished).
scheduler.close().addCallback(function() {
    console.log("All tasks has been finished.");
});

API

Functions:

MethodDescription
MakeCoroutineContext(fn)Create a co-routine context.
Utilities.Buffer.GetBlockSize()Get the block size.
Utilities.Buffer.SetBlockSize(size)Set the block size.
Utilities.Buffer.Copy(dst, src, scheduler)Copy a buffer from src to dst with co-routine.
Utilities.Buffer.IsEqual(buffer1, buffer2, scheduler)Compare two buffers with co-routine.
Utilities.Buffer.Iterate(buffer, scheduler, callback(block, cursor_start, cursor_end))Iterate a buffer with co-routine. (Note: The callback must return true to keep iterating.)
Utilities.Buffer.Concatenate(bufferList, scheduler)Concatenate several buffers.

Classes:

MethodDescription
new CoroutineScheduler()Create a co-routine scheduler.
CoroutineScheduler.process(ctx)Process a co-routine context.
CoroutineScheduler.getInterval()Get the schedule interval (time slice).
CoroutineScheduler.setInterval(interval)Set the schedule interval (time slice).
CoroutineScheduler.getParallelCount()Get the max parallel co-routine count.
CoroutineScheduler.setParallelCount(count)Set the max parallel co-routine count.
CoroutineScheduler.getCurrentCount()Get the co-routine count.
CoroutineScheduler.isClosed()Get whether the scheduler has been closed.
CoroutineScheduler.close()Close the scheduler.