@polka/cluster

1.0.0-next.0 • Public • Published

@polka/cluster npm

Intialize and run a HTTP cluster; not limited to Polka!

Accepts a Polka app, a programmatic Polkadot app, or any HTTP handler and runs it as a Node.js cluster.

For more information on Node.js clustering, please read How it Works 🙇

Install

$ npm install --save @polka/cluster

Usage

const polka = require('polka');
const cluster = require('@polka/cluster');
const cors = require('cors')({ origin:true });

const { PORT=3000 } = process.env;

const app = (
  polka()
    .use(cors)
    .use('/items', require('./items'))
    .get('/alive', (req, res) => {
      res.end('OK');
    })
);

cluster(app, 4).listen(PORT);

API

cluster(handler, size)

Returns: http.Server or Object

If handler was an application, then it is returned. Otherwise a new http.Server instance is returned.

handler

Type: Function or Object

The HTTP handler function to run, which will be attached to the http.Server as its requestListener.

Similarly, handler may be an Object so long as it exposes a listen method.
This means that you may pass in entire Polka, Polkadot, or Express applications!

size

Type: Number
Default: os.cpus().length

The size of the cluster. Put differently, your application will run on <size> threads.

By default, your cluster will spawn one Worker per thread. For example, "most" modern Intel-based CPUs have two threads per CPU core, meaning that a quad-core CPU will have 8 threads... making 8 the default cluster size for this example.

Important: Your size value will not exceed the value determined by require('os').cpus().length

Support

Any issues or questions can be sent to the Polka repository.
However, please specify that your inquiry is about @polka/cluster specifically.

License

MIT © Luke Edwards

Readme

Keywords

none

Package Sidebar

Install

npm i @polka/cluster

Weekly Downloads

35

Version

1.0.0-next.0

License

MIT

Unpacked Size

4.29 kB

Total Files

4

Last publish

Collaborators

  • lukeed