opossum
opossum copied to clipboard
Example of how to integrate into a larger system
Hi,
This is more of a question for an example / documentation links. I am testing out various circuit breakers and opossum is currently on my list. It seems to work well in the toy example provided where you have a simple function but I found that it fails in strange ways if you try to integrated it into a larger system.
For example using breaker.fire()
on a class method that calls other class methods seems to fail where it eventually complaints that some method is not available... This is strange and I am not sure why it happens and how I would go about integrating this into a larger system.
In my case I have a class method that calls another method which I am trying to wrap into the CircuitBreaker, i.e. breaker.fire(this.api.someOtherMethod)
. The someOtherMethod
calls a method called post
in the other class and the error I get now is [Node] ERROR with circuit breaker TypeError: this.post is not a function
Any links to examples would be appreciated! Thanks
Hi @vomc - thanks for the report. Any chance you can provide a reproducible example in code? I suspect it is related somehow to how JavaScript deals with this
.
@lance
Same weird behaviour happened with me. I use an dependency injection resolver and make use of classes. I only can use circuit breaker if I define a anonymous function that calls my class method inside it, otherwise it gives an error inside every foreign classes method calls. For example:
const playlistRecommendationService = container.resolve(PlaylistRecommendationService);
const circuitRecommendByCity = new CircuitBreaker(
playlistRecommendationService.recommendByCity,
circuitBreakerOptions,
);
const tracks = await circuitRecommendByCity.fire(cityName);
It throws:
TypeError: Cannot read property 'fetchTemperatureInCelsiusByCityName' of undefined
at Function.<anonymous> (/Users/dayvsonsales/microservices-resiliency-typescript/src/modules/playlists/services/PlaylistRecommendationService.ts:16:52)
at step (/Users/dayvsonsales/microservices-resiliency-typescript/src/modules/playlists/services/PlaylistRecommendationService.ts:45:23)
at Object.next (/Users/dayvsonsales/microservices-resiliency-typescript/src/modules/playlists/services/PlaylistRecommendationService.ts:26:53)
at /Users/dayvsonsales/microservices-resiliency-typescript/src/modules/playlists/services/PlaylistRecommendationService.ts:20:71
at new Promise (<anonymous>)
at __awaiter (/Users/dayvsonsales/microservices-resiliency-typescript/src/modules/playlists/services/PlaylistRecommendationService.ts:16:12)
at Function.PlaylistRecommendationService.recommendByCity (/Users/dayvsonsales/microservices-resiliency-typescript/src/modules/playlists/services/PlaylistRecommendationService.ts:63:16)
at /Users/dayvsonsales/microservices-resiliency-typescript/node_modules/opossum/lib/circuit.js:521:38
at new Promise (<anonymous>)
at CircuitBreaker.call (/Users/dayvsonsales/microservices-resiliency-typescript/node_modules/opossum/lib/circuit.js:497:12)
But if I use anonymous function, as the example below, everything works.
const playlistRecommendationService = container.resolve(
PlaylistRecommendationService,
);
const circuitRecommendByCity = new CircuitBreaker(
() => playlistRecommendationService.recommendByCity(cityName),
circuitBreakerOptions,
);
const tracks = await circuitRecommendByCity.fire();
Also, inside the PlaylistRecommendationService
I use a lot of this
context, maybe there's some problem with it.
This issue is stale because it has been open 30 days with no activity.
This issue is stale because it has been open 30 days with no activity.
@lholmquist do you think we can identify an actual issue to work on here? If not, maybe this issue can be closed?
This issue is stale because it has been open 30 days with no activity.
@dayvsonsales It's not connected to the library. This is how JS works. So if you want to call it without anonymous function - you should say to function what it should use as this
using bind. In your example this should work:
const playlistRecommendationService = container.resolve(PlaylistRecommendationService);
const circuitRecommendByCity = new CircuitBreaker(
playlistRecommendationService.recommendByCity.bind(playlistRecommendationService),
circuitBreakerOptions,
);
const tracks = await circuitRecommendByCity.fire(cityName);
This issue is stale because it has been open 30 days with no activity.