From 7d8ea671c82799ec2d7b79fa3cc9939811359dfb Mon Sep 17 00:00:00 2001 From: Gabriel mermelstein Date: Fri, 14 Feb 2025 16:23:04 +0200 Subject: [PATCH] moving rate limit to separate file --- waku/common/config.go | 46 -------------------------------------- waku/common/rate_limit.go | 47 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 46 deletions(-) create mode 100644 waku/common/rate_limit.go diff --git a/waku/common/config.go b/waku/common/config.go index 250349c..5026ab0 100644 --- a/waku/common/config.go +++ b/waku/common/config.go @@ -1,10 +1,5 @@ package common -import ( - "encoding/json" - "fmt" -) - type WakuConfig struct { Host string `json:"host,omitempty"` Nodekey string `json:"nodekey,omitempty"` @@ -39,44 +34,3 @@ type WakuConfig struct { TcpPort int `json:"tcpPort,omitempty"` RateLimits RateLimitsConfig `json:"rateLimits,omitempty"` } - -type RateLimitsConfig struct { - Filter *RateLimit `json:"-"` - Lightpush *RateLimit `json:"-"` - PeerExchange *RateLimit `json:"-"` -} - -type RateLimit struct { - Volume int // Number of allowed messages per period - Period int // Length of each rate-limit period (in TimeUnit) - TimeUnit RateLimitTimeUnit // Time unit of the period -} - -type RateLimitTimeUnit string - -const Hour RateLimitTimeUnit = "h" -const Minute RateLimitTimeUnit = "m" -const Second RateLimitTimeUnit = "s" -const Millisecond RateLimitTimeUnit = "ms" - -func (rl RateLimit) String() string { - return fmt.Sprintf("%d/%d%s", rl.Volume, rl.Period, rl.TimeUnit) -} - -func (rl RateLimit) MarshalJSON() ([]byte, error) { - return json.Marshal(rl.String()) -} - -func (rlc RateLimitsConfig) MarshalJSON() ([]byte, error) { - output := []string{} - if rlc.Filter != nil { - output = append(output, fmt.Sprintf("filter:%s", rlc.Filter.String())) - } - if rlc.Lightpush != nil { - output = append(output, fmt.Sprintf("lightpush:%s", rlc.Lightpush.String())) - } - if rlc.PeerExchange != nil { - output = append(output, fmt.Sprintf("px:%s", rlc.PeerExchange.String())) - } - return json.Marshal(output) -} diff --git a/waku/common/rate_limit.go b/waku/common/rate_limit.go new file mode 100644 index 0000000..246ae76 --- /dev/null +++ b/waku/common/rate_limit.go @@ -0,0 +1,47 @@ +package common + +import ( + "encoding/json" + "fmt" +) + +type RateLimitsConfig struct { + Filter *RateLimit `json:"-"` + Lightpush *RateLimit `json:"-"` + PeerExchange *RateLimit `json:"-"` +} + +type RateLimit struct { + Volume int // Number of allowed messages per period + Period int // Length of each rate-limit period (in TimeUnit) + TimeUnit RateLimitTimeUnit // Time unit of the period +} + +type RateLimitTimeUnit string + +const Hour RateLimitTimeUnit = "h" +const Minute RateLimitTimeUnit = "m" +const Second RateLimitTimeUnit = "s" +const Millisecond RateLimitTimeUnit = "ms" + +func (rl RateLimit) String() string { + return fmt.Sprintf("%d/%d%s", rl.Volume, rl.Period, rl.TimeUnit) +} + +func (rl RateLimit) MarshalJSON() ([]byte, error) { + return json.Marshal(rl.String()) +} + +func (rlc RateLimitsConfig) MarshalJSON() ([]byte, error) { + output := []string{} + if rlc.Filter != nil { + output = append(output, fmt.Sprintf("filter:%s", rlc.Filter.String())) + } + if rlc.Lightpush != nil { + output = append(output, fmt.Sprintf("lightpush:%s", rlc.Lightpush.String())) + } + if rlc.PeerExchange != nil { + output = append(output, fmt.Sprintf("px:%s", rlc.PeerExchange.String())) + } + return json.Marshal(output) +}