From 33d41338ef00ff20e444e03f3d9ee7b60fc787a4 Mon Sep 17 00:00:00 2001 From: Larvan2 <78135608+Larvan2@users.noreply.github.com> Date: Sun, 17 Sep 2023 17:05:13 +0800 Subject: [PATCH] migration: go 1.21 --- adapter/adapter.go | 12 ++++++------ common/observable/observable_test.go | 6 +++--- go.mod | 2 +- hub/updater/limitedreader.go | 13 +------------ transport/tuic/congestion/cubic.go | 2 +- transport/tuic/congestion/cubic_sender.go | 4 ++-- .../tuic/congestion/hybrid_slow_start.go | 4 ++-- transport/tuic/congestion/minmax.go | 2 +- transport/tuic/congestion/minmax_go120.go | 19 ------------------- transport/tuic/congestion/minmax_go121.go | 13 ------------- transport/tuic/congestion/pacer.go | 6 +++--- 11 files changed, 20 insertions(+), 63 deletions(-) delete mode 100644 transport/tuic/congestion/minmax_go120.go delete mode 100644 transport/tuic/congestion/minmax_go121.go diff --git a/adapter/adapter.go b/adapter/adapter.go index 13f7f06f..abef3d89 100644 --- a/adapter/adapter.go +++ b/adapter/adapter.go @@ -136,21 +136,21 @@ func (p *Proxy) ExtraDelayHistory() map[string][]C.DelayHistory { // LastDelay return last history record. if proxy is not alive, return the max value of uint16. // implements C.Proxy func (p *Proxy) LastDelay() (delay uint16) { - var max uint16 = 0xffff + var maxDelay uint16 = 0xffff if !p.alive.Load() { - return max + return maxDelay } history := p.history.Last() if history.Delay == 0 { - return max + return maxDelay } return history.Delay } // LastDelayForTestUrl implements C.Proxy func (p *Proxy) LastDelayForTestUrl(url string) (delay uint16) { - var max uint16 = 0xffff + var maxDelay uint16 = 0xffff alive := p.alive.Load() history := p.history.Last() @@ -161,11 +161,11 @@ func (p *Proxy) LastDelayForTestUrl(url string) (delay uint16) { } if !alive { - return max + return maxDelay } if history.Delay == 0 { - return max + return maxDelay } return history.Delay } diff --git a/common/observable/observable_test.go b/common/observable/observable_test.go index 5a02273d..3f0b0e7f 100644 --- a/common/observable/observable_test.go +++ b/common/observable/observable_test.go @@ -85,16 +85,16 @@ func TestObservable_UnSubscribeWithNotExistSubscription(t *testing.T) { func TestObservable_SubscribeGoroutineLeak(t *testing.T) { iter := iterator[int]([]int{1, 2, 3, 4, 5}) src := NewObservable[int](iter) - max := 100 + total := 100 var list []Subscription[int] - for i := 0; i < max; i++ { + for i := 0; i < total; i++ { ch, _ := src.Subscribe() list = append(list, ch) } var wg sync.WaitGroup - wg.Add(max) + wg.Add(total) waitCh := func(ch <-chan int) { for range ch { } diff --git a/go.mod b/go.mod index f75f58fd..ca98937b 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/Dreamacro/clash -go 1.20 +go 1.21 require ( github.com/3andne/restls-client-go v0.1.6 diff --git a/hub/updater/limitedreader.go b/hub/updater/limitedreader.go index c31db601..52d7e969 100644 --- a/hub/updater/limitedreader.go +++ b/hub/updater/limitedreader.go @@ -3,8 +3,6 @@ package updater import ( "fmt" "io" - - "golang.org/x/exp/constraints" ) // LimitReachedError records the limit and the operation that caused it. @@ -35,7 +33,7 @@ func (lr *limitedReader) Read(p []byte) (n int, err error) { } } - p = p[:Min(lr.n, int64(len(p)))] + p = p[:min(lr.n, int64(len(p)))] n, err = lr.r.Read(p) lr.n -= int64(n) @@ -56,12 +54,3 @@ func LimitReader(r io.Reader, n int64) (limited io.Reader, err error) { n: n, }, nil } - -// Min returns the smaller of x or y. -func Min[T constraints.Integer | ~string](x, y T) (res T) { - if x < y { - return x - } - - return y -} diff --git a/transport/tuic/congestion/cubic.go b/transport/tuic/congestion/cubic.go index dd491a32..7dbe2b06 100644 --- a/transport/tuic/congestion/cubic.go +++ b/transport/tuic/congestion/cubic.go @@ -186,7 +186,7 @@ func (c *Cubic) CongestionWindowAfterAck( targetCongestionWindow = c.originPointCongestionWindow - deltaCongestionWindow } // Limit the CWND increase to half the acked bytes. - targetCongestionWindow = Min(targetCongestionWindow, currentCongestionWindow+c.ackedBytesCount/2) + targetCongestionWindow = min(targetCongestionWindow, currentCongestionWindow+c.ackedBytesCount/2) // Increase the window by approximately Alpha * 1 MSS of bytes every // time we ack an estimated tcp window of bytes. For small diff --git a/transport/tuic/congestion/cubic_sender.go b/transport/tuic/congestion/cubic_sender.go index ca20b420..a467b8c9 100644 --- a/transport/tuic/congestion/cubic_sender.go +++ b/transport/tuic/congestion/cubic_sender.go @@ -177,7 +177,7 @@ func (c *cubicSender) OnPacketAcked( priorInFlight congestion.ByteCount, eventTime time.Time, ) { - c.largestAckedPacketNumber = Max(ackedPacketNumber, c.largestAckedPacketNumber) + c.largestAckedPacketNumber = max(ackedPacketNumber, c.largestAckedPacketNumber) if c.InRecovery() { return } @@ -245,7 +245,7 @@ func (c *cubicSender) maybeIncreaseCwnd( c.numAckedPackets = 0 } } else { - c.congestionWindow = Min(c.maxCongestionWindow(), c.cubic.CongestionWindowAfterAck(ackedBytes, c.congestionWindow, c.rttStats.MinRTT(), eventTime)) + c.congestionWindow = min(c.maxCongestionWindow(), c.cubic.CongestionWindowAfterAck(ackedBytes, c.congestionWindow, c.rttStats.MinRTT(), eventTime)) } } diff --git a/transport/tuic/congestion/hybrid_slow_start.go b/transport/tuic/congestion/hybrid_slow_start.go index 7586774e..e0b42e53 100644 --- a/transport/tuic/congestion/hybrid_slow_start.go +++ b/transport/tuic/congestion/hybrid_slow_start.go @@ -74,8 +74,8 @@ func (s *HybridSlowStart) ShouldExitSlowStart(latestRTT time.Duration, minRTT ti // Divide minRTT by 8 to get a rtt increase threshold for exiting. minRTTincreaseThresholdUs := int64(minRTT / time.Microsecond >> hybridStartDelayFactorExp) // Ensure the rtt threshold is never less than 2ms or more than 16ms. - minRTTincreaseThresholdUs = Min(minRTTincreaseThresholdUs, hybridStartDelayMaxThresholdUs) - minRTTincreaseThreshold := time.Duration(Max(minRTTincreaseThresholdUs, hybridStartDelayMinThresholdUs)) * time.Microsecond + minRTTincreaseThresholdUs = min(minRTTincreaseThresholdUs, hybridStartDelayMaxThresholdUs) + minRTTincreaseThreshold := time.Duration(max(minRTTincreaseThresholdUs, hybridStartDelayMinThresholdUs)) * time.Microsecond if s.currentMinRTT > (minRTT + minRTTincreaseThreshold) { s.hystartFound = true diff --git a/transport/tuic/congestion/minmax.go b/transport/tuic/congestion/minmax.go index 0a8f4ad4..50667766 100644 --- a/transport/tuic/congestion/minmax.go +++ b/transport/tuic/congestion/minmax.go @@ -16,7 +16,7 @@ func MinNonZeroDuration(a, b time.Duration) time.Duration { if b == 0 { return a } - return Min(a, b) + return min(a, b) } // AbsDuration returns the absolute value of a time duration diff --git a/transport/tuic/congestion/minmax_go120.go b/transport/tuic/congestion/minmax_go120.go deleted file mode 100644 index 1266edbc..00000000 --- a/transport/tuic/congestion/minmax_go120.go +++ /dev/null @@ -1,19 +0,0 @@ -//go:build !go1.21 - -package congestion - -import "golang.org/x/exp/constraints" - -func Max[T constraints.Ordered](a, b T) T { - if a < b { - return b - } - return a -} - -func Min[T constraints.Ordered](a, b T) T { - if a < b { - return a - } - return b -} diff --git a/transport/tuic/congestion/minmax_go121.go b/transport/tuic/congestion/minmax_go121.go deleted file mode 100644 index 65b06726..00000000 --- a/transport/tuic/congestion/minmax_go121.go +++ /dev/null @@ -1,13 +0,0 @@ -//go:build go1.21 - -package congestion - -import "cmp" - -func Max[T cmp.Ordered](a, b T) T { - return max(a, b) -} - -func Min[T cmp.Ordered](a, b T) T { - return min(a, b) -} diff --git a/transport/tuic/congestion/pacer.go b/transport/tuic/congestion/pacer.go index f60ef5fe..9e85107d 100644 --- a/transport/tuic/congestion/pacer.go +++ b/transport/tuic/congestion/pacer.go @@ -52,11 +52,11 @@ func (p *pacer) Budget(now time.Time) congestion.ByteCount { return p.maxBurstSize() } budget := p.budgetAtLastSent + (congestion.ByteCount(p.getAdjustedBandwidth())*congestion.ByteCount(now.Sub(p.lastSentTime).Nanoseconds()))/1e9 - return Min(p.maxBurstSize(), budget) + return min(p.maxBurstSize(), budget) } func (p *pacer) maxBurstSize() congestion.ByteCount { - return Max( + return max( congestion.ByteCount(uint64((MinPacingDelay+TimerGranularity).Nanoseconds())*p.getAdjustedBandwidth())/1e9, maxBurstSizePackets*p.maxDatagramSize, ) @@ -68,7 +68,7 @@ func (p *pacer) TimeUntilSend() time.Time { if p.budgetAtLastSent >= p.maxDatagramSize { return time.Time{} } - return p.lastSentTime.Add(Max( + return p.lastSentTime.Add(max( MinPacingDelay, time.Duration(math.Ceil(float64(p.maxDatagramSize-p.budgetAtLastSent)*1e9/float64(p.getAdjustedBandwidth())))*time.Nanosecond, ))