mirror of
https://github.com/logos-messaging/logos-messaging-go-bindings.git
synced 2026-01-04 06:53:11 +00:00
moving rate limit to separate file
This commit is contained in:
parent
cdeef8197f
commit
7d8ea671c8
@ -1,10 +1,5 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
)
|
|
||||||
|
|
||||||
type WakuConfig struct {
|
type WakuConfig struct {
|
||||||
Host string `json:"host,omitempty"`
|
Host string `json:"host,omitempty"`
|
||||||
Nodekey string `json:"nodekey,omitempty"`
|
Nodekey string `json:"nodekey,omitempty"`
|
||||||
@ -39,44 +34,3 @@ type WakuConfig struct {
|
|||||||
TcpPort int `json:"tcpPort,omitempty"`
|
TcpPort int `json:"tcpPort,omitempty"`
|
||||||
RateLimits RateLimitsConfig `json:"rateLimits,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)
|
|
||||||
}
|
|
||||||
|
|||||||
47
waku/common/rate_limit.go
Normal file
47
waku/common/rate_limit.go
Normal file
@ -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)
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user