"Fossies" - the Fresh Open Source Software Archive

Member "google-gadgets-for-linux-0.11.2/ggadget/backoff.h" (15 Mar 2009, 2975 Bytes) of package /linux/misc/old/google-gadgets-for-linux-0.11.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2   Copyright 2008 Google Inc.
    3 
    4   Licensed under the Apache License, Version 2.0 (the "License");
    5   you may not use this file except in compliance with the License.
    6   You may obtain a copy of the License at
    7 
    8        http://www.apache.org/licenses/LICENSE-2.0
    9 
   10   Unless required by applicable law or agreed to in writing, software
   11   distributed under the License is distributed on an "AS IS" BASIS,
   12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   13   See the License for the specific language governing permissions and
   14   limitations under the License.
   15 */
   16 
   17 #ifndef GGADGET_BACKOFF_H__
   18 #define GGADGET_BACKOFF_H__
   19 
   20 #include <string>
   21 #include <ggadget/common.h>
   22 
   23 namespace ggadget {
   24 
   25 /**
   26  * @ingroup Utilities
   27  * Class to record service backoff information, to make sure the client
   28  * does not do a DDOS attack a service.
   29  * Uses modified truncated binary exponential backoff.
   30  * The wait unit count will be in the range
   31  * <code>2^(failure_count - 4) .. 2^(failure_count - 1)</code>
   32  * but with hard low and high limits.
   33  * For more information see:
   34  * http://en.wikipedia.org/wiki/Truncated_binary_exponential_backoff
   35  *
   36  * NOTE: The backoff and randomization features in this implementation is
   37  * very important for proper server-side operation. Please do *NOT* disable
   38  * or remove them.
   39  */
   40 class Backoff {
   41  public:
   42   Backoff();
   43   ~Backoff();
   44 
   45   /**
   46    * Judges if it is safe to do a request now.
   47    * @param now the current time in milliseconds.
   48    * @param request identifier of the request. It can be the request URL or
   49    *     the host name of a URL. It can't contain '\n' or '\t' characters.
   50    * @return @c true if it's ok to do the request now.
   51    */
   52   bool IsOkToRequest(uint64_t now, const char *request);
   53 
   54   /**
   55    * Gets the next allowed time for a request.
   56    * If the request has not failed, returns 0.
   57    */
   58   uint64_t GetNextAllowedTime(const char *request);
   59 
   60   /**
   61    * Gets the number of continuous failures of a request.
   62    */
   63   int GetFailureCount(const char *request);
   64 
   65   enum ResultType {
   66     SUCCESS,
   67     EXPONENTIAL_BACKOFF,
   68     CONSTANT_BACKOFF,
   69   };
   70 
   71   /**
   72    * Indicate success or failure of a request. Always report after each
   73    * request to keep backoff data updated.
   74    * Returns @c true if the backoff data changed.
   75    */
   76   bool ReportRequestResult(uint64_t now, const char *request,
   77                            ResultType result_type);
   78 
   79   /**
   80    * Clear all backoff data (for testing only).
   81    */
   82   void Clear();
   83 
   84   /**
   85    * Gets/sets the string representation of the backoff data.
   86    * Useful to save/load backoff data into/from config file.
   87    * The data contains lines of records of service status.
   88    * Format of each line is like:
   89    * service\tlast_failure_time\tfailure_count
   90    */
   91   std::string GetData(uint64_t now) const;
   92   void SetData(uint64_t now, const char *data);
   93 
   94  private:
   95   DISALLOW_EVIL_CONSTRUCTORS(Backoff);
   96   class Impl;
   97   Impl *impl_;
   98 };
   99 
  100 } // namespace ggadget
  101 
  102 #endif // GGADGET_BACKOFF_H__