gpt4 book ai didi

c++ - 在同一系统上运行的多个 boost::asio ssl 客户端

转载 作者:太空狗 更新时间:2023-10-29 23:02:26 24 4
gpt4 key购买 nike

我有一个简单的 Boost ASIO SSL 客户端,它调用一个 web api。客户端是对 Boost SSL 文档示例的轻微修改。

//http.h
class Http {
public:
static void WebApiCall(...);
}

//http.cpp
void Http::WebApiCall(...) {
try {
// .......
boost::asio::io_service io_service;
tcp::resolver resolver(io_service);
tcp::resolver::query query(serverip, serverport);
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
boost::asio::ssl::context ctx(io_service, boost::asio::ssl::context::tlsv1); // ERROR # 1
// ....
// Setting SSL Context Properties Here
// ....
boost::shared_ptr<boost::asio::ssl::stream<tcp::socket> > ssocket(new boost::asio::ssl::stream<tcp::socket>(io_service, ctx));
boost::asio::ip::tcp::endpoint endpoint = *endpoint_iterator;
ssocket->lowest_layer().connect(endpoint);
boost::system::error_code er;
ssocket->handshake(boost::asio::ssl::stream_base::client,er);

boost::asio::streambuf request;
std::ostream request_stream(&request);
// ....
// Set Headers & Body of HTTP Request here
// ....
size_t written = 0;
written = boost::asio::write(*ssocket, request); // ERROR # 2

// .....
// Read server response
boost::asio::streambuf response;
boost::system::error_code error;
int read_bytes = 0;
std::string TempBuf = "";
std::ostringstream responseStringstream;
std::stringstream response_stream;
while ( boost::asio::read(*ssocket,response,boost::asio::transfer_at_least(1), error)) {
read_bytes = read_bytes + response.size();
responseStringstream << &response;
}
}

// Do some stuff with server response....
// ....
} catch ( const boost::system::system_error &error ) {
// Print the exception ..
}
}
// client.cpp
Http::WebApiCall(<api_to_call>)

您可以看到它是一个简单的 HTTP 客户端,带有一个静态函数,该函数使用 ASIO 实现实际启用 SSL 的 HTTP 客户端。

用例:

1000 个进程正在一台机器上运行此客户端。所有进程都在大约同一时间内定期(例如,每隔一分钟)向一个资源发出 POST 请求。机器是 Ubuntu,我似乎没有内存不足(我有大约 6 GB 的空闲空间)

这个客户端工作完美,但在一个情况下,我必须在我的服务器上模拟一些负载,我已经启动了这个客户端的 1000 个进程,所有进程都在一台机器上,所有进程都使用相同的公共(public)证书调用相同的 API 到相同的服务器,除了每个客户端有自己的 OAuth token ¹。在这种情况下,我遇到两种类型的异常:

错误:

  • 错误 # 2:一些客户端(不是全部)在写入时出错(写入:短读)。从不同的论坛和 Boost 来源看来,服务器正在发送 SSL_Shutdown 导致 ASIO 抛出此错误,根据我的发现,这是正常行为。我的问题是,为什么服务器此时发送 SSL_Shutdown?这与从同一台机器调用同一资源的多个进程有什么关系吗?从 ASIO 文档 ASIO SSL 不是线程安全的,但在这种情况下,我只运行一个线程但运行不同的进程(我相信这是完全安全的),除了上面的代码本身是线程安全的。底层 openssl 的行为是否不稳定?

  • 错误#1:有时在创建 Boost ASIO SSL 上下文时出现异常,简单地说“上下文:ssl 错误”。同样的想法,为什么它会这样?这是否与多个进程有关,在这种情况下 openssl 是否混淆了一切?

我的客户端在过去的一年里作为每台机器一个进程运行完美,我以前从未见过这些错误。任何想法表示赞赏。


¹(只是提到了 OAuth,但我认为这与它没有任何关系)

最佳答案

  • Q: Some clients (NOT ALL) while writing get error (write: short read). From different forums and Boost sources it seems the server is sending SSL_Shutdown causing ASIO to throw this error, which, as per my finding is normal behavior.

    最可能的原因是您将系统推到资源限制之外。例如。客户端或服务器可能会用完文件句柄。例如。在我的 Linux 机器上,打开文件的数量默认限制为 1024:ulimit -a 输出:

    core file size          (blocks, -c) 0
    data seg size (kbytes, -d) unlimited
    scheduling priority (-e) 0
    file size (blocks, -f) unlimited
    pending signals (-i) 256878
    max locked memory (kbytes, -l) unlimited
    max memory size (kbytes, -m) unlimited
    open files (-n) 1024
    pipe size (512 bytes, -p) 8
    POSIX message queues (bytes, -q) 819200
    real-time priority (-r) 95
    stack size (kbytes, -s) 8192
    cpu time (seconds, -t) unlimited
    max user processes (-u) 256878
    virtual memory (kbytes, -v) unlimited
    file locks (-x) unlimited
  • Q: My question is, why server is sending SSL_Shutdown at this point?

    很可能是因为上述原因。

  • Q: Does this have to do anything with multiple processes calling the same resource from same machine?

    没有。

  • Q: From ASIO docs ASIO SSL is not thread safe, but in this case I am running only one thread but different processes (which I believe is perfectly safe), besides above code is itself thread safe. Is underlying openssl behaving erratically?

    线程安全或底层 SSL 库不是这里的问题。

  • Q: Sometimes get an exception while creating Boost ASIO SSL Context, simply saying "context: ssl error". Again same thoughts, why it behaves like this? Does this has something to do with multiple processes, is openssl mixing things up in this scenario?

    可能 ssl::context 的每个实例都会产生开销,这不太可能。您可以尝试静态分配它/在循环之外分配它。

    也就是说,如果 SSL 上下文只是遇到(相同的)资源限制,则更有可能进行初始化,因为它可能会打开一些系统配置文件和/或检查是否存在众所周知的路径(例如 CApath等等)

关于c++ - 在同一系统上运行的多个 boost::asio ssl 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28577714/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com