ya-rfc

2.1.0 • Public • Published

ya-rfc

npm version license test Coverage Status Language grade: JavaScript

Yet Another Remote Function Call library for Node js.

Ya-rpc anables to run functions on remote machines within a distributed system without worrying about network messaging or load balancing.

It provides an easy way to implement performance-critical applications by parallel execution on machines and processes.

basic execution flow

Key Features

Typical execution flow

basic execution flow

Installation

$ npm install ya-rfc

ya-rfc is tested with versions 12, 14 & 16 on of node.js on ubuntu-latest.

Quick start

const ya = require('ya-rfc');

Broker

Create a rfc broker that accepts TCP, IPC and websocket connections (only one transport required)

const namedPipe = path.join('\\\\.\\pipe', 'some-pipe');

ya.broker([
  ya.transports.ipc(namedPipe),
  ya.transports.tcp(8005, 'localhost'),
  ya.transports.ws(8006, 'localhost')]);

Server

Define functions that remote rfc clients will request to execute

// procedures.js
module.exports = {
  sum: (a, b) => {
    return a + b
  },
  count: (until, progress) => {
    const x = parseInt(until / 10)
    for (let i = 0; i < until; i++) {
      if (i % x === 0) {
        progress(`${i}/${until}`)
      }
    }
    return until
  }
};

Create a (ideally multiple) rfc server that connects to the Broker over TCP, it can also be IPC or websocket

ya.server(ya.transports.tcp(8005, 'localhost'), 'procedures.js');

// ya.server(ya.transports.ws(8006, 'localhost'));

// const win32namedPipe = require('path').join('\\\\.\\pipe', 'some-pipe');
// ya.server(ya.transports.ipc(win32namedPipe));

Client

Create a rfc client that connects to the Broker over TCP, it can also be IPC or websocket

const client = ya.client(ya.transports.tcp(8005, 'localhost'));

// const wsClient = ya.client(ya.transports.ws(8006, 'localhost'))

// const win32namedPipe = require('path').join('\\\\.\\pipe', 'some-pipe')
// const ipcClient = ya.client(ya.transports.ipc(win32namedPipe))

Execute a function remotely

tcpClient.remote.sum(1, 2)
  .then(result => {
    console.log(result)
  })
  .catch(error => {
    console.error(error)
  });
/* output:
3
*/

Execute a function remotely and get notified of its progression

client.remote.count(10000, {
  onProgress: (progress) => {
    console.log('progress', progress)
  }
})
  .then(result => {
    console.log('result is', result)
  })
  .catch(error => {
    console.error(error)
  });
/* output:
progress 0/10000
progress 1000/10000
progress 2000/10000
progress 3000/10000
progress 4000/10000
progress 5000/10000
progress 6000/10000
progress 7000/10000
progress 8000/10000
progress 9000/10000
result is 10000
*/

User defined load balancing

By default, each server will be assigned a maximum of 4*cpu-cores concurrent requests distributed (round robin) on its 2*cpu-cores workers. The broker will always route requests to the less loaded server.

You can create a server with an arbitrary number of workers

ya.server(transport, modulePath, { workers:10 })

You can define the maximum accepted load. For example, if each request has a load of 1, a maximum of 100 concurrent requests can be defined as such:

ya.server(transport, modulePath, { maxLoad:[100] })

Alternatively you can unset (not recommended) the limit by providing -1

ya.server(transport, modulePath, { maxLoad:[-1] })

By default, all requests are assigned a load of 1, this can be overridden like this:

rfc.remote.foo(bar, { load:[10] })

Often, the impact of a request on cpu or memory usage has to be modelled with several values, this is why maxLoad and load options are to be provided as arrays.

Versioning

Yaps-node uses Semantic Versioning for predictable versioning.

License

This library is licensed under MIT.

Package Sidebar

Install

npm i ya-rfc

Weekly Downloads

3

Version

2.1.0

License

MIT

Unpacked Size

67 kB

Total Files

20

Last publish

Collaborators

  • nicocoul