| 406 | } |
| 407 | |
| 408 | AWS_LAMBDA_RUNTIME_API |
| 409 | void run_handler(std::function<invocation_response(invocation_request const&)> const& handler) |
| 410 | { |
| 411 | logging::log_info(LOG_TAG, "Initializing the C++ Lambda Runtime version %s", aws::lambda_runtime::get_version()); |
| 412 | std::string endpoint("http://"); |
| 413 | if (auto* ep = std::getenv("AWS_LAMBDA_RUNTIME_API")) { |
| 414 | assert(ep); |
| 415 | logging::log_debug(LOG_TAG, "LAMBDA_SERVER_ADDRESS defined in environment as: %s", ep); |
| 416 | endpoint += ep; |
| 417 | } |
| 418 | |
| 419 | runtime rt(endpoint); |
| 420 | |
| 421 | size_t retries = 0; |
| 422 | size_t const max_retries = 3; |
| 423 | |
| 424 | while (retries < max_retries) { |
| 425 | auto next_outcome = rt.get_next(); |
| 426 | if (!next_outcome.is_success()) { |
| 427 | if (next_outcome.get_failure() == aws::http::response_code::REQUEST_NOT_MADE) { |
| 428 | ++retries; |
| 429 | continue; |
| 430 | } |
| 431 | |
| 432 | logging::log_info( |
| 433 | LOG_TAG, |
| 434 | "HTTP request was not successful. HTTP response code: %d. Retrying..", |
| 435 | static_cast<int>(next_outcome.get_failure())); |
| 436 | ++retries; |
| 437 | continue; |
| 438 | } |
| 439 | |
| 440 | retries = 0; |
| 441 | |
| 442 | auto const req = std::move(next_outcome).get_result(); |
| 443 | logging::log_info(LOG_TAG, "Invoking user handler"); |
| 444 | invocation_response res = handler(req); |
| 445 | logging::log_info(LOG_TAG, "Invoking user handler completed."); |
| 446 | |
| 447 | if (res.is_success()) { |
| 448 | const auto post_outcome = rt.post_success(req.request_id, res); |
| 449 | if (!handle_post_outcome(post_outcome, req.request_id)) { |
| 450 | return; // TODO: implement a better retry strategy |
| 451 | } |
| 452 | } |
| 453 | else { |
| 454 | const auto post_outcome = rt.post_failure(req.request_id, res); |
| 455 | if (!handle_post_outcome(post_outcome, req.request_id)) { |
| 456 | return; // TODO: implement a better retry strategy |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | if (retries == max_retries) { |
| 462 | logging::log_error( |
| 463 | LOG_TAG, "Exhausted all retries. This is probably a bug in libcurl v" LIBCURL_VERSION " Exiting!"); |
| 464 | } |
| 465 | } |
no test coverage detected