Skip to content

Document connection pool #1219 #57

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 1 commit into
base: main
Choose a base branch
from
Open
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
36 changes: 36 additions & 0 deletions advanced-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,42 @@ Preparing and executing a web request needs to be done sequentially, but not sin
To further exploit parallelism and take advantage of reusing `cpr::Session` objects take a look at the asynchronous `cpr::Session` interface (e.g. `cpr::AsyncResponse asyncResponse = session.GetAsync();`).
Internally `cpr::ThreadPool` gets used for this, handling all requests (Ref: [Asynchronous Requests](#asynchronous-requests)).

## Connection Pool

Connection pooling allows multiple HTTP requests to reuse the same underlying TCP connections, improving performance by avoiding connection establishment overhead. This is especially beneficial when making multiple requests to the same host.

{% raw %}
```c++
cpr::ConnectionPool pool;

// Multiple requests can reuse the same connection
cpr::Response r1 = cpr::Get(cpr::Url{"http://httpbin.org/get"}, pool);
cpr::Response r2 = cpr::Get(cpr::Url{"http://httpbin.org/headers"}, pool);
cpr::Response r3 = cpr::Get(cpr::Url{"http://httpbin.org/user-agent"}, pool);
```
{% endraw %}

Connection pools work with asynchronous requests and `Session` objects:

{% raw %}
```c++
cpr::ConnectionPool pool;
std::vector<cpr::AsyncResponse> responses;

for (int i = 0; i < 10; ++i) {
responses.emplace_back(cpr::GetAsync(cpr::Url{"http://httpbin.org/get"}, pool));
}

// Session usage
cpr::Session session;
session.SetUrl(cpr::Url{"http://httpbin.org/post"});
session.SetConnectionPool(pool);
cpr::Response r = session.Post();
```
{% endraw %}

Connection pools are thread-safe and can be shared across your application. They're most effective when reused for multiple requests to the same host.


## HTTP Compression

Expand Down