Skip to content

Commit 5c796a3

Browse files
n8mgrChrisSchinnerl
authored andcommitted
fix rate limitter not being used
1 parent 3d03d0e commit 5c796a3

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

rhp/listener.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package rhp
22

33
import (
4+
"context"
45
"net"
56

67
"golang.org/x/time/rate"
@@ -24,7 +25,7 @@ type (
2425

2526
rhpConn struct {
2627
net.Conn
27-
28+
rl, wl *rate.Limiter
2829
monitor DataMonitor
2930
}
3031

@@ -66,6 +67,10 @@ func WithDataMonitor(m DataMonitor) Option {
6667
func (c *rhpConn) Read(b []byte) (int, error) {
6768
n, err := c.Conn.Read(b)
6869
c.monitor.ReadBytes(n)
70+
if err != nil {
71+
return n, err
72+
}
73+
c.rl.WaitN(context.Background(), len(b)) // error can be ignored since context will never be cancelled and len(b) should never exceed burst size
6974
return n, err
7075
}
7176

@@ -74,6 +79,10 @@ func (c *rhpConn) Read(b []byte) (int, error) {
7479
func (c *rhpConn) Write(b []byte) (int, error) {
7580
n, err := c.Conn.Write(b)
7681
c.monitor.WriteBytes(n)
82+
if err != nil {
83+
return n, err
84+
}
85+
c.wl.WaitN(context.Background(), len(b)) // error can be ignored since context will never be cancelled and len(b) should never exceed burst size
7786
return n, err
7887
}
7988

@@ -84,6 +93,8 @@ func (l *rhpListener) Accept() (net.Conn, error) {
8493
}
8594
return &rhpConn{
8695
Conn: c,
96+
rl: l.readLimiter,
97+
wl: l.writeLimiter,
8798
monitor: l.monitor,
8899
}, nil
89100
}

0 commit comments

Comments
 (0)