aboutsummaryrefslogtreecommitdiff
path: root/src/commands/command_queue.h
diff options
context:
space:
mode:
authorAlex Vakulenko <avakulenko@google.com>2016-02-01 12:25:21 -0800
committerAlex Vakulenko <avakulenko@google.com>2016-02-02 20:10:35 +0000
commit98d1fee994302f5e2ad7a7b60de2f2d74f35408b (patch)
tree3f8c45d401f5a600af133e047806e8c97dab72d0 /src/commands/command_queue.h
parent6a1ba84d9b295dcc0c8f50b580182a665c4aa8ce (diff)
downloadlibweave-98d1fee994302f5e2ad7a7b60de2f2d74f35408b.tar.gz
Periodicly clean up command queue and remove old processed commands
Do periodic command queue cleanup to reclaim memory from commands that have been in terminal state for certain period of time (5 mins). Change-Id: Ief9cdbf023a222412c296644c9e927c4be000024 Reviewed-on: https://weave-review.googlesource.com/2434 Reviewed-by: Alex Vakulenko <avakulenko@google.com>
Diffstat (limited to 'src/commands/command_queue.h')
-rw-r--r--src/commands/command_queue.h33
1 files changed, 22 insertions, 11 deletions
diff --git a/src/commands/command_queue.h b/src/commands/command_queue.h
index 01839d8..a092c12 100644
--- a/src/commands/command_queue.h
+++ b/src/commands/command_queue.h
@@ -14,8 +14,10 @@
#include <base/callback.h>
#include <base/macros.h>
+#include <base/time/default_clock.h>
#include <base/time/time.h>
#include <weave/device.h>
+#include <weave/provider/task_runner.h>
#include "src/commands/command_instance.h"
@@ -23,7 +25,7 @@ namespace weave {
class CommandQueue final {
public:
- CommandQueue() = default;
+ CommandQueue(provider::TaskRunner* task_runner, base::Clock* clock);
// TODO: Remove AddCommandAddedCallback and AddCommandRemovedCallback.
using CommandCallback = base::Callback<void(Command* command)>;
@@ -64,23 +66,29 @@ class CommandQueue final {
// Removes a command identified by |id| from the queue.
bool Remove(const std::string& id);
- // Removes old commands selected with DelayedRemove.
- void Cleanup();
+ // Removes old commands scheduled by RemoveLater() to be deleted after
+ // |cutoff_time|.
+ void Cleanup(const base::Time& cutoff_time);
- // Overrides CommandQueue::Now() for tests.
- void SetNowForTest(base::Time now);
+ // Schedule a cleanup task to be run after the specified |delay|.
+ void ScheduleCleanup(base::TimeDelta delay);
- // Returns current time.
- base::Time Now() const;
+ // Perform removal of scheduled commands (by calling Cleanup()) and scheduling
+ // another cleanup task if the removal queue is still not empty.
+ void PerformScheduledCleanup();
- // Overridden value to be returned from Now().
- base::Time test_now_;
+ provider::TaskRunner* task_runner_{nullptr};
+ base::Clock* clock_{nullptr};
// ID-to-CommandInstance map.
std::map<std::string, std::shared_ptr<CommandInstance>> map_;
- // Queue of commands to be removed.
- std::queue<std::pair<base::Time, std::string>> remove_queue_;
+ // Queue of commands to be removed, keeps them sorted by the timestamp
+ // (earliest first). This is done to tolerate system clock changes.
+ template <typename T>
+ using InversePriorityQueue =
+ std::priority_queue<T, std::vector<T>, std::greater<T>>;
+ InversePriorityQueue<std::pair<base::Time, std::string>> remove_queue_;
using CallbackList = std::vector<CommandCallback>;
CallbackList on_command_added_;
@@ -88,6 +96,9 @@ class CommandQueue final {
std::map<std::string, Device::CommandHandlerCallback> command_callbacks_;
Device::CommandHandlerCallback default_command_callback_;
+ // WeakPtr factory for controlling the lifetime of command queue cleanup
+ // tasks.
+ base::WeakPtrFactory<CommandQueue> weak_ptr_factory_{this};
DISALLOW_COPY_AND_ASSIGN(CommandQueue);
};