awolden / brakes

Hystrix compliant Node.js Circuit Breaker Library
MIT License
300 stars 35 forks source link

Slave circuits not working as per the examples #78

Closed CFreeAetna closed 7 years ago

CFreeAetna commented 7 years ago

I'm using the following code:

const Brakes = require('brakes');
const fallback = require('../lib/circuit-breaker-fallback');

const breakerOpts = {
      timeout: 150,
      fallback,
};

const promisifiedPUT = () => {
  return new Promise((resolve, reject) => {
    serviceCall(optsObj, (err, res) => {
      if (err) {
          reject(err);
      } else {
          resolve(res);
      }
    });
  });
};

const brake = new Brakes(breakerOpts);
const slaveCircuit = brake.slaveCircuit(promisifiedPUT);

slaveCircuit.exec()
  .then(handleSuccess)
  .catch(handleFailure);
...

I get the following error:

/path/node_modules/brakes/lib/Brakes.js:174
      throw new Error(consts.NO_FUNCTION);
      ^

Error: This instance of brakes has no master circuit. You must use a slave circuit.
    at Brakes.fallback (/path/node_modules/brakes/lib/Brakes.js:174:13)
    at new Brakes (/path/node_modules/brakes/lib/Brakes.js:71:12)
    at Object.<anonymous> (/path/server/models/greeting.js:17:23)

Following the trail, I get to here and here. This suggests to me that the constructor requires a function and a settings object, but the examples only show a setting object being passed in:

const brake = new Brakes({
  statInterval: 2500,
  threshold: 0.5,
  circuitDuration: 15000,
  timeout: 250
});

const greetingBrake = new Brakes(() => {}, breakerOpts); seems to work, but I'm not sure that's how I'm supposed to be using the library.

Any thoughts? Suggestions?

awolden commented 7 years ago

Thanks for reaching out about this @CFreeAtEbsco. This is definitely a bug in the library.

The library should not prevent you from setting a master fallback when using the library as a slave/master system. The way you originally used it should be the intended way to use it.

I have a proposed fix here: https://github.com/awolden/brakes/compare/fallback-fix

I will be fixing any broken tests, PRing it into master, and releasing a bugfix later today. This change will allow you set a master fallback that all slaves will use, or you can set slave specific fallbacks.

Thanks again for raising this issue.

-Alex