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

Side by Side Diff: chrome/common/net/url_fetcher_protect.h

Issue 4194001: Implement exponential back-off mechanism and enforce it at the URLRequestHttpJob level. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 10 years 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/common/net/url_fetcher.cc ('k') | chrome/common/net/url_fetcher_protect.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2010 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 // This file implements backoff in the suggest system so that we don't
6 // DOS the Suggest servers when using URLFetcher.
7
8 #ifndef CHROME_COMMON_NET_URL_FETCHER_PROTECT_H_
9 #define CHROME_COMMON_NET_URL_FETCHER_PROTECT_H_
10 #pragma once
11
12 #include <map>
13 #include <queue>
14 #include <string>
15
16 #include "base/lock.h"
17 #include "base/scoped_ptr.h"
18 #include "base/time.h"
19
20
21 // This class is used to manage one service's rate protection. It maintains
22 // a queue of connection successes and failures and analyzes the requests
23 // over some period of time, in order to deduce the backoff time of every
24 // request.
25 // The backoff algorithm consists of two parts. Firstly, avoid too many
26 // send events in a sliding window. That will prevent traffic overload.
27 // Secondly, exponential backoff is used when receiving an error message
28 // from server. Exponential backoff period is calculated using the following
29 // formula:
30 //
31 // initial backoff time (the first time to receive error)
32 // backoff = k * current_backoff + c (the second, third, ... error)
33 // maximum backoff time (when backoff > maximum backoff time)
34 //
35 // where |k| is the multiplier, and |c| is the constant factor.
36 class URLFetcherProtectEntry {
37 public:
38 enum EventType {
39 SEND, // request will be sent out
40 SUCCESS, // successful response
41 FAILURE // no response or error
42 };
43
44 URLFetcherProtectEntry();
45 URLFetcherProtectEntry(int sliding_window_period, int max_send_threshold,
46 int max_retries, int initial_timeout,
47 double multiplier, int constant_factor,
48 int maximum_timeout);
49
50
51 virtual ~URLFetcherProtectEntry();
52
53 // When a connection event happens, log it to the queue, and recalculate
54 // the timeout period. It returns the backoff time, in milliseconds, that
55 // indicates to the sender how long should it wait before sending the request.
56 // If the request is allowed to be sent immediately, the backoff time is 0.
57 int64 UpdateBackoff(EventType event_type);
58
59 // Returns the max retries allowed.
60 int max_retries() const {
61 return max_retries_;
62 }
63
64 // Sets the max retries.
65 void SetMaxRetries(int max_retries) {
66 max_retries_ = max_retries;
67 }
68
69 private:
70 // When a request comes, calculate the release time for it.
71 // Returns the backoff time before sending.
72 base::TimeDelta AntiOverload();
73 // Resets backoff when service is ok.
74 // Returns the backoff time before sending.
75 base::TimeDelta ResetBackoff();
76 // Calculates new backoff when encountering a failure.
77 // Returns the backoff time before sending.
78 base::TimeDelta IncreaseBackoff();
79
80 // Default parameters. Time is in milliseconds.
81 static const int kDefaultSlidingWindowPeriod;
82 static const int kDefaultMaxSendThreshold;
83 static const int kDefaultMaxRetries;
84 static const int kDefaultInitialTimeout;
85 static const double kDefaultMultiplier;
86 static const int kDefaultConstantFactor;
87 static const int kDefaultMaximumTimeout;
88
89 // time to consider events when checking backoff
90 int sliding_window_period_;
91
92 // maximum number of requests allowed in sliding window period
93 int max_send_threshold_;
94 // maximum retris allowed
95 int max_retries_;
96
97 // initial timeout on first failure
98 int initial_timeout_;
99 // factor by which to multiply on exponential backoff (e.g., 2.0)
100 double multiplier_;
101 // constant time term to add to each attempt
102 int constant_factor_;
103 // maximum amount of time between requests
104 int maximum_timeout_;
105
106 // current exponential backoff period
107 int timeout_period_;
108 // time that protection is scheduled to end
109 base::TimeTicks release_time_;
110
111 // Sets up a lock to ensure thread safe.
112 Lock lock_;
113
114 // A list of the recent send events. We ues them to decide whether
115 // there are too many requests sent in sliding window.
116 std::queue<base::TimeTicks> send_log_;
117
118 DISALLOW_COPY_AND_ASSIGN(URLFetcherProtectEntry);
119 };
120
121
122 // This singleton class is used to manage all protect entries.
123 // Now we use the host name as service id.
124 class URLFetcherProtectManager {
125 public:
126 ~URLFetcherProtectManager();
127
128 // Returns the global instance of this class.
129 static URLFetcherProtectManager* GetInstance();
130
131 // Registers a new entry in this service. If the entry already exists,
132 // just returns it. Ownership of the return object remains with the manager.
133 URLFetcherProtectEntry* Register(const std::string& id);
134 // Always registers the entry even when it exists; any existing entry for this
135 // id will be deleted and existing references to it will become invalid.
136 // Ownership of the return object remains with the manager.
137 URLFetcherProtectEntry* Register(const std::string& id,
138 URLFetcherProtectEntry* entry);
139
140 private:
141 URLFetcherProtectManager();
142
143 typedef std::map<const std::string, URLFetcherProtectEntry*> ProtectService;
144
145 static Lock lock_;
146 static scoped_ptr<URLFetcherProtectManager> protect_manager_;
147 ProtectService services_;
148
149 DISALLOW_COPY_AND_ASSIGN(URLFetcherProtectManager);
150 };
151
152 #endif // CHROME_COMMON_NET_URL_FETCHER_PROTECT_H_
OLDNEW
« no previous file with comments | « chrome/common/net/url_fetcher.cc ('k') | chrome/common/net/url_fetcher_protect.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698