Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(804)

Unified Diff: media/base/android/media_service_throttler.h

Issue 2471903002: Add MediaServiceThrottler (Closed)
Patch Set: Limit crashes from watchdog creation failures Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: media/base/android/media_service_throttler.h
diff --git a/media/base/android/media_service_throttler.h b/media/base/android/media_service_throttler.h
new file mode 100644
index 0000000000000000000000000000000000000000..71c58fce8b1f82e6bfe384833ea5e424d819835c
--- /dev/null
+++ b/media/base/android/media_service_throttler.h
@@ -0,0 +1,98 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef MEDIA_BASE_ANDROID_MEDIA_SERVICE_THROTTLER_H_
+#define MEDIA_BASE_ANDROID_MEDIA_SERVICE_THROTTLER_H_
+
+#include <memory>
+
+#include "base/callback_forward.h"
+#include "base/cancelable_callback.h"
+#include "base/lazy_instance.h"
+#include "base/macros.h"
+#include "base/single_thread_task_runner.h"
+#include "base/time/tick_clock.h"
+#include "base/time/time.h"
+#include "media/base/media_export.h"
+
+namespace media {
+class MediaServerCrashListener;
+
+class MEDIA_EXPORT MediaServiceThrottler {
DaleCurtis 2016/11/15 00:49:32 Class docs?
tguilbert 2016/11/15 19:29:07 Done.
+ public:
+ // Called to get the singleton MediaServiceThrottler instance.
+ // The first thread on which GetInstance() is called is the thread on which
+ // calls to OnMediaServerCrash() will be signaled.
+ static MediaServiceThrottler* GetInstance();
+
+ // Returns the delay to wait until a new client is allowed to be created.
+ base::TimeDelta GetDelayForClientCreation();
+
+ // Test only methods.
+ void SetTickClockForTesting(base::TickClock* clock);
+ void ResetInternalStateForTesting();
+ base::TimeDelta GetBaseThrottlingRateForTesting();
+ bool IsCrashListenerAliveForTesting();
+ void SetCrashListenerTaskRunnerForTesting(
+ scoped_refptr<base::SingleThreadTaskRunner> crash_listener_task_runner);
+
+ private:
+ friend struct base::DefaultLazyInstanceTraits<MediaServiceThrottler>;
+ friend class MediaServiceThrottlerTest;
+
+ MediaServiceThrottler();
+ virtual ~MediaServiceThrottler();
+
+ // Called by the |crash_listener_| whenever a crash is detected.
+ void OnMediaServerCrash();
+
+ // Updates |current_craches_| according to a linear decay function.
+ void UpdateServerCrashes();
+
+ // Ensures that the MediaServerCrashListener was properly started (can lead
+ // to OnMediaServerCrash() being called in the case it hasn't).
+ void EnsureCrashListenerStarted();
+
+ // Frees up the resources used by |crash_listener_|;
+ void ReleaseCrashListener();
+
+ // Gets the delay for ScheduleClientCreation(), which grows exponentially
+ // based on |current_crashes_|.
+ base::TimeDelta GetThrottlingDelayFromServerCrashes();
+
+ std::unique_ptr<base::TickClock> clock_;
+
+ // Effective number of media server crashes.
+ // NOTE: This is of type double because we decay the number of crashes at a
+ // rate of one per minute (e.g. 30s after a single crash, |curren_crashes_|
+ // should be equal to 0.5).
+ double current_crashes_;
+
+ // Next time at which a client creation can be scheduled.
+ base::TimeTicks next_schedulable_slot_;
+
+ // Last media server crash time.
+ base::TimeTicks last_server_crash_;
+
+ // Last time UpdateServerCrashes() was called.
+ base::TimeTicks last_current_crash_update_time_;
+
+ // Last time ScheduleClientCreation() was called.
+ base::TimeTicks last_schedule_call_;
+
+ // Callbacks used to release |crash_listener_| after 60s of inactivity.
+ base::Closure release_crash_listener_cb_;
+ base::CancelableClosure cancelable_release_crash_listener_cb_;
+
+ // Listener that verifies
+ std::unique_ptr<MediaServerCrashListener> crash_listener_;
+
+ scoped_refptr<base::SingleThreadTaskRunner> crash_listener_task_runner_;
+
+ DISALLOW_COPY_AND_ASSIGN(MediaServiceThrottler);
+};
+
+} // namespace media
+
+#endif // MEDIA_BASE_ANDROID_MEDIA_SERVICE_THROTTLER_H_

Powered by Google App Engine
This is Rietveld 408576698