8000 Add connection pool option (V3) by cleaton · Pull Request #1219 · libcpr/cpr · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add connection pool option (V3) #1219

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

cleaton
Copy link
@cleaton cleaton commented May 11, 2025

Add connection pool option (see #488 & #330)

@COM8
Copy link
Member
COM8 commented May 13, 2025

@cleaton thanks for contributing! I will try to review it over the weekend.

@COM8
Copy link
Member
COM8 commented May 13, 2025

Please also add a MR here for documentation: https://github.com/libcpr/docs

lock->lock();
};

auto unlock_f = +[](__attribute__((unused)) CURL* handle, __attribute__((unused)) curl_lock_data data, void* userptr) {
Copy link
Contributor

Choose a reason for hiding this comment

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

All of the __attribute__((unused)) could be replaced with [[maybe_unused]]?

Copy link
Member

Choose a reason for hiding this comment

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

I prefer just commenting them out.


namespace cpr {
ConnectionPool::ConnectionPool() {
auto* curl_share = curl_share_init();
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
auto* curl_share = curl_share_init();
const CURLSH* curl_share = curl_share_init();

I'm trying to avoid auto when ever possible since I prefer explicit code.

@@ -390,6 +391,13 @@ void Session::SetConnectTimeout(const ConnectTimeout& timeout) {
curl_easy_setopt(curl_->handle, CURLOPT_CONNECTTIMEOUT_MS, timeout.Milliseconds());
}

void Session::SetConnectionPool(const ConnectionPool& pool) {
auto * curl = curl_->handle;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
auto * curl = curl_->handle;
CURL* curl = curl_->handle;

Comment on lines +396 to +398
if (curl) {
pool.SetupHandler(curl);
}
Copy link
Member

Choose a reason for hiding this comment

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

You can expect curl to be valid here. If you do not trust it, use a assert(curl)

Comment on lines +8 to +21
namespace cpr {
class ConnectionPool {
public:
ConnectionPool();
ConnectionPool(const ConnectionPool&) = default;
ConnectionPool& operator=(const ConnectionPool&) = delete;
void SetupHandler(CURL* easy_handler) const;

private:
std::shared_ptr<std::mutex> connection_mutex_;
std::shared_ptr<CURLSH> curl_sh_;
};
} // namespace cpr
#endif
Copy link
Member

Choose a reason for hiding this comment

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

Can I get a bit of inline code documentation here? I know not all the existing code is documented well, but I try to change this with all new additions e.g. #1168

@@ -79,6 +82,18 @@ void AbstractServer::Run() {
server_stop_cv.notify_all();
}

void AbstractServer::AddConnection(int remote_port) {
unique_connections.insert(remote_port);
Copy link
Member

Choose a reason for hiding this comment

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

Should we take care of cleaning them up once the connection is shut down?

Comment on lines +11 to +14
auto lock_f = +[](__attribute__((unused)) CURL* handle, __attribute__((unused)) curl_lock_data data, __attribute__((unused)) curl_lock_access access, void* userptr) {
std::mutex* lock = static_cast<std::mutex*>(userptr);
lock->lock();
};
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
auto lock_f = +[](__attribute__((unused)) CURL* handle, __attribute__((unused)) curl_lock_data data, __attribute__((unused)) curl_lock_access access, void* userptr) {
std::mutex* lock = static_cast<std::mutex*>(userptr);
lock->lock();
};
auto lock_f = +[](CURL* /*handle*/, curl_lock_data /*data*/, curl_lock_access /*access*/, void* userptr) {
std::mutex* lock = static_cast<std::mutex*>(userptr);
lock->lock();
};

I prefer just commenting them out.

lock->lock();
};

auto unlock_f = +[](__attribute__((unused)) CURL* handle, __attribute__((unused)) curl_lock_data data, void* userptr) {
Copy link
Member

Choose a reason for hiding this comment

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

I prefer just commenting them out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0