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

Side by Side Diff: net/base/backoff_entry_serializer.cc

Issue 1023473003: Allow BackoffEntry to be serialized and deserialized. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed mmenke's review comments Created 5 years, 7 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 2015 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 #include "net/base/backoff_entry_serializer.h"
6
7 #include "base/time/tick_clock.h"
8 #include "base/values.h"
9 #include "net/base/backoff_entry.h"
10
11 namespace {
12 const int SERIALIZATION_VERSION_NUMBER = 1;
mmenke 2015/05/06 17:34:48 Think this is worth a short comment
mmenke 2015/05/06 17:34:48 nit: kSerializationVersionNumber (Or maybe just k
johnme 2015/05/06 20:38:08 Done.
johnme 2015/05/06 20:38:08 Done.
13 } // namespace
14
15 namespace net {
16
17 scoped_ptr<base::ListValue> BackoffEntrySerializer::SerializeToValue(
18 const BackoffEntry& entry, base::Time time_now) {
19 scoped_ptr<base::ListValue> serialized(new base::ListValue());
20 serialized->AppendInteger(SERIALIZATION_VERSION_NUMBER);
21
22 serialized->AppendInteger(entry.failure_count());
23
24 // Can't use entry.GetTimeUntilRelease as it doesn't allow negative deltas.
25 base::TimeDelta backoff_duration =
26 entry.GetReleaseTime() - entry.tick_clock()->NowTicks();
27 // Redundantly stores both the remaining time delta and the absolute time.
28 // The delta is used to work around some cases where wall clock time changes.
29 serialized->AppendDouble(backoff_duration.InSecondsF());
30 base::Time absolute_release_time = backoff_duration + time_now;
31 serialized->AppendDouble(absolute_release_time.ToDoubleT());
32
33 return serialized;
34 }
35
36 scoped_ptr<BackoffEntry> BackoffEntrySerializer::DeserializeFromValue(
37 const base::ListValue& serialized, const BackoffEntry::Policy* policy,
38 base::TickClock* tick_clock, base::Time time_now) {
39 if (serialized.GetSize() != 4)
40 return nullptr;
41 int version_number;
42 if (!serialized.GetInteger(0, &version_number) ||
43 version_number != SERIALIZATION_VERSION_NUMBER) {
44 return nullptr;
45 }
46
47 int failure_count;
48 if (!serialized.GetInteger(1, &failure_count) || failure_count < 0)
49 return nullptr;
50 double original_backoff_duration_double;
51 if (!serialized.GetDouble(2, &original_backoff_duration_double))
52 return nullptr;
53 double absolute_release_time_double;
54 if (!serialized.GetDouble(3, &absolute_release_time_double) ||
55 absolute_release_time_double < 0) {
56 return nullptr;
57 }
58
59 scoped_ptr<BackoffEntry> entry(new BackoffEntry(policy, tick_clock));
60
61 for (int n = 0; n < failure_count; n++)
62 entry->InformOfRequest(false);
63
64 base::TimeDelta original_backoff_duration =
65 base::TimeDelta::FromSecondsD(original_backoff_duration_double);
66 base::Time absolute_release_time =
67 base::Time::FromDoubleT(absolute_release_time_double);
68 base::TimeDelta backoff_duration =
69 absolute_release_time - time_now;
mmenke 2015/05/06 17:34:48 nit: Does this fit on one line?
johnme 2015/05/06 20:38:08 Done.
70 // In cases where the system wall clock is rewound, use the redundant
71 // original_backoff_duration to ensure the backoff duration isn't longer
72 // than it was before serializing (note that it's not possible to protect
73 // against the clock being wound forward).
74 if (backoff_duration > original_backoff_duration)
75 backoff_duration = original_backoff_duration;
76 entry->SetCustomReleaseTime(
77 entry->BackoffDurationToReleaseTime(backoff_duration));
78
79 return entry;
80 }
81
82 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698