UNPKG

tm-apps-list-api

Version:

58 lines (44 loc) 1.84 kB
const { commandFactory } = require('hystrixjs'); const promisify = require('util.promisify'); const callbackify = require('util').callbackify || function (promiser) { return function (...args) { const callback = args.pop(); promiser.apply(this, args).then( result => callback(null, result), error => callback(error) ); }; }; module.exports = (redis, options) => { const service = promisify((command_obj, callback) => { command_obj.callback = callback; }); const fallback = promisify((error, [command_obj], callback) => { command_obj.command = command_obj.callback = null; callback(error); }); const breaker = commandFactory.getOrCreate(options.name) .circuitBreakerErrorThresholdPercentage(options.errorThreshold) .timeout(options.timeout) .run(service) .circuitBreakerRequestVolumeThreshold(options.volumeThreshold) .requestVolumeRejectionThreshold(options.rejectionThreshold) .circuitBreakerSleepWindowInMilliseconds(options.sleepWindow) .statisticalWindowLength(options.windowLength) .statisticalWindowNumberOfBuckets(options.windowBuckets) .fallbackTo(fallback) .build(); const { createClient } = redis; redis.createClient = function () { const client = createClient.apply(this, arguments); const { internal_send_command } = client; client.internal_send_command = function (command_obj) { const { command, callback = function () {} } = command_obj; if (!command) return true; callbackify(breaker.execute).call(breaker, command_obj, callback); return internal_send_command.call(this, command_obj); }; return client; }; return redis; };