Skip to content

Commit

Permalink
use atomic for current mtu
Browse files Browse the repository at this point in the history
  • Loading branch information
chungthuang committed Dec 4, 2023
1 parent 6a30c0c commit 183ae63
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions mtu_discoverer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package quic

import (
"net"
"sync/atomic"
"time"

"github.com/quic-go/quic-go/internal/ackhandler"
Expand Down Expand Up @@ -47,23 +48,25 @@ type mtuFinder struct {

rttStats *utils.RTTStats
inFlight protocol.ByteCount // the size of the probe packet currently in flight. InvalidByteCount if none is in flight
current protocol.ByteCount
current *atomic.Int64
max protocol.ByteCount // the maximum value, as advertised by the peer (or our maximum size buffer)
}

var _ mtuDiscoverer = &mtuFinder{}

func newMTUDiscoverer(rttStats *utils.RTTStats, start protocol.ByteCount, mtuIncreased func(protocol.ByteCount)) *mtuFinder {
var current atomic.Int64
current.Store(int64(start))
return &mtuFinder{
inFlight: protocol.InvalidByteCount,
current: start,
current: &current,
rttStats: rttStats,
mtuIncreased: mtuIncreased,
}
}

func (f *mtuFinder) done() bool {
return f.max-f.current <= maxMTUDiff+1
return f.max-f.CurrentSize() <= maxMTUDiff+1
}

func (f *mtuFinder) Start(maxPacketSize protocol.ByteCount) {
Expand All @@ -82,7 +85,7 @@ func (f *mtuFinder) ShouldSendProbe(now time.Time) bool {
}

func (f *mtuFinder) GetPing() (ackhandler.Frame, protocol.ByteCount) {
size := (f.max + f.current) / 2
size := (f.max + f.CurrentSize()) / 2
f.lastProbeTime = time.Now()
f.inFlight = size
return ackhandler.Frame{
Expand All @@ -92,7 +95,7 @@ func (f *mtuFinder) GetPing() (ackhandler.Frame, protocol.ByteCount) {
}

func (f *mtuFinder) CurrentSize() protocol.ByteCount {
return f.current
return protocol.ByteCount(f.current.Load())
}

type mtuFinderAckHandler mtuFinder
Expand All @@ -105,7 +108,7 @@ func (h *mtuFinderAckHandler) OnAcked(wire.Frame) {
panic("OnAcked callback called although there's no MTU probe packet in flight")
}
h.inFlight = protocol.InvalidByteCount
h.current = size
h.current.Store(int64(size))
h.mtuIncreased(size)
}

Expand Down

0 comments on commit 183ae63

Please sign in to comment.