18
18
package netstack_provider
19
19
20
20
import (
21
+ "time"
22
+
21
23
"github.com/mysteriumnetwork/node/config"
22
24
"github.com/mysteriumnetwork/node/eventbus"
23
25
"github.com/rs/zerolog/log"
24
26
"golang.org/x/time/rate"
25
27
)
26
28
27
- const AppTopicConfigShaper = "config:shaper"
29
+ const (
30
+ AppTopicConfigShaper = "config:shaper"
31
+ BurstLimit = 1000 * 1000 * 1000
32
+ )
28
33
29
34
var rateLimiter * rate.Limiter
30
35
@@ -33,7 +38,7 @@ func getRateLimitter() *rate.Limiter {
33
38
}
34
39
35
40
func InitUserspaceShaper (eventBus eventbus.EventBus ) {
36
- applyLimits := func (e interface {}) {
41
+ applyLimits := func (_ interface {}) {
37
42
bandwidthBytes := config .GetUInt64 (config .FlagShaperBandwidth ) * 1024
38
43
bandwidth := rate .Limit (bandwidthBytes )
39
44
if ! config .GetBool (config .FlagShaperEnabled ) {
@@ -43,9 +48,14 @@ func InitUserspaceShaper(eventBus eventbus.EventBus) {
43
48
rateLimiter .SetLimit (bandwidth )
44
49
}
45
50
46
- rateLimiter = rate .NewLimiter (rate .Inf , 0 )
51
+ rateLimiter = rate .NewLimiter (rate .Inf , BurstLimit )
52
+ rateLimiter .AllowN (time .Now (), BurstLimit ) // spend initial burst
53
+
47
54
applyLimits (nil )
48
55
56
+ if eventBus == nil {
57
+ return
58
+ }
49
59
err := eventBus .SubscribeAsync (AppTopicConfigShaper , applyLimits )
50
60
if err != nil {
51
61
log .Error ().Msgf ("could not subscribe to topic: %v" , err )
0 commit comments