Skip to content

Commit

Permalink
burl: add --max-time option
Browse files Browse the repository at this point in the history
  • Loading branch information
ashtum committed Dec 7, 2024
1 parent 78f53ea commit 0bf43d6
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions example/client/burl/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <filesystem>
#include <cstdlib>

namespace ch = std::chrono;
namespace fs = std::filesystem;
namespace http_io = boost::http_io;
using system_error = boost::system::system_error;
Expand Down Expand Up @@ -220,7 +221,6 @@ request(
http_proto::request request,
const urls::url_view& url)
{
namespace ch = std::chrono;
using field = http_proto::field;
auto executor = co_await asio::this_coro::executor;
auto parser = http_proto::response_parser{ http_proto_ctx };
Expand Down Expand Up @@ -547,6 +547,9 @@ main(int argc, char* argv[])
("max-redirs",
po::value<std::int32_t>()->value_name("<num>"),
"Maximum number of redirects allowed")
("max-time",
po::value<float>()->value_name("<frac sec>"),
"Maximum time allowed for transfer")
("no-keepalive", "Disable TCP keepalive on the connection")
("output,o",
po::value<std::string>()->value_name("<file>"),
Expand Down Expand Up @@ -951,6 +954,11 @@ main(int argc, char* argv[])
throw std::runtime_error{
"showing headers and --remote-header-name cannot be combined" };

auto timeout = vm.count("max-time")
? ch::duration_cast<ch::steady_clock::duration>(
ch::duration<float>(vm.at("max-time").as<float>()))
: ch::steady_clock::duration::max();

asio::co_spawn(
ioc,
request(
Expand All @@ -966,11 +974,13 @@ main(int argc, char* argv[])
http_proto_ctx,
create_request(vm, msg, url),
url),
[](std::exception_ptr ep)
{
if(ep)
std::rethrow_exception(ep);
});
asio::cancel_after(
timeout,
[](std::exception_ptr ep)
{
if(ep)
std::rethrow_exception(ep);
}));

ioc.run();

Expand Down

0 comments on commit 0bf43d6

Please sign in to comment.