Skip to content

Commit ff2990c

Browse files
authored
Merge pull request #159 from SenseUnit/new_rl
Shave down ratelimit memory consumption
2 parents e711679 + 2c729b6 commit ff2990c

File tree

7 files changed

+1038
-14
lines changed

7 files changed

+1038
-14
lines changed

forward/bwlimit.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,25 @@ import (
77
"time"
88

99
"github.com/zeebo/xxh3"
10-
"golang.org/x/time/rate"
10+
11+
"github.com/SenseUnit/dumbproxy/rate"
1112
)
1213

1314
const copyChunkSize = 128 * 1024
1415

1516
type BWLimit struct {
16-
d []rate.Limiter
17-
u []rate.Limiter
17+
limit rate.Limit
18+
burst int64
19+
d []rate.Limiter
20+
u []rate.Limiter
1821
}
1922

2023
func NewBWLimit(bytesPerSecond float64, burst int64, buckets uint, separate bool) *BWLimit {
2124
if buckets == 0 {
2225
buckets = 1
2326
}
24-
lim := *(rate.NewLimiter(rate.Limit(bytesPerSecond), max(copyChunkSize, burst)))
27+
burst = max(copyChunkSize, burst)
28+
lim := *(rate.NewLimiter(burst))
2529
d := make([]rate.Limiter, buckets)
2630
for i := range d {
2731
d[i] = lim
@@ -34,8 +38,10 @@ func NewBWLimit(bytesPerSecond float64, burst int64, buckets uint, separate bool
3438
}
3539
}
3640
return &BWLimit{
37-
d: d,
38-
u: u,
41+
limit: rate.Limit(bytesPerSecond),
42+
burst: burst,
43+
d: d,
44+
u: u,
3945
}
4046
}
4147

@@ -47,7 +53,7 @@ func (l *BWLimit) copy(ctx context.Context, rl *rate.Limiter, dst io.Writer, src
4753
var n int64
4854
for {
4955
t := time.Now()
50-
r := rl.ReserveN(t, copyChunkSize)
56+
r := rl.ReserveN(l.limit, l.burst, t, copyChunkSize)
5157
if !r.OK() {
5258
err = errors.New("can't get rate limit reservation")
5359
return
@@ -64,9 +70,9 @@ func (l *BWLimit) copy(ctx context.Context, rl *rate.Limiter, dst io.Writer, src
6470
n, err = io.Copy(dst, lim)
6571
written += n
6672
if n < copyChunkSize {
67-
r.CancelAt(t)
73+
r.CancelAt(l.limit, l.burst, t)
6874
if n > 0 {
69-
rl.ReserveN(t, n)
75+
rl.ReserveN(l.limit, l.burst, t, n)
7076
}
7177
}
7278
if err != nil {

go.mod

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ require (
2121
golang.org/x/crypto/x509roots/fallback v0.0.0-20251028130051-c0531f9c3451
2222
golang.org/x/net v0.46.0
2323
golang.org/x/sync v0.18.0
24-
golang.org/x/time v0.14.0
2524
)
2625

2726
require (
@@ -40,5 +39,3 @@ require (
4039
golang.org/x/term v0.36.0 // indirect
4140
golang.org/x/text v0.30.0 // indirect
4241
)
43-
44-
replace golang.org/x/time => github.com/Snawoot/xtime v0.0.0-20250501122004-d1ce456948bb

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0
44
github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
55
github.com/Snawoot/uniqueslice v0.1.1 h1:KEfv3FtAXiNEoxvcc79pFQDhnqwYXQyZIkxOM4e/qpw=
66
github.com/Snawoot/uniqueslice v0.1.1/go.mod h1:K9zIaHO43FGLHbqm6WCDFeY6+CN/du5eiio/vxvDVC8=
7-
github.com/Snawoot/xtime v0.0.0-20250501122004-d1ce456948bb h1:PleTDwc/EQenzLsvIal2BgvIXr2D214M88RFac3WkeI=
8-
github.com/Snawoot/xtime v0.0.0-20250501122004-d1ce456948bb/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg=
97
github.com/andybalholm/brotli v1.2.0 h1:ukwgCxwYrmACq68yiUqwIWnGY0cTPox/M94sVwToPjQ=
108
github.com/andybalholm/brotli v1.2.0/go.mod h1:rzTDkvFWvIrjDXZHkuS16NPggd91W3kUSvPlQ1pLaKY=
119
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=

rate/LICENSE

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Copyright 2009 The Go Authors.
2+
3+
Redistribution and use in source and binary forms, with or without
4+
modification, are permitted provided that the following conditions are
5+
met:
6+
7+
* Redistributions of source code must retain the above copyright
8+
notice, this list of conditions and the following disclaimer.
9+
* Redistributions in binary form must reproduce the above
10+
copyright notice, this list of conditions and the following disclaimer
11+
in the documentation and/or other materials provided with the
12+
distribution.
13+
* Neither the name of Google LLC nor the names of its
14+
contributors may be used to endorse or promote products derived from
15+
this software without specific prior written permission.
16+
17+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

rate/PATENTS

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Additional IP Rights Grant (Patents)
2+
3+
"This implementation" means the copyrightable works distributed by
4+
Google as part of the Go project.
5+
6+
Google hereby grants to You a perpetual, worldwide, non-exclusive,
7+
no-charge, royalty-free, irrevocable (except as stated in this section)
8+
patent license to make, have made, use, offer to sell, sell, import,
9+
transfer and otherwise run, modify and propagate the contents of this
10+
implementation of Go, where such license applies only to those patent
11+
claims, both currently owned or controlled by Google and acquired in
12+
the future, licensable by Google that are necessarily infringed by this
13+
implementation of Go. This grant does not include claims that would be
14+
infringed only as a consequence of further modification of this
15+
implementation. If you or your agent or exclusive licensee institute or
16+
order or agree to the institution of patent litigation against any
17+
entity (including a cross-claim or counterclaim in a lawsuit) alleging
18+
that this implementation of Go or any code incorporated within this
19+
implementation of Go constitutes direct or contributory patent
20+
infringement, or inducement of patent infringement, then any patent
21+
rights granted to you under this License for this implementation of Go
22+
shall terminate as of the date such litigation is filed.

0 commit comments

Comments
 (0)