Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[backport] feat: support set TOS in curl #2113

Merged
merged 1 commit into from
Feb 27, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion core/sdk/CurlImp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,19 @@
// limitations under the License.

#include "CurlImp.h"

#include <curl/curl.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <sys/socket.h>

#include "Exception.h"
#include "CurlAsynInstance.h"
#include "DNSCache.h"
#include "app_config/AppConfig.h"
#include <curl/curl.h>
#include "common/Flags.h"

DEFINE_FLAG_INT32(sls_request_dscp, "set dscp for sls request, from 0 to 63", -1);

using namespace std;

Expand Down Expand Up @@ -73,6 +81,13 @@ namespace sdk {
return sizes;
}

static size_t socket_write_callback(void* socketData, curl_socket_t fd, curlsocktype purpose) {
// TOS 8 bits: first 6 bits are DSCP (user customized), last 2 bits are ECN (auto set by OS)
int32_t tos = *static_cast<int32_t*>(socketData) << 2;
setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
return 0;
}

CURL* PackCurlRequest(const std::string& httpMethod,
const std::string& host,
const int32_t port,
Expand Down Expand Up @@ -134,6 +149,13 @@ namespace sdk {
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, data_write_callback);
curl_easy_setopt(curl, CURLOPT_HEADERDATA, &(httpMessage.header));
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header_write_callback);

static int sDSCP
= INT32_FLAG(sls_request_dscp); // the lifestyle of sDSCP pointer should be longer than local variable
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

虽然是实现不一样,但是使用方式得保持一致吧,后续得能升级到LoongCollector,这个要重点测试下兼容性

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

使用是一样的,都是配置gflag

if (sDSCP >= 0 && sDSCP <= 63) {
curl_easy_setopt(curl, CURLOPT_SOCKOPTDATA, &sDSCP);
curl_easy_setopt(curl, CURLOPT_SOCKOPTFUNCTION, socket_write_callback);
}
return curl;
}

Expand Down
Loading