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

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 nits 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/strings/string_number_conversions.h"
8 #include "base/time/tick_clock.h"
9 #include "base/values.h"
10 #include "net/base/backoff_entry.h"
11
12 namespace {
13 // Increment this number if we change serialization format, so we don't
14 // misinterpret old serialized values loaded from disk etc.
15 const int kSerializationFormatVersion = 1;
16 } // namespace
17
18 namespace net {
19
20 scoped_ptr<base::Value> BackoffEntrySerializer::SerializeToValue(
21 const BackoffEntry& entry, base::Time time_now) {
22 scoped_ptr<base::ListValue> serialized(new base::ListValue());
23 serialized->AppendInteger(kSerializationFormatVersion);
24
25 serialized->AppendInteger(entry.failure_count());
26
27 // Can't use entry.GetTimeUntilRelease as it doesn't allow negative deltas.
28 base::TimeDelta backoff_duration =
29 entry.GetReleaseTime() - entry.tick_clock()->NowTicks();
30 // Redundantly stores both the remaining time delta and the absolute time.
31 // The delta is used to work around some cases where wall clock time changes.
32 serialized->AppendDouble(backoff_duration.InSecondsF());
33 base::Time absolute_release_time = backoff_duration + time_now;
34 serialized->AppendString(
35 base::Int64ToString(absolute_release_time.ToInternalValue()));
36
37 return serialized.Pass();
38 }
39
40 scoped_ptr<BackoffEntry> BackoffEntrySerializer::DeserializeFromValue(
41 const base::Value& serialized, const BackoffEntry::Policy* policy,
42 base::TickClock* tick_clock, base::Time time_now) {
43 const base::ListValue* serialized_list = nullptr;
44 if (!serialized.GetAsList(&serialized_list))
45 return nullptr;
46 if (serialized_list->GetSize() != 4)
47 return nullptr;
48 int version_number;
49 if (!serialized_list->GetInteger(0, &version_number) ||
50 version_number != kSerializationFormatVersion) {
51 return nullptr;
52 }
53
54 int failure_count;
55 if (!serialized_list->GetInteger(1, &failure_count) || failure_count < 0)
56 return nullptr;
57 double original_backoff_duration_double;
58 if (!serialized_list->GetDouble(2, &original_backoff_duration_double))
59 return nullptr;
60 std::string absolute_release_time_string;
61 if (!serialized_list->GetString(3, &absolute_release_time_string))
62 return nullptr;
63 int64 absolute_release_time_us;
64 if (!base::StringToInt64(absolute_release_time_string,
65 &absolute_release_time_us)
66 || absolute_release_time_us < 0) {
mmenke 2015/05/06 20:48:44 || should go on the previous line, I believe. You
johnme 2015/05/07 12:16:00 Done (`git cl format net` allows both, and the sty
67 return nullptr;
68 }
69
70 scoped_ptr<BackoffEntry> entry(new BackoffEntry(policy, tick_clock));
71
72 for (int n = 0; n < failure_count; n++)
73 entry->InformOfRequest(false);
74
75 base::TimeDelta original_backoff_duration =
76 base::TimeDelta::FromSecondsD(original_backoff_duration_double);
77 base::Time absolute_release_time =
78 base::Time::FromInternalValue(absolute_release_time_us);
79 base::TimeDelta backoff_duration = absolute_release_time - time_now;
80 // In cases where the system wall clock is rewound, use the redundant
81 // original_backoff_duration to ensure the backoff duration isn't longer
82 // than it was before serializing (note that it's not possible to protect
83 // against the clock being wound forward).
84 if (backoff_duration > original_backoff_duration)
85 backoff_duration = original_backoff_duration;
86 entry->SetCustomReleaseTime(
87 entry->BackoffDurationToReleaseTime(backoff_duration));
88
89 return entry;
90 }
91
92 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698