github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/services/wireguard/endpoint/netstack-provider/shaper_service.go (about) 1 /* 2 * Copyright (C) 2022 The "MysteriumNetwork/node" Authors. 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 package netstack_provider 19 20 import ( 21 "github.com/mysteriumnetwork/node/config" 22 "github.com/mysteriumnetwork/node/eventbus" 23 "github.com/rs/zerolog/log" 24 "golang.org/x/time/rate" 25 ) 26 27 const AppTopicConfigShaper = "config:shaper" 28 29 var rateLimiter *rate.Limiter 30 31 func getRateLimitter() *rate.Limiter { 32 return rateLimiter 33 } 34 35 func InitUserspaceShaper(eventBus eventbus.EventBus) { 36 applyLimits := func(e interface{}) { 37 bandwidthBytes := config.GetUInt64(config.FlagShaperBandwidth) * 1024 38 bandwidth := rate.Limit(bandwidthBytes) 39 if !config.GetBool(config.FlagShaperEnabled) { 40 bandwidth = rate.Inf 41 } 42 log.Info().Msgf("Shaper bandwidth: %v", config.GetUInt64(config.FlagShaperBandwidth)) 43 rateLimiter.SetLimit(bandwidth) 44 } 45 46 rateLimiter = rate.NewLimiter(rate.Inf, 0) 47 applyLimits(nil) 48 49 err := eventBus.SubscribeAsync(AppTopicConfigShaper, applyLimits) 50 if err != nil { 51 log.Error().Msgf("could not subscribe to topic: %v", err) 52 } 53 }