1
#include "source/common/quic/envoy_quic_alarm.h"
2

            
3
#include <algorithm>
4

            
5
namespace Envoy {
6
namespace Quic {
7

            
8
EnvoyQuicAlarm::EnvoyQuicAlarm(Event::Dispatcher& dispatcher, const quic::QuicClock& clock,
9
                               quic::QuicArenaScopedPtr<quic::QuicAlarm::Delegate> delegate)
10
54922
    : QuicAlarm(std::move(delegate)), dispatcher_(dispatcher),
11
75426
      timer_(dispatcher_.createTimer([this]() { Fire(); })), clock_(clock) {}
12

            
13
13931
void EnvoyQuicAlarm::CancelImpl() { timer_->disableTimer(); }
14

            
15
292154
void EnvoyQuicAlarm::SetImpl() {
16
292154
  quic::QuicTime::Delta duration = getDurationBeforeDeadline();
17
  // Round up the duration so that any duration < 1us will not be triggered within current event
18
  // loop. QUICHE alarm is not expected to be scheduled in current event loop. This bit is a bummer
19
  // in QUICHE, and we are working on the fix. Once QUICHE is fixed of expecting this behavior, we
20
  // no longer need to round up the duration.
21
  // TODO(antoniovicente) Remove the std::max(1, ...) when decommissioning the
22
  // envoy.reloadable_features.activate_timers_next_event_loop runtime flag.
23
292154
  timer_->enableHRTimer(
24
292154
      std::chrono::microseconds(std::max(static_cast<int64_t>(1), duration.ToMicroseconds())));
25
292154
}
26

            
27
243363
void EnvoyQuicAlarm::UpdateImpl() {
28
  // Since Timer::enableTimer() overrides its deadline from previous calls,
29
  // there is no need to disable the timer before enabling it again.
30
243363
  SetImpl();
31
243363
}
32

            
33
292154
quic::QuicTime::Delta EnvoyQuicAlarm::getDurationBeforeDeadline() {
34
292154
  quic::QuicTime::Delta duration(quic::QuicTime::Delta::Zero());
35
292154
  quic::QuicTime now = clock_.ApproximateNow();
36
292154
  quic::QuicTime tmp = deadline();
37
292154
  if (tmp > now) {
38
262563
    duration = tmp - now;
39
262563
  }
40
292154
  return duration;
41
292154
}
42

            
43
} // namespace Quic
44
} // namespace Envoy