Swan Lake Beta3 is here.
import ballerina/http;

// The circuit breaker looks for errors across a rolling time window.
// After the circuit is broken, it does not send requests to
// the backend until the `resetTime`.
http:Client cbrBackend = check new ("http://localhost:8080", {
            // Configuration options that control the behavior of the circuit
            // breaker.
            circuitBreaker: {
                // Failure calculation window. This is how long the circuit
                // breaker keeps the statistics for the operations.
                rollingWindow: {

                    // Time period in seconds for which the failure
                    // threshold is calculated.
                    timeWindow: 10,

                    // The granularity (in seconds) at which the time
                    // window slides. The `RollingWindow` is divided into
                    // buckets and slides by these increments.
                    bucketSize: 2,

                    // Minimum number of requests in the `RollingWindow` that
                    // will trip the circuit.
                    requestVolumeThreshold: 0

                },
                // The threshold for request failures.
                // When this threshold exceeds, the circuit trips. This is the
                // ratio between failures and total requests. The ratio is
                // calculated using the requests received within the given
                // rolling window.
                failureThreshold: 0.2,

                // The time period (in seconds) to wait before attempting to
                // make another request to the upstream service.
                resetTime: 10,

                // HTTP response status codes that are considered as failures
                statusCodes: [400, 404, 500]

            },
            timeout: 2
        }
    );

service / on new http:Listener(9090) {
    resource function get cb() returns string|error {
        string payload = check cbrBackend->get("/hello");
        return payload;
    }
}

// This sample service is used to mock connection timeouts and service outages.
// This should run separately from the above service.
service / on new http:Listener(8080) {
    private int counter = 1;
    resource function get hello() returns string|http:InternalServerError {
        if (self.counter % 5 == 3) {
            self.counter += 1;
            return {body:"Error occurred while processing the request."};
        } else {
            self.counter += 1;
            return "Hello World!!!";
        }
    }
}

Circuit Breaker

The Circuit Breaker is used to gracefully handle network related errors, which occur when using the HTTP Client. Behavior of this example is something similar to as follows, 1) First two requests works, 2) Third request fails and the circuit breaker trips, 3) As a result subsequent requests fails immediately until the timeout period is reached, 4) Timeout is reached and the circuit breaker falls back to closed state.

For more information on the underlying module, see the HTTP module.

import ballerina/http;
http:Client cbrBackend = check new ("http://localhost:8080", {

The circuit breaker looks for errors across a rolling time window. After the circuit is broken, it does not send requests to the backend until the resetTime.

            circuitBreaker: {

Configuration options that control the behavior of the circuit breaker.

                rollingWindow: {

Failure calculation window. This is how long the circuit breaker keeps the statistics for the operations.

                    timeWindow: 10,

Time period in seconds for which the failure threshold is calculated.

                    bucketSize: 2,

The granularity (in seconds) at which the time window slides. The RollingWindow is divided into buckets and slides by these increments.

                    requestVolumeThreshold: 0

Minimum number of requests in the RollingWindow that will trip the circuit.

                },
                failureThreshold: 0.2,

The threshold for request failures. When this threshold exceeds, the circuit trips. This is the ratio between failures and total requests. The ratio is calculated using the requests received within the given rolling window.

                resetTime: 10,

The time period (in seconds) to wait before attempting to make another request to the upstream service.

                statusCodes: [400, 404, 500]

HTTP response status codes that are considered as failures

            },
            timeout: 2
        }
    );
service / on new http:Listener(9090) {
    resource function get cb() returns string|error {
        string payload = check cbrBackend->get("/hello");
        return payload;
    }
}
service / on new http:Listener(8080) {
    private int counter = 1;
    resource function get hello() returns string|http:InternalServerError {
        if (self.counter % 5 == 3) {
            self.counter += 1;
            return {body:"Error occurred while processing the request."};
        } else {
            self.counter += 1;
            return "Hello World!!!";
        }
    }
}

This sample service is used to mock connection timeouts and service outages. This should run separately from the above service.

bal run http_circuit_breaker.bal
[ballerina/http] started HTTP/WS listener 0.0.0.0:9090
[ballerina/http] started HTTP/WS listener 0.0.0.0:8080
# Keep sending the below request to experience how circuit breaker works.
curl http://localhost:9090/cb
Hello World!!!
import ballerina/http;

// The circuit breaker looks for errors across a rolling time window.
// After the circuit is broken, it does not send requests to
// the backend until the `resetTime`.
http:Client cbrBackend = check new ("http://localhost:8080", {
            // Configuration options that control the behavior of the circuit
            // breaker.
            circuitBreaker: {
                // Failure calculation window. This is how long the circuit
                // breaker keeps the statistics for the operations.
                rollingWindow: {

                    // Time period in seconds for which the failure
                    // threshold is calculated.
                    timeWindow: 10,

                    // The granularity (in seconds) at which the time
                    // window slides. The `RollingWindow` is divided into
                    // buckets and slides by these increments.
                    bucketSize: 2,

                    // Minimum number of requests in the `RollingWindow` that
                    // will trip the circuit.
                    requestVolumeThreshold: 0

                },
                // The threshold for request failures.
                // When this threshold exceeds, the circuit trips. This is the
                // ratio between failures and total requests. The ratio is
                // calculated using the requests received within the given
                // rolling window.
                failureThreshold: 0.2,

                // The time period (in seconds) to wait before attempting to
                // make another request to the upstream service.
                resetTime: 10,

                // HTTP response status codes that are considered as failures
                statusCodes: [400, 404, 500]

            },
            timeout: 2
        }
    );

service / on new http:Listener(9090) {
    resource function get cb() returns string|error {
        string payload = check cbrBackend->get("/hello");
        return payload;
    }
}

// This sample service is used to mock connection timeouts and service outages.
// This should run separately from the above service.
service / on new http:Listener(8080) {
    private int counter = 1;
    resource function get hello() returns string|http:InternalServerError {
        if (self.counter % 5 == 3) {
            self.counter += 1;
            return {body:"Error occurred while processing the request."};
        } else {
            self.counter += 1;
            return "Hello World!!!";
        }
    }
}

Circuit Breaker

The Circuit Breaker is used to gracefully handle network related errors, which occur when using the HTTP Client. Behavior of this example is something similar to as follows, 1) First two requests works, 2) Third request fails and the circuit breaker trips, 3) As a result subsequent requests fails immediately until the timeout period is reached, 4) Timeout is reached and the circuit breaker falls back to closed state.

For more information on the underlying module, see the HTTP module.

import ballerina/http;
http:Client cbrBackend = check new ("http://localhost:8080", {

The circuit breaker looks for errors across a rolling time window. After the circuit is broken, it does not send requests to the backend until the resetTime.

            circuitBreaker: {

Configuration options that control the behavior of the circuit breaker.

                rollingWindow: {

Failure calculation window. This is how long the circuit breaker keeps the statistics for the operations.

                    timeWindow: 10,

Time period in seconds for which the failure threshold is calculated.

                    bucketSize: 2,

The granularity (in seconds) at which the time window slides. The RollingWindow is divided into buckets and slides by these increments.

                    requestVolumeThreshold: 0

Minimum number of requests in the RollingWindow that will trip the circuit.

                },
                failureThreshold: 0.2,

The threshold for request failures. When this threshold exceeds, the circuit trips. This is the ratio between failures and total requests. The ratio is calculated using the requests received within the given rolling window.

                resetTime: 10,

The time period (in seconds) to wait before attempting to make another request to the upstream service.

                statusCodes: [400, 404, 500]

HTTP response status codes that are considered as failures

            },
            timeout: 2
        }
    );
service / on new http:Listener(9090) {
    resource function get cb() returns string|error {
        string payload = check cbrBackend->get("/hello");
        return payload;
    }
}
service / on new http:Listener(8080) {
    private int counter = 1;
    resource function get hello() returns string|http:InternalServerError {
        if (self.counter % 5 == 3) {
            self.counter += 1;
            return {body:"Error occurred while processing the request."};
        } else {
            self.counter += 1;
            return "Hello World!!!";
        }
    }
}

This sample service is used to mock connection timeouts and service outages. This should run separately from the above service.

bal run http_circuit_breaker.bal
[ballerina/http] started HTTP/WS listener 0.0.0.0:9090
[ballerina/http] started HTTP/WS listener 0.0.0.0:8080
# Keep sending the below request to experience how circuit breaker works.
curl http://localhost:9090/cb
Hello World!!!
Subscribe to the newsletter

In the creation of Ballerina, we were inspired by so many technologies. Thank you to all that have come before us (and forgive us if we missed one): Java, Go, C, C++, D, Rust, Haskell, Kotlin, Dart, TypeScript, JavaScript, Python, Perl, Flow, Swift, Elm, RelaxNG, NPM, Crates, Maven, Gradle, Kubernetes, Docker, Envoy, Markdown, GitHub and WSO2.

Cookie Policy

This website uses cookies so that we can provide you with the best user experience. Read our Cookie Policy to find out more.

If you wish to disable cookies you can do so from your browser.

I Understand