-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathasync_reqs.cpp
More file actions
39 lines (37 loc) · 1.4 KB
/
async_reqs.cpp
File metadata and controls
39 lines (37 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//! @example async_reqs.cpp
#include "fastly/sdk.h"
#include "request.h"
#include <iostream>
namespace log {
using namespace fastly::log;
}
int main() {
// We can't use a plain `select({req1, req2, ...})` because PendingRequests
// are not copyable, so we have to push them when building up the
// `std::vector`.
log::init_simple("logs");
log::info("Sending off two requests...");
std::vector<fastly::http::request::PendingRequest> pending;
pending.push_back(fastly::Request::get("https://www.fastly.com/")
.send_async("fastly")
.value());
pending.push_back(fastly::Request::get("https://en.wikipedia.org/wiki/Fastly")
.send_async("wikipedia")
.value());
log::info("using select() to race the two requests...");
auto [resp, _other_pending] = fastly::http::request::select(pending);
if (!resp) {
std::cerr << resp.error().error_msg();
fastly::Response::from_status(
fastly::http::StatusCode::INTERNAL_SERVER_ERROR)
.send_to_client();
return 1;
}
log::info("Request from {} won. Printing it out.",
resp->get_backend_name().value());
fastly::Body tail;
tail << "\n\nThis was the request from " << resp->get_backend_name().value()
<< "\nLocation: " << resp->take_backend_request().value().get_url();
resp->append_body(std::move(tail));
resp->send_to_client();
}