From 03f3a849c4cae5cd25e4b7b12bba60db550b691d Mon Sep 17 00:00:00 2001 From: Jordi Moraleda Date: Mon, 28 Nov 2016 21:17:46 +0100 Subject: [PATCH] Polishing the docker example --- Dockerfile | 12 ++++++------ README.md | 10 ++++++++++ balancing.client.js | 10 +++++----- config-default.js | 10 ++-------- example.broker.js | 2 +- example.client.js | 2 +- example.worker.js | 2 +- package.json | 4 ++-- 8 files changed, 28 insertions(+), 24 deletions(-) diff --git a/Dockerfile b/Dockerfile index 840bc68..e0e01d7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,16 +5,16 @@ MAINTAINER Jordi Moraleda RUN mkdir -p /usr/src/app WORKDIR /usr/src/app -ADD package.json /usr/src/app/package.json - # Native dependencies RUN apk --update add --no-cache make git g++ python zeromq-dev \ && npm install -g nodemon \ + && git clone https://github.com/TvrboPro/TinyZmq.git . \ && npm install --production \ && apk del --purge g++ python -ADD . /usr/src/app -CMD nodemon -L -d 2 example.broker.js -#CMD nodemon -L -d 2 example.worker.js -#CMD nodemon -L -d 2 example.client.js +# uncomment the appropriate command below + +CMD nodemon -L -d 1 example.broker.js +#CMD nodemon -L -d 1 example.worker.js +#CMD nodemon -L -d 1 example.client.js diff --git a/README.md b/README.md index 1c1c56a..da06829 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,16 @@ To register a client, add this code to the component that places requests: console.log("THE CLIENT GOT BACK:", response); }); +## Parameterization + +The internal behavior can be tuned by using environment variables at run time. + +* ```TINY_ZMQ_DEBUG```: By default, disabled when ```NODE_ENV='production'``` and ```true``` otherwise. When set to true, provides extra logging information. +* ```TINY_ZMQ_PING_BASE_INTERVAL```: The client will periodically ping the worker to assert that it is still alive. By default, every ```500``` milliseconds. +* ```TINY_ZMQ_INACTIVITY_TIMEOUT```: After no pingback response from either the client or the worker, the connection will be retried again. By default, the timeout is ```5000``` milliseconds. +* ```TINY_ZMQ_CLIENT_INSTANCES```: The number of client instances running on the environment. This helps to better adjust the actual rate of pings between nodes. +* ```TINY_ZMQ_WORKER_INSTANCES```: The number of worker instances running on the environment. + ## Utilities To get an example of a container running the broker, worker or client on a Linux + NodeJS + ZMQ environment, refer to the ```Dockerfile```. diff --git a/balancing.client.js b/balancing.client.js index 095d499..7ee050f 100644 --- a/balancing.client.js +++ b/balancing.client.js @@ -3,7 +3,7 @@ const uuid = require('uuid'); const config = require('./config.js'); const procSignals = ['SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGILL', 'SIGTRAP', 'SIGABRT', 'SIGBUS', 'SIGFPE', 'SIGUSR1', 'SIGSEGV', 'SIGTERM'/*, 'SIGUSR2'*/]; -const PING_INTERVAL = Math.floor(config.PING_BASE_INTERVAL * config.MATCHER_INSTANCES / config.API_INSTANCES) - 10; +const PING_INTERVAL = Math.floor(config.PING_BASE_INTERVAL * config.WORKER_INSTANCES / config.CLIENT_INSTANCES) - 10; // STATE @@ -159,7 +159,7 @@ function connect(uri){ } // READY - requester.connect(config.MATCHER_CLIENT_URI); + requester.connect(brokerURI); // return the method to call us to the client return sendRequest; @@ -199,7 +199,7 @@ function reconnect(){ } // READY - requester.connect(config.MATCHER_CLIENT_URI); + requester.connect(brokerURI); }, 50); } @@ -209,8 +209,8 @@ function onTerminate(code){ terminating = true; if(requester) { try { - // requester.disconnect(config.MATCHER_CLIENT_URI); - requester.close(config.MATCHER_CLIENT_URI); + // requester.disconnect(brokerURI); + requester.close(brokerURI); } catch(e){ ; } } diff --git a/config-default.js b/config-default.js index 7e89a2c..fe7ca30 100644 --- a/config-default.js +++ b/config-default.js @@ -3,17 +3,11 @@ module.exports = { DEBUG: true, - COMPONENT_NAME: 'TWINS LIB', PING_BASE_INTERVAL: 500, INACTIVITY_TIMEOUT: 5000, - MATCHER_CLIENT_PORT: 5559, - MATCHER_WORKER_PORT: 5560, - MATCHER_CLIENT_URI: 'tcp://localhost:5559', - MATCHER_WORKER_URI: 'tcp://localhost:5560', - // Cluster aware - API_INSTANCES: 2, - MATCHER_INSTANCES: 4 + CLIENT_INSTANCES: 2, + WORKER_INSTANCES: 4 }; diff --git a/example.broker.js b/example.broker.js index c515b5f..0e8964c 100644 --- a/example.broker.js +++ b/example.broker.js @@ -1,4 +1,4 @@ -const broker = require('tiny-zmq').broker; +const broker = require('.').broker; broker.bind({ clientsPort: 5559, diff --git a/example.client.js b/example.client.js index 26accda..7135c26 100644 --- a/example.client.js +++ b/example.client.js @@ -1,4 +1,4 @@ -const client = require('tiny-zmq').client; +const client = require('.').client; const sendRequest = client.connect('tcp://localhost:5559'); diff --git a/example.worker.js b/example.worker.js index 4abfb54..8f2e6c3 100644 --- a/example.worker.js +++ b/example.worker.js @@ -1,4 +1,4 @@ -const worker = require('tiny-zmq').worker; +const worker = require('.').worker; worker.connect('tcp://localhost:5560', function(parameters, doneCallback){ console.log("THE WORKER GOT", parameters); diff --git a/package.json b/package.json index 471e049..cf26910 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,9 @@ { "name": "tiny-zmq", - "version": "1.0.1", + "version": "1.0.2", "description": "A NodeJS package that provides simple load balanced messaging on distributed environments", "main": "index.js", - "keywords": ["zmq", "messaging", "tvrbo", "ventilator", "clustering", "cluster", "load", "balancing", "broker", "worker", "client"], + "keywords": [ "zmq", "messaging", "tvrbo", "ventilator", "clustering", "cluster", "load", "balancing", "broker", "worker", "client" ], "author": "Jordi Moraleda ", "repository": { "type": "git",