Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'node-zookeeper-client' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
function createClient(connectionString, options) {
const zkClient = zookeeper.createClient(connectionString, options);
zkClient.once('connected', () => {
// for some reason zkClient.exists() does not return
// NO_NODE when base path does not exist, hence use
// getData() instead
zkClient.getData('/', err => {
if (err && err.name !== 'NO_NODE') {
return zkClient.emit('error', err);
}
// NO_NODE error and autoCreateNamespace is enabled
if (err && options && options.autoCreateNamespace) {
const nsIndex = connectionString.indexOf('/');
const hostPort = connectionString.slice(0, nsIndex);
const namespace = connectionString.slice(nsIndex);
const rootZkClient = zookeeper.createClient(hostPort, options);
rootZkClient.connect();
return rootZkClient.mkdirp(namespace, err => {
this._zk.create(path, new Buffer(data), mode, (error, path) => {
if (error) {
if (error.getCode() === zookeeper.Exception.NODE_EXISTS ||
error.getCode() === zookeeper.Exception.OK) {
this.emit(utils.EVENT.DEBUG, 'Create or Exist path: ' + (path || this._root));
} else {
// other error
return reject(error);
}
}
return resolve(true);
});
});
this._zk.create(path, new Buffer(data), mode, (error, path) => {
if (error) {
if (error.getCode() === zookeeper.Exception.NODE_EXISTS ||
error.getCode() === zookeeper.Exception.OK) {
this.emit(utils.EVENT.DEBUG, 'Create or Exist path: ' + (path || this._root));
} else {
// other error
return reject(error);
}
}
return resolve(true);
});
});
function (error, children, stat) {
if (error) {
console.warn('Failed to list children due to: %s.', error);
if (error.getCode() == zookeeper.Exception.CONNECTION_LOSS) { // TODO 나중에 좀더 확인이 필요함!
self.zkClient.close();
self._connect(self.isWatching, function (err) {
if (err) console.error(err);
})
}
} else {
var max = children.length;
var nodeTask = function (taskId, value, callback) {
self._getServerNode(children[taskId], function () {
taskId++
if (taskId < max) {
function_array.splice(function_array.length - 1, 0, nodeTask);
}, (next) => { // fn
count++;
logger.debug('%s server is try to connect to zookeeper',
mon.app.serverId);
mon.client.close();
mon.client = zookeeper.createClient(mon.servers, {
sessionTimeout: mon.timeout,
retries: 0,
spinDelay: mon.spinDelay
});
mon.cbTimer = setTimeout(() => {
logger.info('reconnect to zookeeper timeout');
mon.state = ZK_CONNECT_FAIL;
utils.invokeCallback(next);
}, constants.TIME.DEFAULT_ZK_CONNECT_TIMEOUT);
mon.client.once('connected', () => {
logger.info('%s connect zookeeper successfully.', mon.app.serverId);
mon.state = ZK_CONNECTED;
mon.client.addAuthInfo('digest', new Buffer(mon.authentication));
this.connectingString = connectingString;
this.options = options;
this.root = root || "/illyria";
this.prefix = prefix || "/t";
this.path = null;
this.root = this.root.trim();
this.prefix = this.prefix.trim();
if(!this.root.length) this.root = "/illyria";
if(this.root[0] !== "/") this.root = "/" + this.root;
if(!this.prefix.length) this.prefix = "/t";
if(this.prefix[0] !== "/") this.prefix = "/" + this.prefix;
// zookeeper core object
this.client = zookeeper.createClient(this.connectingString, options);
this.server = {
host: "127.0.0.1",
port: 3721,
clients: 0
};
this.clientStatus = ZK_STATUS.NOT_CONNECTED;
};
.then((found)=>{
if(found) {
return Promise.resolve();
}
else {
return zkUtil.createP(zk, zkUtil.NODE_CLIENT, null, ZooKeeper.CreateMode.PERSISTENT, 'Failed to create clients node due to');
}
})
.then(()=>{ // create client node
private async _createRootConsumer(consumer: string) {
let {res, err} = await go(this._exists(consumer));
//check error
if (err) {
return err;
}
// current consumer root path was existed.
if (res) {
return null;
}
//create current consumer path
({err} = await go(this._create(consumer, zookeeper.CreateMode.PERSISTENT)));
if (err) {
return err;
}
log('create root comsumer %s successfull', consumer);
}
* manage zookeeper client
*
* @author zzxun
*/
'use strict';
/**
* module dependencies
*/
const _ = require('lodash');
const zookeeper = require('node-zookeeper-client');
const Promises = require('bluebird');
const EventEmitter = require('events').EventEmitter;
const utils = require('./util');
const CreateMode = zookeeper.CreateMode;
class ZookeeperService extends EventEmitter {
constructor(options) {
super();
let self = this;
self._root = options.root;
// initial
options = _.merge({
connect : '127.0.0.1:2181',
sessionTimeout: 200000
}, options);
self._zk = zookeeper.createClient(options.connect, options);
this.client.on("state", function(state) {
// automatically reconnect
if(self.clientStatus !== ZK_STATUS.NOT_CONNECTED &&
state === zookeeper.State.EXPIRED) {
setTimeout(self.connect, 1, callback, -1);
}
});