Fastly Compute C++ SDK
Loading...
Searching...
No Matches
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::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();
return 1;
}
log::info("Request from {} won. Printing it out.",
resp->get_backend_name().value());
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();
}
int main()
Definition async_reqs.cpp:10
static Request get(std::string_view url)
static Response from_status(StatusCode status)
Create a new response with the given status code.
Definition body.h:42
static const StatusCode INTERNAL_SERVER_ERROR
Definition status_code.h:227
std::pair< fastly::expected< fastly::http::Response >, std::vector< PendingRequest > > select(std::vector< PendingRequest > &reqs)
Definition log.h:192
void init_simple(Endpoint endpoint, LogLevelFilter level)
void info(std::format_string< Args... > fmt, Args &&...args)
Definition log.h:294
Definition async_reqs.cpp:6