The Circuit Breaker pattern is used to shield an application from an integration that might become unavailable or unresponsive.
It works like a circuit breaker in an electrical circuit, in that it 'opens' when problems with the integration are detected. While the circuit breaker is open, no communication with the integration will be allowed for a certain period of time. After this waiting period, communication will be allowed again and the circuit breaker will be 'half-open' until a certain number of successful requests have been made, after which the circuit breaker will close again and let all communication through.
The integration module (typically an _IS module) that will use the Circuit Breaker first needs to 'install' it.
This can be done in a Timer that calls the CircuitBreaker_Install server action:
The EndpointKey is the unique id (or name) of the Circuit Breaker.
This Timer should be scheduled On Publish.
In your server actions that call your integration (an API for example), you need to first check the Circuit Breaker, like this:
You pass the same EndpointKey to CircuitBreaker_Check as you used for installing the Circuit Breaker.
CircuitBreaker_Check will return the Status of the CircuitBreaker (Closed, Open or HalfOpen), and if communication IsAllowed.
If communication is not allowed, throw a CircuitBreakerException (create this User Exception), and handle it as follows:
Return the fact that the service is unavailable using an output parameter IsUnavailable.
This output parameter can then be used in your UI for example to give the user a message stating that, for the moment, the service is unavailable.
At the end of your server action where you check the Circuit Breaker, you need to update the Circuit Breaker if its status was HalfOpen.
Finally, add an Exception Handler for All Exceptions where you also update the Circuit Breaker, so it can open after a certain amount of errors were detected:
Your complete server action will now look something like this:
You can use the server action CircuitBreaker_GetStatusDetails to get the status of the circuit breaker.
This can be used to show a message in you user interface to warn users that certain functionality might be temporarily unavailable.