File tree Expand file tree Collapse file tree 1 file changed +12
-1
lines changed Expand file tree Collapse file tree 1 file changed +12
-1
lines changed Original file line number Diff line number Diff line change 1
1
package rhp
2
2
3
3
import (
4
+ "context"
4
5
"net"
5
6
6
7
"golang.org/x/time/rate"
24
25
25
26
rhpConn struct {
26
27
net.Conn
27
-
28
+ rl , wl * rate. Limiter
28
29
monitor DataMonitor
29
30
}
30
31
@@ -66,6 +67,10 @@ func WithDataMonitor(m DataMonitor) Option {
66
67
func (c * rhpConn ) Read (b []byte ) (int , error ) {
67
68
n , err := c .Conn .Read (b )
68
69
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
69
74
return n , err
70
75
}
71
76
@@ -74,6 +79,10 @@ func (c *rhpConn) Read(b []byte) (int, error) {
74
79
func (c * rhpConn ) Write (b []byte ) (int , error ) {
75
80
n , err := c .Conn .Write (b )
76
81
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
77
86
return n , err
78
87
}
79
88
@@ -84,6 +93,8 @@ func (l *rhpListener) Accept() (net.Conn, error) {
84
93
}
85
94
return & rhpConn {
86
95
Conn : c ,
96
+ rl : l .readLimiter ,
97
+ wl : l .writeLimiter ,
87
98
monitor : l .monitor ,
88
99
}, nil
89
100
}
You can’t perform that action at this time.
0 commit comments