UTIL_CircuitBreaker.Breaker
Class
global interface UTIL_CircuitBreaker.BreakerInterface for circuit breaker operations. This interface defines the contract for circuit breaker behavior, including: Request gating (allowRequest) Success/failure recording State inspection and metrics Configuration via fluent API Convenient execute() helpers for protected actions
Since: 1.0
Methods
| Method | Description |
|---|---|
| global abstract Boolean allowRequest() | Determines if a request is allowed through the circuit breaker |
| global abstract void execute(UTIL_CircuitBreaker.ProtectedAction action) | Executes an action within the circuit breaker context This is a convenience method that abstracts the boilerplate pattern: Checks if request is allowed (throws exception if circuit is OPEN) Executes the action Records success/failure automatically |
| global abstract Object execute(UTIL_CircuitBreaker.Provider action) | Executes an action within the circuit breaker context (with return value) This overload allows actions to return data, eliminating the need for member variables to capture API responses. |
| global abstract void forceOpen() | Manually forces the circuit breaker to OPEN state (use with caution) |
| global abstract UTIL_CircuitBreaker.Metrics getMetrics() | Gets detailed metrics about the circuit breaker |
| global abstract UTIL_CircuitBreaker.State getState() | Gets the current state of the circuit breaker |
| global abstract void recordFailure() | Records a failed request |
| global abstract void recordSuccess() | Records a successful request |
| global abstract void reset() | Manually resets the circuit breaker to CLOSED state (use with caution) |
| global abstract UTIL_CircuitBreaker.Breaker withFailureThreshold(Integer threshold) | Sets the failure threshold (fluent API) |
| global abstract UTIL_CircuitBreaker.Breaker withHalfOpenMaxAttempts(Integer maxAttempts) | Sets the maximum attempts allowed in HALF_OPEN state (fluent API) |
| global abstract UTIL_CircuitBreaker.Breaker withSuccessThreshold(Integer threshold) | Sets the success threshold for HALF_OPEN state (fluent API) |
| global abstract UTIL_CircuitBreaker.Breaker withTimeout(Integer seconds) | Sets the timeout period in seconds (fluent API) |
Method Details
allowRequest
global abstract Boolean allowRequest()Determines if a request is allowed through the circuit breaker
Returns: Boolean - True if the request should proceed, false if it should fail fast
execute
global abstract void execute(UTIL_CircuitBreaker.ProtectedAction action)Executes an action within the circuit breaker context
This is a convenience method that abstracts the boilerplate pattern:
Checks if request is allowed (throws exception if circuit is OPEN)
Executes the action
Records success/failure automatically
Parameters:
action(UTIL_CircuitBreaker.ProtectedAction) - The action to execute with circuit breaker protection
Throws:
- Exception - if the circuit is OPEN and request is blocked
Example:
UTIL_CircuitBreaker.Breaker breaker = UTIL_CircuitBreaker.monitor('API_SendGridEmail');
try
{
breaker.execute(new SendEmailAction('user@example.com'));
}
catch(UTIL_CircuitBreaker.OpenException e)
{
LOG_Builder.build().error(e).at('MyClass.sendEmail').emit();
// Handle circuit open scenario
}execute
global abstract Object execute(UTIL_CircuitBreaker.Provider action)Executes an action within the circuit breaker context (with return value)
This overload allows actions to return data, eliminating the need for member variables to capture API responses.
Parameters:
action(UTIL_CircuitBreaker.Provider) - The action to execute with circuit breaker protection
Returns: Object - The result from action.execute() (caller must cast to appropriate type)
Throws:
- Exception - if the circuit is OPEN and request is blocked
Example:
UTIL_CircuitBreaker.Breaker breaker = UTIL_CircuitBreaker.monitor('API_GetCustomerData');
try
{
DTO_CustomerResponse response = (DTO_CustomerResponse)breaker.execute(
new GetCustomerDataProvider(customerId)
);
System.debug('Customer Name: ' + response.name);
}
catch(UTIL_CircuitBreaker.OpenException e)
{
LOG_Builder.build().error(e).at('MyClass.getCustomerData').emit();
// Handle circuit open scenario
}forceOpen
global abstract void forceOpen()Manually forces the circuit breaker to OPEN state (use with caution)
getMetrics
global abstract UTIL_CircuitBreaker.Metrics getMetrics()Gets detailed metrics about the circuit breaker
Returns: UTIL_CircuitBreaker.Metrics - Circuit metrics including failure count, state, etc.
getState
global abstract UTIL_CircuitBreaker.State getState()Gets the current state of the circuit breaker
Returns: UTIL_CircuitBreaker.State - The current circuit state
recordFailure
global abstract void recordFailure()Records a failed request
recordSuccess
global abstract void recordSuccess()Records a successful request
reset
global abstract void reset()Manually resets the circuit breaker to CLOSED state (use with caution)
withFailureThreshold
global abstract UTIL_CircuitBreaker.Breaker withFailureThreshold(Integer threshold)Sets the failure threshold (fluent API)
Parameters:
threshold(Integer) - Number of failures before opening the circuit
Returns: UTIL_CircuitBreaker.Breaker - This instance for method chaining
withHalfOpenMaxAttempts
global abstract UTIL_CircuitBreaker.Breaker withHalfOpenMaxAttempts(Integer maxAttempts)Sets the maximum attempts allowed in HALF_OPEN state (fluent API)
Parameters:
maxAttempts(Integer) - Maximum number of requests allowed in HALF_OPEN state
Returns: UTIL_CircuitBreaker.Breaker - This instance for method chaining
withSuccessThreshold
global abstract UTIL_CircuitBreaker.Breaker withSuccessThreshold(Integer threshold)Sets the success threshold for HALF_OPEN state (fluent API)
Parameters:
threshold(Integer) - Number of consecutive successes needed to close circuit
Returns: UTIL_CircuitBreaker.Breaker - This instance for method chaining
withTimeout
global abstract UTIL_CircuitBreaker.Breaker withTimeout(Integer seconds)Sets the timeout period in seconds (fluent API)
Parameters:
seconds(Integer) - Timeout in seconds before transitioning from OPEN to HALF_OPEN
Returns: UTIL_CircuitBreaker.Breaker - This instance for method chaining