Use structured ABSL types for handling time values This CL migrates a bunch of GFE code to use absl::Time and absl::Duration rather than bare numbers to represent time values. PiperOrigin-RevId: 504627740
diff --git a/quiche/http2/core/priority_write_scheduler.h b/quiche/http2/core/priority_write_scheduler.h index 4e32b90..d63a316 100644 --- a/quiche/http2/core/priority_write_scheduler.h +++ b/quiche/http2/core/priority_write_scheduler.h
@@ -16,6 +16,8 @@ #include "absl/container/flat_hash_map.h" #include "absl/strings/str_cat.h" +#include "absl/time/time.h" +#include "absl/types/optional.h" #include "quiche/common/platform/api/quiche_bug_tracker.h" #include "quiche/common/platform/api/quiche_export.h" #include "quiche/common/platform/api/quiche_logging.h" @@ -149,11 +151,10 @@ stream_info->priority = std::move(priority); } - // Records time (in microseconds) of a read/write event for the given - // stream. + // Records time of a read/write event for the given stream. // // Preconditions: `stream_id` should be registered. - void RecordStreamEventTime(StreamIdType stream_id, int64_t now_in_usec) { + void RecordStreamEventTime(StreamIdType stream_id, absl::Time now) { auto it = stream_infos_.find(stream_id); if (it == stream_infos_.end()) { QUICHE_BUG(spdy_bug_19_4) << "Stream " << stream_id << " not registered"; @@ -161,29 +162,30 @@ } PriorityInfo& priority_info = priority_infos_[PriorityTypeToInt()(it->second->priority)]; - priority_info.last_event_time_usec = - std::max(priority_info.last_event_time_usec, now_in_usec); + priority_info.last_event_time = + std::max(priority_info.last_event_time, absl::make_optional(now)); } - // Returns time (in microseconds) of the last read/write event for a stream - // with higher priority than the priority of the given stream, or 0 if there - // is no such event. + // Returns time of the last read/write event for a stream with higher priority + // than the priority of the given stream, or nullopt if there is no such + // event. // // Preconditions: `stream_id` should be registered. - int64_t GetLatestEventWithPriority(StreamIdType stream_id) const { + absl::optional<absl::Time> GetLatestEventWithPriority( + StreamIdType stream_id) const { auto it = stream_infos_.find(stream_id); if (it == stream_infos_.end()) { QUICHE_BUG(spdy_bug_19_5) << "Stream " << stream_id << " not registered"; - return 0; + return absl::nullopt; } - int64_t last_event_time_usec = 0; + absl::optional<absl::Time> last_event_time; const StreamInfo* const stream_info = it->second.get(); for (int p = kHighestPriority; p < PriorityTypeToInt()(stream_info->priority); ++p) { - last_event_time_usec = std::max(last_event_time_usec, - priority_infos_[p].last_event_time_usec); + last_event_time = + std::max(last_event_time, priority_infos_[p].last_event_time); } - return last_event_time_usec; + return last_event_time; } // If the scheduler has any ready streams, returns the next scheduled @@ -344,8 +346,8 @@ struct QUICHE_EXPORT PriorityInfo { // IDs of streams that are ready to write. ReadyList ready_list; - // Time of latest write event for stream of this priority, in microseconds. - int64_t last_event_time_usec = 0; + // Time of latest write event for stream of this priority. + absl::optional<absl::Time> last_event_time; }; // Use std::unique_ptr, because absl::flat_hash_map does not have pointer
diff --git a/quiche/http2/core/priority_write_scheduler_test.cc b/quiche/http2/core/priority_write_scheduler_test.cc index 8a4e518..96c4bd2 100644 --- a/quiche/http2/core/priority_write_scheduler_test.cc +++ b/quiche/http2/core/priority_write_scheduler_test.cc
@@ -15,6 +15,8 @@ using ::spdy::kHttp2RootStreamId; using ::spdy::SpdyPriority; using ::spdy::SpdyStreamId; +using ::testing::Eq; +using ::testing::Optional; template <typename StreamIdType> class PriorityWriteSchedulerPeer { @@ -314,22 +316,26 @@ } TEST_F(PriorityWriteSchedulerTest, GetLatestEventWithPriority) { - EXPECT_QUICHE_BUG(scheduler_.RecordStreamEventTime(3, 5), - "Stream 3 not registered"); - EXPECT_QUICHE_BUG(EXPECT_EQ(0, scheduler_.GetLatestEventWithPriority(4)), - "Stream 4 not registered"); + EXPECT_QUICHE_BUG( + scheduler_.RecordStreamEventTime(3, absl::FromUnixMicros(5)), + "Stream 3 not registered"); + EXPECT_QUICHE_BUG( + EXPECT_FALSE(scheduler_.GetLatestEventWithPriority(4).has_value()), + "Stream 4 not registered"); for (int i = 1; i < 5; ++i) { scheduler_.RegisterStream(i, i); } for (int i = 1; i < 5; ++i) { - EXPECT_EQ(0, scheduler_.GetLatestEventWithPriority(i)); + EXPECT_FALSE(scheduler_.GetLatestEventWithPriority(i).has_value()); } for (int i = 1; i < 5; ++i) { - scheduler_.RecordStreamEventTime(i, i * 100); + scheduler_.RecordStreamEventTime(i, absl::FromUnixMicros(i * 100)); } - for (int i = 1; i < 5; ++i) { - EXPECT_EQ((i - 1) * 100, scheduler_.GetLatestEventWithPriority(i)); + EXPECT_FALSE(scheduler_.GetLatestEventWithPriority(1).has_value()); + for (int i = 2; i < 5; ++i) { + EXPECT_THAT(scheduler_.GetLatestEventWithPriority(i), + Optional(Eq(absl::FromUnixMicros((i - 1) * 100)))); } }