Skip to content

Commit 0f97d60

Browse files
committed
quic: use TimerWrap for idle and retransmit timers
Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: #34186 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent e19a251 commit 0f97d60

5 files changed

+9
-87
lines changed

src/quic/node_quic_session-inl.h

+3-6
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,7 @@ QuicCID QuicSession::dcid() const {
375375
// timer to actually monitor. Here we take the calculated timeout
376376
// and extend out the libuv timer.
377377
void QuicSession::UpdateRetransmitTimer(uint64_t timeout) {
378-
DCHECK_NOT_NULL(retransmit_);
379-
retransmit_->Update(timeout);
378+
retransmit_.Update(timeout, timeout);
380379
}
381380

382381
void QuicSession::CheckAllocatedSize(size_t previous_size) const {
@@ -512,13 +511,11 @@ void QuicSession::set_remote_transport_params() {
512511
}
513512

514513
void QuicSession::StopIdleTimer() {
515-
CHECK_NOT_NULL(idle_);
516-
idle_->Stop();
514+
idle_.Stop();
517515
}
518516

519517
void QuicSession::StopRetransmitTimer() {
520-
CHECK_NOT_NULL(retransmit_);
521-
retransmit_->Stop();
518+
retransmit_.Stop();
522519
}
523520

524521
// Called by the OnVersionNegotiation callback when a version

src/quic/node_quic_session.cc

+3-4
Original file line numberDiff line numberDiff line change
@@ -1431,8 +1431,8 @@ QuicSession::QuicSession(
14311431
socket_(socket),
14321432
alpn_(alpn),
14331433
hostname_(hostname),
1434-
idle_(new Timer(socket->env(), [this]() { OnIdleTimeout(); })),
1435-
retransmit_(new Timer(socket->env(), [this]() { MaybeTimeout(); })),
1434+
idle_(socket->env(), [this](void* data) { OnIdleTimeout(); }),
1435+
retransmit_(socket->env(), [this](void* data) { MaybeTimeout(); }),
14361436
dcid_(dcid),
14371437
state_(env()->isolate()),
14381438
quic_state_(socket->quic_state()) {
@@ -2461,14 +2461,13 @@ void QuicSession::UpdateConnectionID(
24612461
// will be silently closed. It is important to update this as activity
24622462
// occurs to keep the idle timer from firing.
24632463
void QuicSession::UpdateIdleTimer() {
2464-
CHECK_NOT_NULL(idle_);
24652464
uint64_t now = uv_hrtime();
24662465
uint64_t expiry = ngtcp2_conn_get_idle_expiry(connection());
24672466
// nano to millis
24682467
uint64_t timeout = expiry > now ? (expiry - now) / 1000000ULL : 1;
24692468
if (timeout == 0) timeout = 1;
24702469
Debug(this, "Updating idle timeout to %" PRIu64, timeout);
2471-
idle_->Update(timeout);
2470+
idle_.Update(timeout, timeout);
24722471
}
24732472

24742473

src/quic/node_quic_session.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "node_quic_util.h"
1919
#include "node_sockaddr.h"
2020
#include "stream_base.h"
21+
#include "timer_wrap.h"
2122
#include "v8.h"
2223
#include "uv.h"
2324

@@ -1471,8 +1472,8 @@ class QuicSession : public AsyncWrap,
14711472
QuicSessionListener* listener_ = nullptr;
14721473
JSQuicSessionListener default_listener_;
14731474

1474-
TimerPointer idle_;
1475-
TimerPointer retransmit_;
1475+
TimerWrapHandle idle_;
1476+
TimerWrapHandle retransmit_;
14761477

14771478
QuicCID scid_;
14781479
QuicCID dcid_;

src/quic/node_quic_util-inl.h

-43
Original file line numberDiff line numberDiff line change
@@ -73,49 +73,6 @@ size_t GetMaxPktLen(const SocketAddress& addr) {
7373
NGTCP2_MAX_PKTLEN_IPV4;
7474
}
7575

76-
Timer::Timer(Environment* env, std::function<void()> fn)
77-
: env_(env),
78-
fn_(fn) {
79-
uv_timer_init(env_->event_loop(), &timer_);
80-
timer_.data = this;
81-
}
82-
83-
void Timer::Stop() {
84-
if (stopped_)
85-
return;
86-
stopped_ = true;
87-
88-
if (timer_.data == this) {
89-
uv_timer_stop(&timer_);
90-
timer_.data = nullptr;
91-
}
92-
}
93-
94-
// If the timer is not currently active, interval must be either 0 or greater.
95-
// If the timer is already active, interval is ignored.
96-
void Timer::Update(uint64_t interval) {
97-
if (stopped_)
98-
return;
99-
uv_timer_start(&timer_, OnTimeout, interval, interval);
100-
uv_unref(reinterpret_cast<uv_handle_t*>(&timer_));
101-
}
102-
103-
void Timer::Free(Timer* timer) {
104-
timer->env_->CloseHandle(
105-
reinterpret_cast<uv_handle_t*>(&timer->timer_),
106-
[&](uv_handle_t* timer) {
107-
Timer* t = ContainerOf(
108-
&Timer::timer_,
109-
reinterpret_cast<uv_timer_t*>(timer));
110-
delete t;
111-
});
112-
}
113-
114-
void Timer::OnTimeout(uv_timer_t* timer) {
115-
Timer* t = ContainerOf(&Timer::timer_, timer);
116-
t->fn_();
117-
}
118-
11976
QuicError::QuicError(
12077
int32_t family_,
12178
uint64_t code_) :

src/quic/node_quic_util.h

-32
Original file line numberDiff line numberDiff line change
@@ -338,38 +338,6 @@ class QuicCID : public MemoryRetainer {
338338
const ngtcp2_cid* ptr_;
339339
};
340340

341-
// Simple timer wrapper that is used to implement the internals
342-
// for idle and retransmission timeouts. Call Update to start or
343-
// reset the timer; Stop to halt the timer.
344-
class Timer final : public MemoryRetainer {
345-
public:
346-
inline explicit Timer(Environment* env, std::function<void()> fn);
347-
348-
// Stops the timer with the side effect of the timer no longer being usable.
349-
// It will be cleaned up and the Timer object will be destroyed.
350-
inline void Stop();
351-
352-
// If the timer is not currently active, interval must be either 0 or greater.
353-
// If the timer is already active, interval is ignored.
354-
inline void Update(uint64_t interval);
355-
356-
static inline void Free(Timer* timer);
357-
358-
SET_NO_MEMORY_INFO()
359-
SET_MEMORY_INFO_NAME(Timer)
360-
SET_SELF_SIZE(Timer)
361-
362-
private:
363-
static inline void OnTimeout(uv_timer_t* timer);
364-
365-
bool stopped_ = false;
366-
Environment* env_;
367-
std::function<void()> fn_;
368-
uv_timer_t timer_;
369-
};
370-
371-
using TimerPointer = DeleteFnPtr<Timer, Timer::Free>;
372-
373341
// A Stateless Reset Token is a mechanism by which a QUIC
374342
// endpoint can discreetly signal to a peer that it has
375343
// lost all state associated with a connection. This

0 commit comments

Comments
 (0)