summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAshod Nakashian <ashod.nakashian@collabora.co.uk>2020-12-06 16:55:22 -0500
committerMichael Meeks <michael.meeks@collabora.com>2020-12-08 09:26:41 +0000
commit4cd460e239c03c74c4633ff819b9e6b33eb7f06c (patch)
tree4898edffd7263287b8b793487d8adfa1f5ab5b63
parentwsd: convert limitLifeSeconds from int to chrono (diff)
downloadonline-4cd460e239c03c74c4633ff819b9e6b33eb7f06c.tar.gz
online-4cd460e239c03c74c4633ff819b9e6b33eb7f06c.zip
wsd: avoid chrono::duration<double>
While chrono supports double as a datatype, it is opaque and doesn't lend itself to any obvious units of time (presumably seconds). Using chrono::milliseconds is much more readable and also safe when converting from seconds or any other units. Ultimately, we typically convert to milliseconds anyway, mostly for logging. There is but one exception where we convert in seconds, and now that case is documented. Change-Id: Ide98f45f2ad8da8225d41ae870bbc4bc09a2a0b5 Signed-off-by: Ashod Nakashian <ashod.nakashian@collabora.co.uk>
-rw-r--r--wsd/DocumentBroker.cpp11
-rw-r--r--wsd/Storage.cpp31
-rw-r--r--wsd/Storage.hpp18
3 files changed, 32 insertions, 28 deletions
diff --git a/wsd/DocumentBroker.cpp b/wsd/DocumentBroker.cpp
index b9bfdc950b..2f323c4375 100644
--- a/wsd/DocumentBroker.cpp
+++ b/wsd/DocumentBroker.cpp
@@ -623,7 +623,7 @@ bool DocumentBroker::load(const std::shared_ptr<ClientSession>& session, const s
std::string templateSource;
#if !MOBILEAPP
- std::chrono::duration<double> getInfoCallDuration(0);
+ std::chrono::milliseconds checkFileInfoCallDurationMs;
WopiStorage* wopiStorage = dynamic_cast<WopiStorage*>(_storage.get());
if (wopiStorage != nullptr)
{
@@ -712,7 +712,7 @@ bool DocumentBroker::load(const std::shared_ptr<ClientSession>& session, const s
session->setDocumentOwner(true);
}
- getInfoCallDuration = wopifileinfo->getCallDuration();
+ checkFileInfoCallDurationMs = wopifileinfo->getCallDurationMs();
// Pass the ownership to client session
session->setWopiFileInfo(wopifileinfo);
@@ -917,11 +917,10 @@ bool DocumentBroker::load(const std::shared_ptr<ClientSession>& session, const s
if (wopiStorage != nullptr)
{
// Get the time taken to load the file from storage
- auto callDuration = wopiStorage->getWopiLoadDuration();
// Add the time taken to check file info
- callDuration += getInfoCallDuration;
- _wopiLoadDuration = std::chrono::duration_cast<std::chrono::milliseconds>(callDuration);
- const std::string msg = "stats: wopiloadduration " + std::to_string(callDuration.count());
+ _wopiLoadDuration = wopiStorage->getWopiLoadDuration() + checkFileInfoCallDurationMs;
+ const std::string msg
+ = "stats: wopiloadduration " + std::to_string(_wopiLoadDuration.count() / 1000.); // In seconds.
LOG_TRC("Sending to Client [" << msg << "].");
session->sendTextFrame(msg);
}
diff --git a/wsd/Storage.cpp b/wsd/Storage.cpp
index e44e13cfc3..5d41823df3 100644
--- a/wsd/Storage.cpp
+++ b/wsd/Storage.cpp
@@ -5,6 +5,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
+#include <chrono>
#include <config.h>
#include "Storage.hpp"
@@ -595,7 +596,7 @@ std::unique_ptr<WopiStorage::WOPIFileInfo> WopiStorage::getWOPIFileInfo(const Au
LOG_DBG("Getting info for wopi uri [" << uriAnonym << "].");
std::string wopiResponse;
- std::chrono::duration<double> callDuration(0);
+ std::chrono::milliseconds callDurationMs;
try
{
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET,
@@ -622,7 +623,8 @@ std::unique_ptr<WopiStorage::WOPIFileInfo> WopiStorage::getWOPIFileInfo(const Au
Poco::Net::HTTPResponse response;
std::istream& rs = psession->receiveResponse(response);
- callDuration = (std::chrono::steady_clock::now() - startTime);
+ callDurationMs = std::chrono::duration_cast<std::chrono::milliseconds>(
+ std::chrono::steady_clock::now() - startTime);
Log::StreamLogger logRes = Log::trace();
if (logRes.enabled())
@@ -660,9 +662,9 @@ std::unique_ptr<WopiStorage::WOPIFileInfo> WopiStorage::getWOPIFileInfo(const Au
if (JsonUtil::parseJSON(wopiResponse, object))
{
if (LOOLWSD::AnonymizeUserData)
- LOG_DBG("WOPI::CheckFileInfo (" << callDuration.count() * 1000. << " ms): anonymizing...");
+ LOG_DBG("WOPI::CheckFileInfo (" << callDurationMs.count() << " ms): anonymizing...");
else
- LOG_DBG("WOPI::CheckFileInfo (" << callDuration.count() * 1000. << " ms): " << wopiResponse);
+ LOG_DBG("WOPI::CheckFileInfo (" << callDurationMs.count() << " ms): " << wopiResponse);
std::size_t size = 0;
std::string filename, ownerId, lastModifiedTime;
@@ -679,7 +681,7 @@ std::unique_ptr<WopiStorage::WOPIFileInfo> WopiStorage::getWOPIFileInfo(const Au
if (LOOLWSD::AnonymizeUserData)
Util::mapAnonymized(Util::getFilenameFromURL(filename), Util::getFilenameFromURL(getUri().toString()));
- auto wopiInfo = std::unique_ptr<WopiStorage::WOPIFileInfo>(new WOPIFileInfo(fileInfo, callDuration, object));
+ auto wopiInfo = Util::make_unique<WopiStorage::WOPIFileInfo>(fileInfo, callDurationMs, object);
if (wopiInfo->getSupportsLocks())
lockCtx.initSupportsLocks();
@@ -690,9 +692,11 @@ std::unique_ptr<WopiStorage::WOPIFileInfo> WopiStorage::getWOPIFileInfo(const Au
if (LOOLWSD::AnonymizeUserData)
wopiResponse = "obfuscated";
- LOG_ERR("WOPI::CheckFileInfo (" << callDuration.count() * 1000. <<
- " ms) failed or no valid JSON payload returned. Access denied. "
- "Original response: [" << wopiResponse << "].");
+ LOG_ERR("WOPI::CheckFileInfo ("
+ << callDurationMs.count()
+ << " ms) failed or no valid JSON payload returned. Access denied. "
+ "Original response: ["
+ << wopiResponse << "].");
throw UnauthorizedRequestException("Access denied. WOPI::CheckFileInfo failed on: " + uriAnonym);
}
@@ -723,7 +727,7 @@ void WopiStorage::WOPIFileInfo::init()
}
WopiStorage::WOPIFileInfo::WOPIFileInfo(const FileInfo &fileInfo,
- std::chrono::duration<double> callDuration,
+ std::chrono::milliseconds callDurationMs,
Poco::JSON::Object::Ptr &object)
{
init();
@@ -768,7 +772,7 @@ WopiStorage::WOPIFileInfo::WOPIFileInfo(const FileInfo &fileInfo,
else
object->stringify(wopiResponse);
- LOG_DBG("WOPI::CheckFileInfo (" << callDuration.count() * 1000. << " ms): " << wopiResponse.str());
+ LOG_DBG("WOPI::CheckFileInfo (" << callDurationMs.count() << " ms): " << wopiResponse.str());
JsonUtil::findJSONValue(object, "UserExtraInfo", _userExtraInfo);
JsonUtil::findJSONValue(object, "WatermarkText", _watermarkText);
@@ -922,7 +926,9 @@ std::string WopiStorage::downloadStorageFileToLocal(const Authorization& auth,
Poco::Net::HTTPResponse response;
std::istream& rs = psession->receiveResponse(response);
- const std::chrono::duration<double> diff = (std::chrono::steady_clock::now() - startTime);
+ const std::chrono::milliseconds diff
+ = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now()
+ - startTime);
_wopiLoadDuration += diff;
Log::StreamLogger logger = Log::trace();
@@ -1098,7 +1104,8 @@ WopiStorage::uploadLocalFileToStorage(const Authorization& auth, const std::stri
Poco::Net::HTTPResponse response;
std::istream& rs = psession->receiveResponse(response);
- _wopiSaveDuration = std::chrono::steady_clock::now() - startTime;
+ _wopiSaveDuration = std::chrono::duration_cast<std::chrono::milliseconds>(
+ std::chrono::steady_clock::now() - startTime);
WopiUploadDetails details
= { filePathAnonym, uriAnonym, response.getReason(), response.getStatus(), size,
diff --git a/wsd/Storage.hpp b/wsd/Storage.hpp
index 3c9ca71119..00e8dfa30e 100644
--- a/wsd/Storage.hpp
+++ b/wsd/Storage.hpp
@@ -374,8 +374,6 @@ public:
const std::string& localStorePath,
const std::string& jailPath) :
StorageBase(uri, localStorePath, jailPath),
- _wopiLoadDuration(0),
- _wopiSaveDuration(0),
_reuseCookies(false)
{
const auto& app = Poco::Util::Application::instance();
@@ -398,8 +396,8 @@ public:
};
/// warning - removes items from object.
- WOPIFileInfo(const FileInfo &fileInfo, std::chrono::duration<double> callDuration,
- Poco::JSON::Object::Ptr &object);
+ WOPIFileInfo(const FileInfo& fileInfo, std::chrono::milliseconds callDurationMs,
+ Poco::JSON::Object::Ptr& object);
const std::string& getUserId() const { return _userId; }
const std::string& getUsername() const { return _username; }
@@ -432,7 +430,7 @@ public:
TriState getDisableChangeTrackingShow() const { return _disableChangeTrackingShow; }
TriState getDisableChangeTrackingRecord() const { return _disableChangeTrackingRecord; }
TriState getHideChangeTrackingControls() const { return _hideChangeTrackingControls; }
- std::chrono::duration<double> getCallDuration() const { return _callDuration; }
+ std::chrono::milliseconds getCallDurationMs() const { return _callDurationMs; }
private:
/// User id of the user accessing the file
std::string _userId;
@@ -496,7 +494,7 @@ public:
bool _userCanRename;
/// Time it took to call WOPI's CheckFileInfo
- std::chrono::duration<double> _callDuration;
+ std::chrono::milliseconds _callDurationMs;
};
/// Returns the response of CheckFileInfo WOPI call for URI that was
@@ -522,8 +520,8 @@ public:
const bool isRename) override;
/// Total time taken for making WOPI calls during load
- std::chrono::duration<double> getWopiLoadDuration() const { return _wopiLoadDuration; }
- std::chrono::duration<double> getWopiSaveDuration() const { return _wopiSaveDuration; }
+ std::chrono::milliseconds getWopiLoadDuration() const { return _wopiLoadDuration; }
+ std::chrono::milliseconds getWopiSaveDuration() const { return _wopiSaveDuration; }
protected:
struct WopiUploadDetails
@@ -549,8 +547,8 @@ private:
private:
// Time spend in loading the file from storage
- std::chrono::duration<double> _wopiLoadDuration;
- std::chrono::duration<double> _wopiSaveDuration;
+ std::chrono::milliseconds _wopiLoadDuration;
+ std::chrono::milliseconds _wopiSaveDuration;
/// Whether or not to re-use cookies from the browser for the WOPI requests.
bool _reuseCookies;
};