awolden / brakes

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

Fallbacks executions #90

Open pauloballan opened 6 years ago

pauloballan commented 6 years ago

Hello!

I am implementing this circuit breaker for node.js and i noted something strange. In a normal flow for my, in this case, API call i always see that the fallback functions is being called, the service is working ok and the response is totally fine, but whenever i set a breakpoint in the fallback, it stops right there, even if the flow goes ok. I am not sure why is it executing if there are not errors in the api call.

Thanks!

awolden commented 6 years ago

Hey @pauloballan, that definitely shouldn't be happening. Can you give a code snippet of your implementation that I could look at?

Thanks,

-Alex

pauloballan commented 6 years ago

Hi @awolden! Thanks for such a quick answer. I think i'm not getting it right the usage for the breaker. I am currently using callbacks in my project and the apicall is something like this:

`function apicall(params, callback) {

if (!params) {
    return callback({
        message: 'Invalid or missing params'
    });
}
client.get(opts, function(error, response){
  error = 'error';
  if (error){
    callback(error);
  }
  return callback(undefined, response && response.body);
});

};`

This function is forcing the error in order to see fallback working. And this is the brakes setting up:

const brake = new brakes(apicall, { statInterval: 2500, threshold: 0.2, circuitDuration: 15000, timeout: 1000 });

let apicallBrakes = function(params, callback){ brake.exec(params, callback); };

apicallBrakes is the one called as a client service. With this code, the fallback is always being executed even is not throwing an error. I am not sure if this is recognized as an error or i am doing it wrong. In this case it still calls the fallback (noted it using a console.log) but the apicall is not returning it.

Thanks!

awolden commented 6 years ago

I don't see Hey @pauloballan thanks for sharing the code. Based on this I don't see where a fallback is being defined. Normally a fallback is defined via brakesInstance.fallback() or setting a fallback function in the initial options object.