-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Timeout after which the asynchronous (background) transfer will inter…
…rupt data transmission during process termination (#106) (#107) * Fixed timeout calculation * Improved synchronization
- Loading branch information
Showing
14 changed files
with
514 additions
and
237 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "CommonUtils.h" | ||
|
||
#include <chrono> | ||
#include <memory> | ||
#include <string> | ||
#include <string_view> | ||
#include <curl/curl.h> | ||
|
||
namespace elasticapm::php::transport { | ||
|
||
using namespace std::literals; | ||
|
||
class HttpEndpoint { | ||
public: | ||
using enpointHeaders_t = std::vector<std::pair<std::string_view, std::string_view>>; | ||
using connectionId_t = std::size_t; | ||
|
||
HttpEndpoint(HttpEndpoint &&) = delete; | ||
HttpEndpoint &operator=(HttpEndpoint &&) = delete; | ||
HttpEndpoint(HttpEndpoint const &) = delete; | ||
HttpEndpoint &operator=(HttpEndpoint const &) = delete; | ||
|
||
HttpEndpoint(std::string endpoint, std::string_view contentType, enpointHeaders_t const &headers, std::size_t maxRetries, std::chrono::milliseconds retryDelay) : endpoint_(std::move(endpoint)), maxRetries_(maxRetries), retryDelay_(retryDelay) { | ||
auto connectionDetails = utils::getConnectionDetailsFromURL(endpoint_); | ||
if (!connectionDetails) { | ||
std::string msg = "Unable to parse connection details from endpoint: "s; | ||
msg.append(endpoint_); | ||
throw std::runtime_error(msg); | ||
} | ||
connectionId_ = std::hash<std::string>{}(connectionDetails.value()); | ||
|
||
fillCurlHeaders(contentType, headers); | ||
} | ||
|
||
~HttpEndpoint() { | ||
if (curlHeaders_) { | ||
curl_slist_free_all(curlHeaders_); | ||
curlHeaders_ = nullptr; | ||
} | ||
} | ||
|
||
std::string const &getEndpoint() const { | ||
return endpoint_; | ||
} | ||
|
||
struct curl_slist *getHeaders() { | ||
return curlHeaders_; | ||
} | ||
|
||
connectionId_t getConnectionId() const { | ||
return connectionId_; | ||
} | ||
|
||
std::size_t getMaxRetries() const { | ||
return maxRetries_; | ||
} | ||
|
||
std::chrono::milliseconds getRetryDelay() const { | ||
return retryDelay_; | ||
} | ||
|
||
private: | ||
void fillCurlHeaders(std::string_view contentType, enpointHeaders_t const &headers) { | ||
if (!contentType.empty()) { | ||
std::string cType = "Content-Type: "s; | ||
cType.append(contentType); | ||
curlHeaders_ = curl_slist_append(curlHeaders_, cType.c_str()); | ||
} | ||
|
||
for (auto const &hdr : headers) { | ||
std::string header; | ||
header.append(hdr.first); | ||
header.append(": "sv); | ||
header.append(hdr.second); | ||
curlHeaders_ = curl_slist_append(curlHeaders_, header.c_str()); | ||
} | ||
} | ||
|
||
std::string endpoint_; | ||
std::size_t maxRetries_ = 1; | ||
std::chrono::milliseconds retryDelay_ = 0ms; | ||
connectionId_t connectionId_; | ||
struct curl_slist *curlHeaders_ = nullptr; | ||
}; | ||
|
||
} // namespace elasticapm::php::transport |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "CommonUtils.h" | ||
#include "CurlSender.h" | ||
#include "HttpEndpoint.h" | ||
#include "LoggerInterface.h" | ||
|
||
#include <chrono> | ||
#include <memory> | ||
#include <string> | ||
#include <string_view> | ||
#include <unordered_map> | ||
|
||
namespace elasticapm::php::transport { | ||
|
||
using namespace std::literals; | ||
|
||
class HttpEndpoints { | ||
public: | ||
using endpointUrlHash_t = std::size_t; | ||
|
||
HttpEndpoints(std::shared_ptr<LoggerInterface> log) : log_(log) { | ||
} | ||
|
||
bool add(std::string endpointUrl, size_t endpointHash, bool verifyServerCertificate, std::string contentType, HttpEndpoint::enpointHeaders_t const &endpointHeaders, std::chrono::milliseconds timeout, std::size_t maxRetries, std::chrono::milliseconds retryDelay) { | ||
std::lock_guard<std::mutex> lock(mutex_); | ||
auto result = endpoints_.try_emplace(endpointHash, std::move(endpointUrl), std::move(contentType), endpointHeaders, maxRetries, retryDelay); | ||
if (connections_.try_emplace(result.first->second.getConnectionId(), log_, timeout, verifyServerCertificate).second) { // CurlSender | ||
ELOG_DEBUG(log_, "HttpEndpoints::add endpointUrl '%s' enpointHash: %X initialize new connectionId: %X", result.first->second.getEndpoint().c_str(), endpointHash, result.first->second.getConnectionId()); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
std::tuple<std::string, curl_slist *, HttpEndpoint::connectionId_t, CurlSender &, std::size_t, std::chrono::milliseconds> getConnection(size_t endpointHash) { | ||
std::lock_guard<std::mutex> lock(mutex_); | ||
auto const &endpoint = endpoints_.find(endpointHash); | ||
if (endpoint == std::end(endpoints_)) { | ||
std::stringstream stream; | ||
stream << "HttpEnpoints missing enpointHash:" << std::hex << endpointHash; | ||
throw std::runtime_error(stream.str()); | ||
} | ||
|
||
auto const &connection = connections_.find(endpoint->second.getConnectionId()); | ||
if (connection == std::end(connections_)) { | ||
std::stringstream stream; | ||
stream << "HttpEndpoints enpointHash:" << std::hex << endpointHash << " missing connectionId " << std::hex << endpoint->second.getConnectionId(); | ||
throw std::runtime_error(stream.str()); | ||
} | ||
|
||
auto &conn = connection->second; | ||
auto maxRetries = std::max(static_cast<std::size_t>(1), static_cast<std::size_t>(endpoint->second.getMaxRetries())); | ||
auto retryDelay = endpoint->second.getRetryDelay(); | ||
|
||
return {endpoint->second.getEndpoint(), endpoint->second.getHeaders(), endpoint->second.getConnectionId(), conn, maxRetries, retryDelay}; | ||
} | ||
|
||
protected: | ||
std::shared_ptr<LoggerInterface> log_; | ||
std::mutex mutex_; | ||
std::unordered_map<endpointUrlHash_t, HttpEndpoint> endpoints_; | ||
std::unordered_map<HttpEndpoint::connectionId_t, CurlSender> connections_; | ||
}; | ||
|
||
} // namespace elasticapm::php::transport |
Oops, something went wrong.