blob: 662390c6de2c9118205072d96e82ac5bda1399e6 [file] [edit]
#ifndef PRODUCTION_BORG_MGMT_NODE_PROXY_SAFEPOWER_SAFEPOWER_AGENT_ACTIVITY_WATCHER_H_
#define PRODUCTION_BORG_MGMT_NODE_PROXY_SAFEPOWER_SAFEPOWER_AGENT_ACTIVITY_WATCHER_H_
#include <memory>
#include <utility>
#include "callback_manager.h"
#include "state_updater.h"
#include "absl/base/nullability.h"
#include "absl/base/thread_annotations.h"
#include "absl/functional/any_invocable.h"
#include "absl/synchronization/mutex.h"
namespace safepower_agent {
// Watches `StateUpdater<StateProto>` for transitions between active (at least
// one active listener) and idle (no active listeners), invoking `on_change`
// with `true` when active and `false` when idle.
//
// TODO(b/581544360): Remove ActivityWatcher once state updates and streaming
// RPCs are routed through ServiceImpl's event loop (Milestone 3).
template <typename StateProto>
class ActivityWatcher {
public:
ActivityWatcher(
std::shared_ptr<StateUpdater<StateProto>> state_updater,
absl::AnyInvocable<void(bool)> on_change)
: state_updater_(std::move(state_updater)),
on_change_(std::move(on_change)) {
// StateUpdater starts with 0 listeners (idle), so we arm OnActive to detect
// when the first streaming client arrives. If listeners already exist,
// StateUpdater::OnActive invokes the callback immediately.
ArmOnActive();
}
~ActivityWatcher() { Stop(); }
ActivityWatcher(const ActivityWatcher&) = delete;
ActivityWatcher& operator=(const ActivityWatcher&) = delete;
void Stop() ABSL_LOCKS_EXCLUDED(mutex_) {
CallbackManager::Handle active_handle;
CallbackManager::Handle idle_handle;
{
absl::MutexLock lock(mutex_);
if (stopping_) {
return;
}
stopping_ = true;
active_handle_.TryCancel();
idle_handle_.TryCancel();
active_handle = std::move(active_handle_);
idle_handle = std::move(idle_handle_);
}
active_handle.Wait();
idle_handle.Wait();
}
private:
void ArmOnActive() ABSL_LOCKS_EXCLUDED(mutex_) {
{
absl::MutexLock lock(mutex_);
if (stopping_) {
return;
}
}
// TODO(b/581544360): Re-entrant callback re-arming has known races, but
// ActivityWatcher is an interim bridge that will be removed when state
// updates move to the event loop.
CallbackManager::Handle handle = state_updater_->OnActive([this] {
on_change_(true);
ArmOnIdle();
});
{
absl::MutexLock lock(mutex_);
if (!stopping_) {
active_handle_ = std::move(handle);
return;
}
}
handle.TryCancel();
handle.Wait();
}
void ArmOnIdle() ABSL_LOCKS_EXCLUDED(mutex_) {
{
absl::MutexLock lock(mutex_);
if (stopping_) {
return;
}
}
CallbackManager::Handle handle = state_updater_->OnIdle([this] {
on_change_(false);
ArmOnActive();
});
{
absl::MutexLock lock(mutex_);
if (!stopping_) {
idle_handle_ = std::move(handle);
return;
}
}
handle.TryCancel();
handle.Wait();
}
std::shared_ptr<StateUpdater<StateProto>> state_updater_;
absl::AnyInvocable<void(bool)> on_change_;
mutable absl::Mutex mutex_;
bool stopping_ ABSL_GUARDED_BY(mutex_) = false;
CallbackManager::Handle active_handle_ ABSL_GUARDED_BY(mutex_);
CallbackManager::Handle idle_handle_ ABSL_GUARDED_BY(mutex_);
};
} // namespace safepower_agent
#endif // PRODUCTION_BORG_MGMT_NODE_PROXY_SAFEPOWER_SAFEPOWER_AGENT_ACTIVITY_WATCHER_H_