Skip to content

Commit

Permalink
Change setCompletionStatus to convertCompletionStatus
Browse files Browse the repository at this point in the history
So async and sync uses of Request only set the status code in one
place, change setCompletionStatus to convertCompletionStatus and
use the result of the function to set the status in either
onComplete (async) or Perform (sync).
  • Loading branch information
prestonfromspotx committed Jan 7, 2020
1 parent 419ecdf commit 9ee7166
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 44 deletions.
16 changes: 8 additions & 8 deletions inc/lift/EventLoop.h
Original file line number Diff line number Diff line change
Expand Up @@ -271,16 +271,16 @@ class EventLoop {
/**
* @param event_loop Reference to the EventLoop that is calling onComplete (so requests that have
* response wait times can be removed from the multiset of ResponseWaitTimeWrapper)
* @param completion_status RequestStatus enum indicating the status of the lift Request when it has completed
* (Success, error, etc.).
* @param shared_request Shared pointer to the SharedRequest that owns this Request, so it can be used to create
* a RequestHandle and return the Request to the RequestPool if necessary.
* @param response_wait_time_timeout Bool indicating whether or not onComplete was called because
* a response wait time was exceeded (true) or not (false)
* @param finish_time Optional that will contain a uint64_t indicating the timepoint when
* the request was timed out while waiting for the response time.
* If the request received a response or timed out via cURL, this will be
* empty and we'll get the total time from the cURL handle.
* a RequestHandle and return the Request to the RequestPool if necessary.
* @param finish_time Optional that will contain a uint64_t indicating the timepoint when request was timed out
* while waiting for the response time. If the request received a response or timed out via
* cURL, this will be empty and we'll get the total time from the cURL handle.
* Default is an empty optional.
*/
friend auto Request::onComplete(EventLoop& event_loop, std::shared_ptr<SharedRequest> shared_request, std::optional<uint64_t> finish_time) -> void;
friend auto Request::onComplete(EventLoop& event_loop, RequestStatus completion_status, std::shared_ptr<SharedRequest> shared_request, std::optional<uint64_t> finish_time) -> void;
};

} // lift
Expand Down
8 changes: 5 additions & 3 deletions inc/lift/Request.h
Original file line number Diff line number Diff line change
Expand Up @@ -415,21 +415,23 @@ class Request {
/**
* Converts a CURLcode into a RequestStatus.
* @param curl_code The CURLcode to convert.
* @return The lift RequestStatus that corresponds to the CURLcode
*/
auto setCompletionStatus(
CURLcode curl_code) -> void;
auto convertCompletionStatus(CURLcode curl_code) -> RequestStatus;

/**
* @param event_loop Reference to the EventLoop that is calling onComplete (so requests that have
* response wait times can be removed from the multiset of ResponseWaitTimeWrapper)
* @param completion_status RequestStatus enum indicating the status of the lift Request when it has completed
* (Success, error, etc.).
* @param shared_request Shared pointer to the SharedRequest that owns this Request, so it can be used to create
* a RequestHandle and return the Request to the RequestPool if necessary.
* @param finish_time Optional that will contain a uint64_t indicating the timepoint when request was timed out
* while waiting for the response time. If the request received a response or timed out via
* cURL, this will be empty and we'll get the total time from the cURL handle.
* Default is an empty optional.
*/
auto onComplete(EventLoop& event_loop, std::shared_ptr<SharedRequest> shared_request, std::optional<uint64_t> finish_time = std::nullopt) -> void;
auto onComplete(EventLoop& event_loop, RequestStatus completion_status, std::shared_ptr<SharedRequest> shared_request, std::optional<uint64_t> finish_time = std::nullopt) -> void;

/**
* Helper function to find how many bytes are left to be downloaded for a request
Expand Down
16 changes: 6 additions & 10 deletions src/EventLoop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ auto EventLoop::stopTimedOutRequests() -> void

// Call onComplete on the underlying Request object, copying in the shared_pointer
// so we get a correct reference count.
shared_request->GetAsReference().onComplete(*this, shared_request, now);
shared_request->GetAsReference().onComplete(*this, RequestStatus::RESPONSE_WAIT_TIME_TIMEOUT, shared_request, now);
}

// If there are still items in the multiset, get the first item and use its time to reset the request timer.
Expand Down Expand Up @@ -294,8 +294,7 @@ auto EventLoop::checkActions(
auto shared_request_ptr = *shared_request_ptr_pointer;
auto& shared_request_ref = shared_request_ptr->GetAsReference();

shared_request_ref.setCompletionStatus(easy_result);
shared_request_ref.onComplete(*this, shared_request_ptr);
shared_request_ref.onComplete(*this, shared_request_ref.convertCompletionStatus(easy_result), shared_request_ptr);

--m_active_request_count;
}
Expand Down Expand Up @@ -495,14 +494,11 @@ auto requests_accept_async(uv_async_t* handle) -> void

if(curl_code != CURLM_OK && curl_code != CURLM_CALL_MULTI_PERFORM)
{
/**
* If curl_multi_add_handle fails then notify the user that the request failed to start
* immediately.
/*
* If curl_multi_add_handle fails then notify the user that the request failed to start immediately.
* If we are calling onComplete now, move the shared_ptr into the method so it cleans itself up correctly.
*/
request_handle->setCompletionStatus(CURLcode::CURLE_SEND_ERROR);

// If we are calling onComplete now, move the shared_ptr into the method so it cleans itself up correctly.
request_handle->onComplete(*event_loop, *shared_request_on_heap);
request_handle->onComplete(*event_loop, RequestStatus::ERROR_FAILED_TO_START, *shared_request_on_heap);
}
else
{
Expand Down
36 changes: 13 additions & 23 deletions src/Request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ auto Request::Perform() -> bool
{
prepareForPerform();
auto curl_error_code = curl_easy_perform(m_curl_handle);
setCompletionStatus(curl_error_code);
m_status_code = convertCompletionStatus(curl_error_code);
return (m_status_code == RequestStatus::SUCCESS);
}

Expand Down Expand Up @@ -451,52 +451,41 @@ auto Request::clearResponseBuffers() -> void
m_response_data.clear();
}

auto Request::setCompletionStatus(
CURLcode curl_code) -> void
auto Request::convertCompletionStatus(CURLcode curl_code) -> RequestStatus
{

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wswitch-enum"
switch (curl_code) {
case CURLcode::CURLE_OK:
m_status_code = RequestStatus::SUCCESS;
break;
return RequestStatus::SUCCESS;
case CURLcode::CURLE_GOT_NOTHING:
m_status_code = RequestStatus::RESPONSE_EMPTY;
break;
return RequestStatus::RESPONSE_EMPTY;
case CURLcode::CURLE_OPERATION_TIMEDOUT:
m_status_code = RequestStatus::TIMEOUT;
break;
return RequestStatus::TIMEOUT;
case CURLcode::CURLE_COULDNT_CONNECT:
m_status_code = RequestStatus::CONNECT_ERROR;
break;
return RequestStatus::CONNECT_ERROR;
case CURLcode::CURLE_COULDNT_RESOLVE_HOST:
m_status_code = RequestStatus::CONNECT_DNS_ERROR;
break;
return RequestStatus::CONNECT_DNS_ERROR;
case CURLcode::CURLE_SSL_CONNECT_ERROR:
m_status_code = RequestStatus::CONNECT_SSL_ERROR;
break;
return RequestStatus::CONNECT_SSL_ERROR;
case CURLcode::CURLE_WRITE_ERROR:
/**
* If there is a cURL write error, but the maximum number of bytes has been written,
* then we intentionally aborted, so let's set this to success.
* Otherwise, there was an error in the CURL write callback.
*/
m_status_code = (getRemainingDownloadBytes() == 0)
return (getRemainingDownloadBytes() == 0)
? RequestStatus::SUCCESS
: RequestStatus::DOWNLOAD_ERROR;
break;
case CURLcode::CURLE_SEND_ERROR:
m_status_code = RequestStatus::ERROR_FAILED_TO_START;
break;
return RequestStatus::ERROR_FAILED_TO_START;
default:
m_status_code = RequestStatus::ERROR;
break;
return RequestStatus::ERROR;
}
#pragma GCC diagnostic pop
}

auto Request::onComplete(EventLoop& event_loop, std::shared_ptr<SharedRequest> shared_request, std::optional<uint64_t> finish_time) -> void
auto Request::onComplete(EventLoop& event_loop, RequestStatus completion_status, std::shared_ptr<SharedRequest> shared_request, std::optional<uint64_t> finish_time) -> void
{
auto request_handle_ptr = RequestHandle(std::move(shared_request));

Expand All @@ -510,6 +499,7 @@ auto Request::onComplete(EventLoop& event_loop, std::shared_ptr<SharedRequest> s
}

setTotalTime(finish_time);
m_status_code = completion_status;

if (m_on_complete_handler != nullptr)
{
Expand Down

0 comments on commit 9ee7166

Please sign in to comment.