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

Support connection pool invalidation #256

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions include/ozo/connection_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,13 @@ class connection_pool {
return connection_provider(*this, io);
}

/**
* Invalidate all contained connections. It will prevent all available and currently used connections to be reused.
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you please to add a use-case description for this function?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I can think of something like: close all connections when it's considered that all of them are invalid due to some user defined heuristics but the only way to truly check it is to wait for each connection until request is timed out.

Copy link
Contributor

Choose a reason for hiding this comment

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

Well, it's maybe a good point. E.g. invalidate all connections if get some wrong OID error or something like this. I mean, the feature should have a particular use case or nobody will use it. Another use case - invalidate all the connections by a signal like a log rotation.

*/
void invalidate() {
impl_.invalidate();
}

private:

auto queue_timeout(time_traits::time_point at) const {
Expand Down
89 changes: 89 additions & 0 deletions tests/integration/connection_pool_integration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,93 @@ TEST(connection_pool_integration, should_serve_concurrent_requests) {
EXPECT_THAT(results, Each(results.front()));
}

TEST(connection_pool_integration, invalidate_should_prevent_to_reuse_any_available_connection) {
using namespace ozo::literals;
using namespace std::chrono_literals;

ozo::io_context io;
ozo::connection_info conn_info(OZO_PG_TEST_CONNINFO);
ozo::connection_pool_config config;
config.capacity = 1;
config.queue_capacity = 0;
ozo::connection_pool pool(conn_info, config, !ozo::thread_safe);

asio::spawn(io, [&] (asio::yield_context yield) {
const auto get_pg_backend_pid = [&] (int& pg_backend_pid) {
ozo::rows_of<int> result;
ozo::error_code ec;
const auto conn = ozo::request(
pool[io],
"SELECT pg_backend_pid()"_SQL,
ozo::deadline(1s),
ozo::into(result),
yield[ec]
);

ASSERT_FALSE(ec) << ec.message();
ASSERT_FALSE(ozo::is_null_recursive(conn));
ASSERT_EQ(1u, result.size());

pg_backend_pid = std::get<0>(result[0]);
};

int pg_backend_pid1 = 0;
get_pg_backend_pid(pg_backend_pid1);

pool.invalidate();

int pg_backend_pid2 = 0;
get_pg_backend_pid(pg_backend_pid2);

ASSERT_NE(pg_backend_pid1, 0);
EXPECT_NE(pg_backend_pid1, pg_backend_pid2);
});

io.run();
}

TEST(connection_pool_integration, invalidate_should_prevent_to_reuse_any_used_connection) {
using namespace ozo::literals;
using namespace std::chrono_literals;

ozo::io_context io;
ozo::connection_info conn_info(OZO_PG_TEST_CONNINFO);
ozo::connection_pool_config config;
config.capacity = 1;
config.queue_capacity = 0;
ozo::connection_pool pool(conn_info, config, !ozo::thread_safe);

asio::spawn(io, [&] (asio::yield_context yield) {
const auto get_pg_backend_pid = [&] (int& pg_backend_pid) {
ozo::rows_of<int> result;
ozo::error_code ec;
const auto conn = ozo::request(
pool[io],
"SELECT pg_backend_pid()"_SQL,
ozo::deadline(1s),
ozo::into(result),
yield[ec]
);

ASSERT_FALSE(ec) << ec.message();
ASSERT_FALSE(ozo::is_null_recursive(conn));
ASSERT_EQ(1u, result.size());

pool.invalidate();

pg_backend_pid = std::get<0>(result[0]);
};

int pg_backend_pid1 = 0;
get_pg_backend_pid(pg_backend_pid1);
int pg_backend_pid2 = 0;
get_pg_backend_pid(pg_backend_pid2);

ASSERT_NE(pg_backend_pid1, 0);
EXPECT_NE(pg_backend_pid1, pg_backend_pid2);
});

io.run();
}

} // namespace