Skip to content

Commit

Permalink
RestBackend: Do not expire connections that are in use
Browse files Browse the repository at this point in the history
Actually update the "lastUsed" timestamp when accessing a cached
connection. Otherwise the cache is destroyed and recreated in normal
operation, leading to LongPollingIdx=Next requests to fail (as they
were redirected to the latest index before cache reset, which is then
considered a "future" index, as the latest index is reset to 0).
  • Loading branch information
frankosterfeld authored and RalphSteinhagen committed Mar 13, 2024
1 parent 09cc4c9 commit c7236c4
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/majordomo/include/majordomo/RestBackend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ struct Connection {
zmq::Socket requestResponseSocket;
std::string subscriptionKey;

using Timestamp = std::chrono::time_point<std::chrono::system_clock>;
Timestamp lastUsed = std::chrono::system_clock::now();
using Timestamp = std::chrono::time_point<std::chrono::system_clock>;
std::atomic<Timestamp> lastUsed = std::chrono::system_clock::now();

private:
mutable std::shared_mutex _cachedRepliesMutex;
Expand All @@ -235,7 +235,7 @@ struct Connection {
: notificationSubscriptionSocket(std::move(other.notificationSubscriptionSocket))
, requestResponseSocket(std::move(other.requestResponseSocket))
, subscriptionKey(std::move(other.subscriptionKey))
, lastUsed(std::move(other.lastUsed))
, lastUsed(other.lastUsed.load())
, _cachedReplies(std::move(other._cachedReplies))
, _nextPollingIndex(other._nextPollingIndex) {
}
Expand Down Expand Up @@ -408,7 +408,7 @@ class RestBackend : public Mode {
if (connection->referenceCount() != 0) {
continue;
}
if (std::chrono::system_clock::now() - connection->lastUsed > UNUSED_SUBSCRIPTION_EXPIRATION_TIME) {
if (std::chrono::system_clock::now() - connection->lastUsed.load() > UNUSED_SUBSCRIPTION_EXPIRATION_TIME) {
expiredSubscriptions.push_back(subscriptionKey);
}
}
Expand Down Expand Up @@ -823,7 +823,8 @@ struct RestBackend<Mode, VirtualFS, Roles...>::RestWorker {
if (auto it = recycledConnectionForService.find(subscriptionKey); it != recycledConnectionForService.cend()) {
auto *connectionCache = it->second.get();
detail::Connection::KeepAlive keep(connectionCache);
auto connectionCacheLock = connectionCache->readLock();
connectionCache->lastUsed = std::chrono::system_clock::now();
auto connectionCacheLock = connectionCache->readLock();
return CacheInfo{
.firstCachedIndex = connectionCache->nextPollingIndex(connectionCacheLock) - connectionCache->cachedRepliesSize(connectionCacheLock),
.nextPollingIndex = connectionCache->nextPollingIndex(connectionCacheLock),
Expand Down

0 comments on commit c7236c4

Please sign in to comment.