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

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: Tweak 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 (c) 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;
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 serialized->AppendInteger(entry.failure_count());
22 // Can't use entry.GetTimeUntilRelease as it doesn't allow negative deltas.
23 base::TimeDelta time_until_release =
24 entry.GetReleaseTime() - entry.clock_->NowTicks();
25 // We redundantly store both the remaining time delta and the absolute time.
mmenke 2015/05/05 15:47:52 Avoid using "we" in comments.
johnme 2015/05/06 12:46:47 Done.
26 // This will let us work around some cases where wall clock time changes.
mmenke 2015/05/05 15:47:51 Avoid using "us" in comments.
johnme 2015/05/06 12:46:47 Done.
27 serialized->AppendDouble(time_until_release.InSecondsF());
28 base::Time absolute_release_time = time_until_release + time_now;
29 serialized->AppendDouble(absolute_release_time.ToDoubleT());
mmenke 2015/05/05 15:47:52 Do we really need these both? Seems like we could
johnme 2015/05/06 12:46:47 I added serialization of the time_until_release (n
mmenke 2015/05/06 17:34:47 Ah, right. Forgot about SetCustomReleaseTime.
30 return serialized;
31 }
32
33 scoped_ptr<BackoffEntry> BackoffEntrySerializer::DeserializeFromValue(
34 const base::ListValue& serialized, const BackoffEntry::Policy* policy,
35 base::TickClock* tick_clock, base::Time time_now) {
36 if (serialized.GetSize() != 4)
37 return nullptr;
38 int version_number;
39 if (!serialized.GetInteger(0, &version_number) ||
40 version_number != SERIALIZATION_VERSION_NUMBER)
41 return nullptr;
mmenke 2015/05/05 15:47:51 Use braces when the condition of an if takes up mo
johnme 2015/05/06 12:46:46 Done.
42 int failure_count;
43 if (!serialized.GetInteger(1, &failure_count))
mmenke 2015/05/05 15:47:51 We should fail if this is negative.
johnme 2015/05/06 12:46:47 Done.
44 return nullptr;
45 double original_time_until_release_double;
46 if (!serialized.GetDouble(2, &original_time_until_release_double))
47 return nullptr;
48 double absolute_release_time_double;
49 if (!serialized.GetDouble(3, &absolute_release_time_double))
mmenke 2015/05/05 15:47:51 We should fail if this is negative.
johnme 2015/05/06 12:46:47 Done.
50 return nullptr;
51 scoped_ptr<BackoffEntry> entry(new BackoffEntry(policy, tick_clock));
52 entry->failure_count_ = failure_count;
mmenke 2015/05/05 15:47:52 Erm...Could we just call InformOfRequest(false) fa
johnme 2015/05/06 12:46:47 Hmm, ok done, but note that I had to make TimeUnti
53 base::TimeDelta original_time_until_release =
54 base::TimeDelta::FromSecondsD(original_time_until_release_double);
55 base::Time absolute_release_time =
56 base::Time::FromDoubleT(absolute_release_time_double);
57 base::TimeDelta time_until_release =
58 absolute_release_time - time_now;
59 // In cases where the system wall clock is rewound, use the redundant
60 // original_time_until_release to ensure the backoff duration isn't longer
61 // than it was before serializing (note that it's not possible to protect
62 // against the clock being wound forward).
63 if (time_until_release > original_time_until_release)
64 time_until_release = original_time_until_release;
65 entry->SetCustomReleaseTime(
66 entry->TimeUntilReleaseToReleaseTime(time_until_release));
67 return entry;
mmenke 2015/05/05 15:47:52 Think this method could use a couple blank lines,
johnme 2015/05/06 12:46:47 Done.
68 }
69
70 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698