Skip to content

Commit

Permalink
Revert "[chttp2] Fix bug in timeout encoding" (grpc#34811)
Browse files Browse the repository at this point in the history
Reverts grpc#34751
  • Loading branch information
ctiller authored Oct 27, 2023
1 parent 0798590 commit af7aa3b
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 113 deletions.
14 changes: 0 additions & 14 deletions fuzztest/core/transport/chttp2/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,3 @@ grpc_fuzz_test(
],
deps = ["//src/core:write_size_policy"],
)

grpc_fuzz_test(
name = "hpack_encoder_timeout_test",
srcs = ["hpack_encoder_timeout_test.cc"],
external_deps = [
"fuzztest",
"fuzztest_main",
"gtest",
],
deps = [
"//:hpack_encoder",
"//:hpack_parser",
],
)
79 changes: 0 additions & 79 deletions fuzztest/core/transport/chttp2/hpack_encoder_timeout_test.cc

This file was deleted.

26 changes: 16 additions & 10 deletions src/core/ext/transport/chttp2/transport/hpack_encoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -475,25 +475,31 @@ void Encoder::EncodeRepeatingSliceValue(const absl::string_view& key,

void TimeoutCompressorImpl::EncodeWith(absl::string_view key,
Timestamp deadline, Encoder* encoder) {
const Timeout timeout = Timeout::FromDuration(deadline - Timestamp::Now());
Timeout timeout = Timeout::FromDuration(deadline - Timestamp::Now());
auto& table = encoder->hpack_table();
for (size_t i = 0; i < kNumPreviousValues; i++) {
const auto& previous = previous_timeouts_[i];
if (!table.ConvertableToDynamicIndex(previous.index)) continue;
const double ratio = timeout.RatioVersus(previous.timeout);
for (auto it = previous_timeouts_.begin(); it != previous_timeouts_.end();
++it) {
double ratio = timeout.RatioVersus(it->timeout);
// If the timeout we're sending is shorter than a previous timeout, but
// within 3% of it, we'll consider sending it.
if (ratio > -3 && ratio <= 0) {
encoder->EmitIndexed(table.DynamicIndex(previous.index));
if (ratio > -3 && ratio <= 0 &&
table.ConvertableToDynamicIndex(it->index)) {
encoder->EmitIndexed(table.DynamicIndex(it->index));
// Put this timeout to the front of the queue - forces common timeouts to
// be considered earlier.
std::swap(*it, *previous_timeouts_.begin());
return;
}
}
// Clean out some expired timeouts.
while (!previous_timeouts_.empty() &&
!table.ConvertableToDynamicIndex(previous_timeouts_.back().index)) {
previous_timeouts_.pop_back();
}
Slice encoded = timeout.Encode();
uint32_t index = encoder->EmitLitHdrWithNonBinaryStringKeyIncIdx(
Slice::FromStaticString(key), std::move(encoded));
uint32_t i = next_previous_value_;
++next_previous_value_;
previous_timeouts_[i % kNumPreviousValues] = PreviousTimeout{timeout, index};
previous_timeouts_.push_back(PreviousTimeout{timeout, index});
}

Encoder::Encoder(HPackCompressor* compressor, bool use_true_binary_metadata,
Expand Down
10 changes: 3 additions & 7 deletions src/core/ext/transport/chttp2/transport/hpack_encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -276,20 +276,16 @@ class Compressor<MetadataTrait, SmallSetOfValuesCompressor> {
};

struct PreviousTimeout {
Timeout timeout = Timeout::FromDuration(Duration::Zero());
// Dynamic table index of a previously sent timeout
// 0 is guaranteed not in the dynamic table so is a safe initializer
uint32_t index = 0;
Timeout timeout;
uint32_t index;
};

class TimeoutCompressorImpl {
public:
void EncodeWith(absl::string_view key, Timestamp deadline, Encoder* encoder);

private:
static constexpr const size_t kNumPreviousValues = 5;
PreviousTimeout previous_timeouts_[kNumPreviousValues];
uint32_t next_previous_value_ = 0;
std::vector<PreviousTimeout> previous_timeouts_;
};

template <typename MetadataTrait>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ class HPackEncoderTable {
table_elems_ - index;
}
// Check if an element index is convertable to a dynamic index
// Note that 0 is always not convertable
bool ConvertableToDynamicIndex(uint32_t index) const {
return index > tail_remote_index_;
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/lib/transport/timeout_encoding.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ class Timeout {
static Timeout FromMinutes(int64_t minutes);
static Timeout FromHours(int64_t hours);

uint16_t value_ = 0;
Unit unit_ = Unit::kNanoseconds;
uint16_t value_;
Unit unit_;
};

absl::optional<Duration> ParseTimeout(const Slice& text);
Expand Down

0 comments on commit af7aa3b

Please sign in to comment.