Skip to content

Commit 664f9c4

Browse files
committed
dial.go: Re-use tickers during OpenConnectionRequest/Reply sequence.
1 parent e76728a commit 664f9c4

File tree

3 files changed

+26
-29
lines changed

3 files changed

+26
-29
lines changed

conn.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func newConn(conn net.PacketConn, raddr net.Addr, mtu uint16, h connectionHandle
105105
win: newDatagramWindow(),
106106
packetQueue: newPacketQueue(),
107107
retransmission: newRecoveryQueue(),
108-
buf: bytes.NewBuffer(make([]byte, 0, mtu)),
108+
buf: bytes.NewBuffer(make([]byte, 0, mtu-28)), // - headers.
109109
ackBuf: bytes.NewBuffer(make([]byte, 0, 128)),
110110
nackBuf: bytes.NewBuffer(make([]byte, 0, 64)),
111111
}
@@ -160,7 +160,7 @@ func (conn *Conn) startTicking() {
160160
_ = conn.send(&message.ConnectedPing{ClientTimestamp: timestamp()})
161161

162162
conn.mu.Lock()
163-
if t.Sub(*conn.lastActivity.Load()) > time.Second*5+conn.retransmission.rtt()*2 {
163+
if t.Sub(*conn.lastActivity.Load()) > time.Second*5+conn.retransmission.rtt(t)*2 {
164164
// No activity for too long: Start timeout.
165165
_ = conn.Close()
166166
}
@@ -196,7 +196,7 @@ func (conn *Conn) checkResend(now time.Time) {
196196

197197
var (
198198
resend []uint24
199-
rtt = conn.retransmission.rtt()
199+
rtt = conn.retransmission.rtt(now)
200200
delay = rtt + rtt/2
201201
)
202202
conn.rtt.Store(int64(rtt))

dial.go

+8-9
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,8 @@ func (dialer Dialer) DialContext(ctx context.Context, address string) (*Conn, er
219219
return nil, dialer.error("dial", err)
220220
}
221221

222-
cs := &connState{conn: conn, raddr: conn.RemoteAddr(), id: atomic.AddInt64(&dialerID, 1)}
222+
cs := &connState{conn: conn, raddr: conn.RemoteAddr(), id: atomic.AddInt64(&dialerID, 1), ticker: time.NewTicker(time.Second / 2)}
223+
defer cs.ticker.Stop()
223224
if err = cs.discoverMTU(ctx); err != nil {
224225
return nil, dialer.error("dial", err)
225226
} else if err = cs.openConnection(ctx); err != nil {
@@ -285,6 +286,8 @@ type connState struct {
285286

286287
serverSecurity bool
287288
cookie uint32
289+
290+
ticker *time.Ticker
288291
}
289292

290293
var mtuSizes = []uint16{1492, 1200, 576}
@@ -337,14 +340,12 @@ func (state *connState) discoverMTU(ctx context.Context) error {
337340
// request1 sends a message.OpenConnectionRequest1 three times for each mtu
338341
// size passed, spaced by 500ms.
339342
func (state *connState) request1(ctx context.Context, sizes []uint16) {
340-
ticker := time.NewTicker(time.Second / 2)
341-
defer ticker.Stop()
342-
343+
state.ticker.Reset(time.Second / 2)
343344
for _, size := range sizes {
344345
for range 3 {
345346
state.openConnectionRequest1(size)
346347
select {
347-
case <-ticker.C:
348+
case <-state.ticker.C:
348349
continue
349350
case <-ctx.Done():
350351
return
@@ -384,13 +385,11 @@ func (state *connState) openConnection(ctx context.Context) error {
384385

385386
// request2 continuously sends a message.OpenConnectionRequest2 every 500ms.
386387
func (state *connState) request2(ctx context.Context, mtu uint16) {
387-
ticker := time.NewTicker(time.Second / 2)
388-
defer ticker.Stop()
389-
388+
state.ticker.Reset(time.Second / 2)
390389
for {
391390
state.openConnectionRequest2(mtu)
392391
select {
393-
case <-ticker.C:
392+
case <-state.ticker.C:
394393
continue
395394
case <-ctx.Done():
396395
return

resend_map.go

+15-17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package raknet
22

33
import (
4+
"maps"
45
"time"
56
)
67

@@ -60,23 +61,20 @@ func (m *resendMap) remove(index uint24, mul int) (*packet, bool) {
6061
// rtt returns the average round trip time between the putting of the value
6162
// into the recovery queue and the taking out of it again. It is measured over
6263
// the last delayRecordCount values add in.
63-
func (m *resendMap) rtt() time.Duration {
64-
const maxRTT = time.Second * 5
65-
var (
66-
total, records time.Duration
67-
now = time.Now()
68-
)
69-
for t, rtt := range m.delays {
70-
if now.Sub(t) > maxRTT {
71-
delete(m.delays, t)
72-
continue
73-
}
74-
total += rtt
75-
records++
76-
}
77-
if records == 0 {
78-
// No records yet, generally should not happen. Just return a reasonable amount of time.
64+
func (m *resendMap) rtt(now time.Time) time.Duration {
65+
const rttCalculationWindow = time.Second * 5
66+
maps.DeleteFunc(m.delays, func(t time.Time, duration time.Duration) bool {
67+
// Remove records that are older than the max window.
68+
return now.Sub(t) > rttCalculationWindow
69+
})
70+
if len(m.delays) == 0 {
71+
// No records yet, generally should not happen. Just return a reasonable
72+
// amount of time.
7973
return time.Millisecond * 50
8074
}
81-
return total / records
75+
var total time.Duration
76+
for _, rtt := range m.delays {
77+
total += rtt
78+
}
79+
return total / time.Duration(len(m.delays))
8280
}

0 commit comments

Comments
 (0)