Commit 909de115 authored by Charles Zhao's avatar Charles Zhao Committed by Commit Bot

hindsight: add some safeguards.

If for some reason, lots of actions are generated at very short period
of time, we need to make sure:

(1) We don't use too much memory.
(2) We don't use too much disk.

Bug: 1012936
Change-Id: I00e9d79c3a45dc3bdbe682bc2e9aae532bc00862
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1866091Reviewed-by: default avatarTony Yeoman <tby@chromium.org>
Reviewed-by: default avatarCharles . <charleszhao@chromium.org>
Commit-Queue: Charles . <charleszhao@chromium.org>
Cr-Commit-Position: refs/heads/master@{#706766}
parent 513ca106
......@@ -21,6 +21,12 @@ namespace app_list {
namespace {
constexpr int kSecondsPerDay = 86400;
// If |CrOSActionRecorder::actions_| gets longer than this, force a Flush to
// disk.
constexpr int kActionLimitInMemory = 3600;
// If current file already contains more record than this, skip the rest for
// that day.
constexpr int kActionLimitPerFile = 100000;
enum CrOSActionRecorderType {
kDefault = 0,
......@@ -39,6 +45,9 @@ void SaveToDiskOnWorkerThread(const CrOSActionHistoryProto actions,
if (!actions_to_write.ParseFromString(proto_str))
actions_to_write.Clear();
if (actions_to_write.actions_size() > kActionLimitPerFile)
return;
actions_to_write.MergeFrom(actions);
const std::string proto_str_to_write = actions_to_write.SerializeAsString();
......@@ -112,8 +121,12 @@ void CrOSActionRecorder::RecordAction(
void CrOSActionRecorder::MaybeFlushToDisk() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (actions_.actions().empty())
return;
const base::Time now = base::Time::Now();
if (now - last_save_timestamp_ >= kSaveInternal) {
if (now - last_save_timestamp_ >= kSaveInternal ||
actions_.actions_size() > kActionLimitInMemory) {
last_save_timestamp_ = now;
// Writes the predictor proto to disk asynchronously.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment