Skip to content

Commit

Permalink
bug fix: atomic cannot work with time_point
Browse files Browse the repository at this point in the history
  • Loading branch information
sewenew committed Aug 2, 2021
1 parent 3fd8535 commit 1b9d62b
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/sw/redis++/async_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ AsyncConnection::AsyncConnection(const ConnectionOptions &opts,
_opts(opts),
_loop(loop),
_create_time(std::chrono::steady_clock::now()),
_last_active(std::chrono::steady_clock::now()) {
_last_active(std::chrono::steady_clock::now().time_since_epoch()) {
assert(_loop != nullptr);

switch (mode) {
Expand Down
8 changes: 5 additions & 3 deletions src/sw/redis++/async_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class AsyncConnection : public std::enable_shared_from_this<AsyncConnection> {
}

auto last_active() const
-> std::chrono::time_point<std::chrono::steady_clock> {
-> std::chrono::steady_clock::duration {
return _last_active;
}

Expand Down Expand Up @@ -128,7 +128,7 @@ class AsyncConnection : public std::enable_shared_from_this<AsyncConnection> {
redisAsyncContext& _context() {
assert(_ctx != nullptr);

_last_active = std::chrono::steady_clock::now();
_last_active = std::chrono::steady_clock::now().time_since_epoch();

return *_ctx;
}
Expand Down Expand Up @@ -188,7 +188,9 @@ class AsyncConnection : public std::enable_shared_from_this<AsyncConnection> {

// The time that the connection is created or the time that
// the connection is recently used, i.e. `_context()` is called.
std::atomic<std::chrono::time_point<std::chrono::steady_clock>> _last_active{};
// NOTE: `_last_active` is `std::atomic`, and we cannot make it of type time_point,
// since time_point's constructor is non-trival.
std::atomic<std::chrono::steady_clock::duration> _last_active{};

std::vector<std::unique_ptr<AsyncEvent>> _events;

Expand Down
2 changes: 1 addition & 1 deletion src/sw/redis++/async_connection_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ bool AsyncConnectionPool::_need_reconnect(const AsyncConnection &connection,
}

if (connection_idle_time > std::chrono::milliseconds(0)) {
if (now - connection.last_active() > connection_idle_time) {
if (now.time_since_epoch() - connection.last_active() > connection_idle_time) {
return true;
}
}
Expand Down

0 comments on commit 1b9d62b

Please sign in to comment.