import ballerina/http;
import ballerina/lang.runtime;

final http:Client backendClientEP = check new ("http://localhost:8080", {
    // Timeout configuration.
    timeout: 10
});

// Create an HTTP service bound to the listener endpoint.
service / on new http:Listener(9090) {

    resource function get timeout() returns string|http:InternalServerError {
        string|error response = backendClientEP->get("/hello");

        // If `response` is a `string` (text/plain), it is sent back to the
        // client. If `response` is an `http:ClientError`, an internal
        // server error is returned to the client.
        if (response is string) {
            return response;
        } else {
            if (response is http:IdleTimeoutError) {
                return { body: 
                "Request timed out. Please try again in sometime."};
            } else {
                return { body: response.message()};
            }
        }

    }
}

// This sample service is used to mock connection timeouts.
service / on new http:Listener(8080) {

    resource function get hello() returns string {
        // Delay the response by 15 seconds to mimic the network level delays.
        runtime:sleep(15);
        return "Hello World!!!";
    }
}

Timeout

The Timeout is used to gracefully handle network timeouts, which occur when using the HTTP Client.

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

import ballerina/http;
import ballerina/lang.runtime;
final http:Client backendClientEP = check new ("http://localhost:8080", {
    timeout: 10
});

Timeout configuration.

service / on new http:Listener(9090) {

Create an HTTP service bound to the listener endpoint.

    resource function get timeout() returns string|http:InternalServerError {
        string|error response = backendClientEP->get("/hello");
        if (response is string) {
            return response;
        } else {
            if (response is http:IdleTimeoutError) {
                return { body: 
                "Request timed out. Please try again in sometime."};
            } else {
                return { body: response.message()};
            }
        }

If response is a string (text/plain), it is sent back to the client. If response is an http:ClientError, an internal server error is returned to the client.

    }
}
service / on new http:Listener(8080) {

This sample service is used to mock connection timeouts.

    resource function get hello() returns string {
        runtime:sleep(15);
        return "Hello World!!!";
    }
}

Delay the response by 15 seconds to mimic the network level delays.

bal run http_timeout.bal
[ballerina/http] started HTTP/WS listener 0.0.0.0:9090
[ballerina/http] started HTTP/WS listener 0.0.0.0:8080
curl -v http://localhost:9090/timeout
*   Trying 127.0.0.1:9090...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 9090 (#0)
> GET /timeout HTTP/1.1
> Host: localhost:9090
> User-Agent: curl/7.68.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 500 Internal Server Error
< content-type: text/plain
< content-length: 48
< server: ballerina
< date: Mon, 21 Sep 2020 20:36:56 +0530
< 
* Connection #0 to host localhost left intact
Request timed out. Please try again in sometime.

Invoke the service using “cURL”.