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

Side by Side Diff: third_party/cacheinvalidation/src/google/cacheinvalidation/impl/smearer.h

Issue 1162033004: Pull cacheinvalidations code directory into chromium repo. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 6 months 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
OLDNEW
(Empty)
1 // Copyright 2012 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // An abstraction to "smear" values by a given percent. Useful for randomizing
16 // delays a little bit so that (say) processes do not get synchronized on time
17 // inadvertently, e.g., a heartbeat task that sends a message every few minutes
18 // is smeared so that all clients do not end up sending a message at the same
19 // time. In particular, given a |delay|, returns a value that is randomly
20 // distributed between
21 // [delay - smearPercent * delay, delay + smearPercent * delay]
22
23 #ifndef GOOGLE_CACHEINVALIDATION_IMPL_SMEARER_H_
24 #define GOOGLE_CACHEINVALIDATION_IMPL_SMEARER_H_
25
26 #include "google/cacheinvalidation/deps/logging.h"
27 #include "google/cacheinvalidation/deps/random.h"
28 #include "google/cacheinvalidation/deps/scoped_ptr.h"
29
30 namespace invalidation {
31
32 class Smearer {
33 public:
34 /* Creates a smearer with the given random number generator.
35 * REQUIRES: 0 <= smear_percent <= 100
36 * Caller continues to own space for random.
37 */
38 Smearer(Random* random, int smear_percent) : random_(random),
39 smear_fraction_(smear_percent / 100.0) {
40 CHECK((smear_percent >= 0) && (smear_percent <= 100));
41 }
42
43 /* Given a delay, returns a value that is randomly distributed between
44 * (delay - smear_percent * delay, delay + smear_percent * delay)
45 */
46 TimeDelta GetSmearedDelay(TimeDelta delay) {
47 // Get a random number between -1 and 1 and then multiply that by the
48 // smear fraction.
49 double smear_factor = (2 * random_->RandDouble() - 1.0) * smear_fraction_;
50 return TimeDelta::FromMilliseconds(
51 delay.InMilliseconds() * (1.0 + smear_factor));
52 }
53
54 private:
55 Random* random_;
56
57 /* The percentage (0, 1.0] for smearing the delay. */
58 double smear_fraction_;
59 };
60 } // namespace invalidation
61
62 #endif // GOOGLE_CACHEINVALIDATION_IMPL_SMEARER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698