From ebeff20215257acbc6b8c773e030fe9404e06f1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Blin?= <sebastien.blin@savoirfairelinux.com> Date: Wed, 7 Sep 2022 14:40:37 -0400 Subject: [PATCH] dht_proxy_server: fix permanent put When a second permanent put request was received, we didn't call async_wait after changing the expiration, causing push to not be sent the second day and presence to expire on some devices after ~3 days. --- src/dht_proxy_server.cpp | 47 ++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/src/dht_proxy_server.cpp b/src/dht_proxy_server.cpp index 5b1fbb0b..515b72c9 100644 --- a/src/dht_proxy_server.cpp +++ b/src/dht_proxy_server.cpp @@ -760,7 +760,7 @@ DhtProxyServer::getTypeFromString(const std::string& type) { } std::string -DhtProxyServer::getDefaultTopic(PushType type) { +DhtProxyServer::getDefaultTopic(PushType) { return bundleId_; } @@ -1182,9 +1182,8 @@ DhtProxyServer::put(restinio::request_handle_t request, pput.value = value; pput.expiration = timeout; if (not pput.expireTimer) { - auto &ctx = io_context(); // cancel permanent put - pput.expireTimer = std::make_unique<asio::steady_timer>(ctx, timeout); + pput.expireTimer = std::make_unique<asio::steady_timer>(io_context(), timeout); #ifdef OPENDHT_PUSH_NOTIFICATIONS if (not pushToken.empty()) { pput.pushToken = pushToken; @@ -1194,24 +1193,6 @@ DhtProxyServer::put(restinio::request_handle_t request, topic = getDefaultTopic(pput.type); pput.topic = topic; pput.sessionCtx = std::make_shared<PushSessionContext>(sessionId); - // notify push listen expire - auto jsonProvider = [infoHash, clientId, vid, sessionCtx = pput.sessionCtx](){ - Json::Value json; - json["timeout"] = infoHash.toString(); - json["to"] = clientId; - json["vid"] = std::to_string(vid); - std::lock_guard<std::mutex> l(sessionCtx->lock); - json["s"] = sessionCtx->sessionId; - return json; - }; - if (!pput.expireNotifyTimer) - pput.expireNotifyTimer = std::make_unique<asio::steady_timer>(ctx, - timeout - proxy::OP_MARGIN); - else - pput.expireNotifyTimer->expires_at(timeout - proxy::OP_MARGIN); - pput.expireNotifyTimer->async_wait(std::bind( - &DhtProxyServer::handleNotifyPushListenExpire, this, - std::placeholders::_1, pushToken, std::move(jsonProvider), pput.type, pput.topic)); } #endif } else { @@ -1224,12 +1205,32 @@ DhtProxyServer::put(restinio::request_handle_t request, } } pput.expireTimer->expires_at(timeout); - if (pput.expireNotifyTimer) - pput.expireNotifyTimer->expires_at(timeout - proxy::OP_MARGIN); } pput.expireTimer->async_wait(std::bind(&DhtProxyServer::handleCancelPermamentPut, this, std::placeholders::_1, infoHash, vid)); + +#ifdef OPENDHT_PUSH_NOTIFICATIONS + // notify put permanent expiration + auto jsonProvider = [infoHash, clientId, vid, sessionCtx = pput.sessionCtx](){ + Json::Value json; + json["timeout"] = infoHash.toString(); + json["to"] = clientId; + json["vid"] = std::to_string(vid); + std::lock_guard<std::mutex> l(sessionCtx->lock); + json["s"] = sessionCtx->sessionId; + return json; + }; + if (!pput.expireNotifyTimer) + pput.expireNotifyTimer = std::make_unique<asio::steady_timer>(io_context(), + timeout - proxy::OP_MARGIN); + else + pput.expireNotifyTimer->expires_at(timeout - proxy::OP_MARGIN); + pput.expireNotifyTimer->async_wait(std::bind( + &DhtProxyServer::handleNotifyPushListenExpire, this, + std::placeholders::_1, pushToken, std::move(jsonProvider), pput.type, pput.topic)); +#endif } + dht_->put(infoHash, value, [this, request, value](bool ok){ if (ok){ auto response = initHttpResponse(request->create_response()); -- GitLab