Skip to content

Global timeout feature (same as "--max-time" curl option) #2034

Closed
@marcovc

Description

Hi,

I've finally had some time to investigate the issue reported here, and created a small example:

A client that wants to have a reply in at most 2100 ms:

#include <httplib.h>

int main()
{

  httplib::Client http_client("http://127.0.0.1:8082");
  auto timeout = std::chrono::milliseconds{2100};
  http_client.set_connection_timeout(timeout);
  http_client.set_read_timeout(timeout);
  http_client.set_write_timeout(timeout);
  auto start = std::chrono::steady_clock::now();
  http_client.Get("/");
  auto end = std::chrono::steady_clock::now();
  auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
  std::cout << "Elapsed time: " << elapsed.count() << "ms\n";
}

A simple server to test (in python):

from http.server import BaseHTTPRequestHandler, HTTPServer
import time

class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        if self.path == "/":
            self.send_response(200)
            self.send_header("Content-type", "text/html")
            self.end_headers()
            for i in range(5):
                time.sleep(1)
                self.wfile.write(f"Response part {i+1}\n".encode('utf-8'))            
        else:
            self.send_response(404)
            self.end_headers()

def run(server_class=HTTPServer, handler_class=SimpleHTTPRequestHandler):
    server_address = ('', 8082)
    httpd = server_class(server_address, handler_class)
    print("Starting httpd server on port 8082...")
    httpd.serve_forever()

if __name__ == "__main__":
    run()

It will print something like

Elapsed time: 5001ms

while I was expecting something close to

Elapsed time: 2100ms

Perhaps there is another way to set a timeout for the request?

Thanks

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions