Coder Social home page Coder Social logo

cancellation's Introduction

Synchronous cancellation in Node

This simple demo shows how to check for a notification without returning to the event loop.

The code uses named pipes on Windows as written, so would need changes to support other platforms. (See https://nodejs.org/api/net.html#net_server_listen_path_backlog_callback for platform details).

How it works

  • The client spawns the server, passing the name of a named pipe to check for as a cancellation signal.
  • The client sends "work" requests, and waits for "done" or "cancelled" responses.
  • If the client wants to cancel work, it creates a named pipe of the provided name.
  • While the server is doing work, it regularly checks for existance of the above pipe.
  • It is sees it, it aborts the current request.

Overhead

The overhead of each check is minimal. For example, in the code given, if the cancellation check is just hardcoded to return false (i.e. doesn't actually check for the pipe), the iterations finish in around 490ms on my test machine. Actually checking for the pipe repeatedly during a unit or work (2000 times in this example), adds only a few milliseconds total on average (i.e. around 1% overhead).

This example checks for the pipe overly aggressively, which generally results in the creation and detection of the pipe across processes occurring within the same millisecond. For example, a run on my machine produced the below for some iterations (the column on the left being the current millisecond):

818648: server received msg: work
819152: server checks for cancellations ths workload: 2000
819152: client receivied result from server: done in 499ms
821647: client sending work request
821647: server received msg: work
821896: client sending a cancellation
821896: server detected cancellation pipe
821898: client receivied result from server: cancelled

To run the demo

Build the project using the TypeScript compiler in the root (e.g. run "tsc"). With the ./built directory as the current directory, run the client module (e.g. "node editor.js")

cancellation's People

Contributors

billti avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.