From 1ca365f69d1da3f1b63f756f6d0999132f893256 Mon Sep 17 00:00:00 2001 From: Gabriel mermelstein Date: Thu, 19 Dec 2024 10:31:14 +0100 Subject: [PATCH] chore: adding rate limit config --- waku/nwaku.go | 102 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 72 insertions(+), 30 deletions(-) diff --git a/waku/nwaku.go b/waku/nwaku.go index b6ab7d7..6cb0026 100644 --- a/waku/nwaku.go +++ b/waku/nwaku.go @@ -330,36 +330,78 @@ const requestTimeout = 30 * time.Second const MsgChanBufferSize = 100 type WakuConfig struct { - Host string `json:"host,omitempty"` - Nodekey string `json:"nodekey,omitempty"` - Relay bool `json:"relay,omitempty"` - Store bool `json:"store,omitempty"` - Storenode string `json:"storenode,omitempty"` - StoreMessageRetentionPolicy string `json:"storeMessageRetentionPolicy,omitempty"` - StoreMessageDbUrl string `json:"storeMessageDbUrl,omitempty"` - StoreMessageDbVacuum bool `json:"storeMessageDbVacuum,omitempty"` - StoreMaxNumDbConnections int `json:"storeMaxNumDbConnections,omitempty"` - StoreResume bool `json:"storeResume,omitempty"` - Filter bool `json:"filter,omitempty"` - Filternode string `json:"filternode,omitempty"` - FilterSubscriptionTimeout int64 `json:"filterSubscriptionTimeout,omitempty"` - FilterMaxPeersToServe uint32 `json:"filterMaxPeersToServe,omitempty"` - FilterMaxCriteria uint32 `json:"filterMaxCriteria,omitempty"` - Lightpush bool `json:"lightpush,omitempty"` - LightpushNode string `json:"lightpushnode,omitempty"` - LogLevel string `json:"logLevel,omitempty"` - DnsDiscovery bool `json:"dnsDiscovery,omitempty"` - DnsDiscoveryUrl string `json:"dnsDiscoveryUrl,omitempty"` - MaxMessageSize string `json:"maxMessageSize,omitempty"` - Staticnodes []string `json:"staticnodes,omitempty"` - Discv5BootstrapNodes []string `json:"discv5BootstrapNodes,omitempty"` - Discv5Discovery bool `json:"discv5Discovery,omitempty"` - Discv5UdpPort int `json:"discv5UdpPort,omitempty"` - ClusterID uint16 `json:"clusterId,omitempty"` - Shards []uint16 `json:"shards,omitempty"` - PeerExchange bool `json:"peerExchange,omitempty"` - PeerExchangeNode string `json:"peerExchangeNode,omitempty"` - TcpPort int `json:"tcpPort,omitempty"` + Host string `json:"host,omitempty"` + Nodekey string `json:"nodekey,omitempty"` + Relay bool `json:"relay,omitempty"` + Store bool `json:"store,omitempty"` + Storenode string `json:"storenode,omitempty"` + StoreMessageRetentionPolicy string `json:"storeMessageRetentionPolicy,omitempty"` + StoreMessageDbUrl string `json:"storeMessageDbUrl,omitempty"` + StoreMessageDbVacuum bool `json:"storeMessageDbVacuum,omitempty"` + StoreMaxNumDbConnections int `json:"storeMaxNumDbConnections,omitempty"` + StoreResume bool `json:"storeResume,omitempty"` + Filter bool `json:"filter,omitempty"` + Filternode string `json:"filternode,omitempty"` + FilterSubscriptionTimeout int64 `json:"filterSubscriptionTimeout,omitempty"` + FilterMaxPeersToServe uint32 `json:"filterMaxPeersToServe,omitempty"` + FilterMaxCriteria uint32 `json:"filterMaxCriteria,omitempty"` + Lightpush bool `json:"lightpush,omitempty"` + LightpushNode string `json:"lightpushnode,omitempty"` + LogLevel string `json:"logLevel,omitempty"` + DnsDiscovery bool `json:"dnsDiscovery,omitempty"` + DnsDiscoveryUrl string `json:"dnsDiscoveryUrl,omitempty"` + MaxMessageSize string `json:"maxMessageSize,omitempty"` + Staticnodes []string `json:"staticnodes,omitempty"` + Discv5BootstrapNodes []string `json:"discv5BootstrapNodes,omitempty"` + Discv5Discovery bool `json:"discv5Discovery,omitempty"` + Discv5UdpPort int `json:"discv5UdpPort,omitempty"` + ClusterID uint16 `json:"clusterId,omitempty"` + Shards []uint16 `json:"shards,omitempty"` + 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