chore: adding rate limit config

This commit is contained in:
Gabriel mermelstein 2024-12-19 10:31:14 +01:00
parent 7eb7647599
commit 1ca365f69d
No known key found for this signature in database
GPG Key ID: 82B8134785FEAE0D

View File

@ -360,6 +360,48 @@ type WakuConfig struct {
PeerExchange bool `json:"peerExchange,omitempty"`
PeerExchangeNode string `json:"peerExchangeNode,omitempty"`
TcpPort int `json:"tcpPort,omitempty"`
RateLimits RateLimitsConfig `json:"rateLimits,omitempty"`
}
type RateLimitsConfig struct {
Filter *RateLimit `json:"-"`
Lightpush *RateLimit `json:"-"`
PeerExchange *RateLimit `json:"-"`
}
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)
}
type RateLimitUnit string
const Hour RateLimitUnit = "h"
const Minute RateLimitUnit = "m"
const Second RateLimitUnit = "s"
const Millisecond RateLimitUnit = "ms"
type RateLimit struct {
Volume int
Period int
Unit RateLimitUnit
}
func (rl RateLimit) String() string {
return fmt.Sprintf("%d/%d%s", rl.Volume, rl.Period, rl.Unit)
}
func (rl RateLimit) MarshalJSON() ([]byte, error) {
return json.Marshal(rl.String())
}
// Waku represents a dark communication interface through the Ethereum