Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef MEDIA_BASE_ANDROID_MEDIA_SERVICE_THROTTLER_H_ | |
| 6 #define MEDIA_BASE_ANDROID_MEDIA_SERVICE_THROTTLER_H_ | |
| 7 | |
| 8 #include <memory> | |
| 9 | |
| 10 #include "base/callback_forward.h" | |
| 11 #include "base/cancelable_callback.h" | |
| 12 #include "base/lazy_instance.h" | |
| 13 #include "base/macros.h" | |
| 14 #include "base/single_thread_task_runner.h" | |
| 15 #include "base/time/tick_clock.h" | |
| 16 #include "base/time/time.h" | |
| 17 #include "media/base/media_export.h" | |
| 18 | |
| 19 namespace media { | |
| 20 class MediaServerCrashListener; | |
| 21 | |
| 22 class MEDIA_EXPORT MediaServiceThrottler { | |
|
DaleCurtis
2016/11/15 00:49:32
Class docs?
tguilbert
2016/11/15 19:29:07
Done.
| |
| 23 public: | |
| 24 // Called to get the singleton MediaServiceThrottler instance. | |
| 25 // The first thread on which GetInstance() is called is the thread on which | |
| 26 // calls to OnMediaServerCrash() will be signaled. | |
| 27 static MediaServiceThrottler* GetInstance(); | |
| 28 | |
| 29 // Returns the delay to wait until a new client is allowed to be created. | |
| 30 base::TimeDelta GetDelayForClientCreation(); | |
| 31 | |
| 32 // Test only methods. | |
| 33 void SetTickClockForTesting(base::TickClock* clock); | |
| 34 void ResetInternalStateForTesting(); | |
| 35 base::TimeDelta GetBaseThrottlingRateForTesting(); | |
| 36 bool IsCrashListenerAliveForTesting(); | |
| 37 void SetCrashListenerTaskRunnerForTesting( | |
| 38 scoped_refptr<base::SingleThreadTaskRunner> crash_listener_task_runner); | |
| 39 | |
| 40 private: | |
| 41 friend struct base::DefaultLazyInstanceTraits<MediaServiceThrottler>; | |
| 42 friend class MediaServiceThrottlerTest; | |
| 43 | |
| 44 MediaServiceThrottler(); | |
| 45 virtual ~MediaServiceThrottler(); | |
| 46 | |
| 47 // Called by the |crash_listener_| whenever a crash is detected. | |
| 48 void OnMediaServerCrash(); | |
| 49 | |
| 50 // Updates |current_craches_| according to a linear decay function. | |
| 51 void UpdateServerCrashes(); | |
| 52 | |
| 53 // Ensures that the MediaServerCrashListener was properly started (can lead | |
| 54 // to OnMediaServerCrash() being called in the case it hasn't). | |
| 55 void EnsureCrashListenerStarted(); | |
| 56 | |
| 57 // Frees up the resources used by |crash_listener_|; | |
| 58 void ReleaseCrashListener(); | |
| 59 | |
| 60 // Gets the delay for ScheduleClientCreation(), which grows exponentially | |
| 61 // based on |current_crashes_|. | |
| 62 base::TimeDelta GetThrottlingDelayFromServerCrashes(); | |
| 63 | |
| 64 std::unique_ptr<base::TickClock> clock_; | |
| 65 | |
| 66 // Effective number of media server crashes. | |
| 67 // NOTE: This is of type double because we decay the number of crashes at a | |
| 68 // rate of one per minute (e.g. 30s after a single crash, |curren_crashes_| | |
| 69 // should be equal to 0.5). | |
| 70 double current_crashes_; | |
| 71 | |
| 72 // Next time at which a client creation can be scheduled. | |
| 73 base::TimeTicks next_schedulable_slot_; | |
| 74 | |
| 75 // Last media server crash time. | |
| 76 base::TimeTicks last_server_crash_; | |
| 77 | |
| 78 // Last time UpdateServerCrashes() was called. | |
| 79 base::TimeTicks last_current_crash_update_time_; | |
| 80 | |
| 81 // Last time ScheduleClientCreation() was called. | |
| 82 base::TimeTicks last_schedule_call_; | |
| 83 | |
| 84 // Callbacks used to release |crash_listener_| after 60s of inactivity. | |
| 85 base::Closure release_crash_listener_cb_; | |
| 86 base::CancelableClosure cancelable_release_crash_listener_cb_; | |
| 87 | |
| 88 // Listener that verifies | |
| 89 std::unique_ptr<MediaServerCrashListener> crash_listener_; | |
| 90 | |
| 91 scoped_refptr<base::SingleThreadTaskRunner> crash_listener_task_runner_; | |
| 92 | |
| 93 DISALLOW_COPY_AND_ASSIGN(MediaServiceThrottler); | |
| 94 }; | |
| 95 | |
| 96 } // namespace media | |
| 97 | |
| 98 #endif // MEDIA_BASE_ANDROID_MEDIA_SERVICE_THROTTLER_H_ | |
| OLD | NEW |