@Override
    protected void doApply(ApiRequest request, IPolicyContext context, CircuitBreakerConfigBean config,
            IPolicyChain<ApiRequest> chain) {
        CircuitKey ckey = new CircuitKey(request.getApiOrgId(), request.getApiId(), request.getApiVersion());
        Circuit circuit = circuits.get(ckey);
        if (circuit == null) {
            circuit = new Circuit(config.getLimit(), config.getWindow(), config.getReset());
            circuits.put(ckey, circuit);
        }
        
        context.setAttribute(CircuitBreakerPolicy.class.getName() + ".circuit", circuit); //$NON-NLS-1$

        // Is the circuit broken?  If so, either immediately send a failure, or else
        // if the circuit is ready to be reset (the reset time has elapsed) then try
        // to reset the circuit by letting through the request and seeing what happens.
        if (circuit.isBroken()) {
            // Can the circuit possibly be reset?  If so, try...otherwise fail.
            if (circuit.isResettable()) {
                circuit.startReset();
                super.doApply(request, context, config, chain);
            } else {
                IPolicyFailureFactoryComponent failureFactory = context.getComponent(IPolicyFailureFactoryComponent.class);
                PolicyFailure failure = failureFactory.createFailure(PolicyFailureType.Other, BROKEN_CIRCUIT_FAILURE_CODE, "Circuit broken."); //$NON-NLS-1$
                failure.setResponseCode(config.getFailureCode());
                chain.doFailure(failure);
            }
        } else {
            super.doApply(request, context, config, chain);
        }
    }
@see io.apiman.gateway.engine.policies.AbstractMappedPolicy#doApply(io.apiman.gateway.engine.beans.ApiRequest, io.apiman.gateway.engine.policy.IPolicyContext, java.lang.Object, io.apiman.gateway.engine.policy.IPolicyChain)
plugins/circuit-breaker-policy/src/main/java/io/apiman/plugins/circuit_breaker/CircuitBreakerPolicy.java