Add a default timeout parameter to HHWheelTimer.
authorMaxim Georgiev <maxgeorg@fb.com>
Fri, 21 Aug 2015 19:54:59 +0000 (12:54 -0700)
committerfacebook-github-bot-9 <folly-bot@fb.com>
Fri, 21 Aug 2015 20:20:22 +0000 (13:20 -0700)
Summary: Currently HHWheelTimer requires providing an explicit timeout value every time a new timeout is scgeduled. This change adds an optional "default timeout" parameter. With this parameter set, HHWheelTimer can be used the same way as AsyncTimeoutSet. Variable timeout functionality is still available even if the default parameter is set.

Reviewed By: @djwatson

Differential Revision: D2366783

folly/io/async/HHWheelTimer.cpp
folly/io/async/HHWheelTimer.h

index 366b5eeb7a47e0f00f5bd91c8707d1e07671334f..1fd5e6d25d81b041bf691c154b610b63664bd456 100644 (file)
@@ -77,9 +77,11 @@ void HHWheelTimer::Callback::cancelTimeoutImpl() {
 
 HHWheelTimer::HHWheelTimer(folly::EventBase* eventBase,
                            std::chrono::milliseconds intervalMS,
-                           AsyncTimeout::InternalEnum internal)
+                           AsyncTimeout::InternalEnum internal,
+                           std::chrono::milliseconds defaultTimeoutMS)
   : AsyncTimeout(eventBase, internal)
   , interval_(intervalMS)
+  , defaultTimeout_(defaultTimeoutMS)
   , nextTick_(1)
   , count_(0)
   , catchupEveryN_(DEFAULT_CATCHUP_EVERY_N)
@@ -137,6 +139,12 @@ void HHWheelTimer::scheduleTimeout(Callback* callback,
   count_++;
 }
 
+void HHWheelTimer::scheduleTimeout(Callback* callback) {
+  CHECK(std::chrono::milliseconds(-1) != defaultTimeout_)
+      << "Default timeout was not initialized";
+  scheduleTimeout(callback, defaultTimeout_);
+}
+
 bool HHWheelTimer::cascadeTimers(int bucket, int tick) {
   CallbackList cbs;
   cbs.swap(buckets_[bucket][tick]);
index 56ded208aaf6e61633856da5768032c0192a5ede..0424ac59793b769fa95006a5b5e00c4265c4cfb3 100644 (file)
@@ -148,14 +148,22 @@ class HHWheelTimer : private folly::AsyncTimeout,
   };
 
   /**
-   * Create a new HHWheelTimer with the specified interval.
+   * Create a new HHWheelTimer with the specified interval and the
+   * default timeout value set.
+   *
+   * Objects created using this version of constructor can be used
+   * to schedule both variable interval timeouts using
+   * scheduleTimeout(callback, timeout) method, and default
+   * interval timeouts using scheduleTimeout(callback) method.
    */
   static int DEFAULT_TICK_INTERVAL;
   explicit HHWheelTimer(folly::EventBase* eventBase,
                         std::chrono::milliseconds intervalMS =
                         std::chrono::milliseconds(DEFAULT_TICK_INTERVAL),
                         AsyncTimeout::InternalEnum internal =
-                        AsyncTimeout::InternalEnum::NORMAL);
+                        AsyncTimeout::InternalEnum::NORMAL,
+                        std::chrono::milliseconds defaultTimeoutMS =
+                        std::chrono::milliseconds(-1));
 
   /**
    * Destroy the HHWheelTimer.
@@ -182,6 +190,15 @@ class HHWheelTimer : private folly::AsyncTimeout,
     return interval_;
   }
 
+  /**
+   * Get the default timeout interval for this HHWheelTimer.
+   *
+   * Returns the timeout interval in milliseconds.
+   */
+  std::chrono::milliseconds getDefaultTimeout() const {
+    return defaultTimeout_;
+  }
+
   /**
    * Schedule the specified Callback to be invoked after the
    * specified timeout interval.
@@ -194,6 +211,18 @@ class HHWheelTimer : private folly::AsyncTimeout,
   void scheduleTimeoutImpl(Callback* callback,
                        std::chrono::milliseconds timeout);
 
+  /**
+   * Schedule the specified Callback to be invoked after the
+   * fefault timeout interval.
+   *
+   * If the callback is already scheduled, this cancels the existing timeout
+   * before scheduling the new timeout.
+   *
+   * This method uses CHECK() to make sure that the default timeout was
+   * specified on the object initialization.
+   */
+  void scheduleTimeout(Callback* callback);
+
   template <class F>
   void scheduleTimeoutFn(F fn, std::chrono::milliseconds timeout) {
     struct Wrapper : Callback {
@@ -262,6 +291,7 @@ class HHWheelTimer : private folly::AsyncTimeout,
   virtual void timeoutExpired() noexcept;
 
   std::chrono::milliseconds interval_;
+  std::chrono::milliseconds defaultTimeout_;
 
   static constexpr int WHEEL_BUCKETS = 4;
   static constexpr int WHEEL_BITS = 8;