refactor(ax): improve internal naming for rate limiter and custom diff
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
b65bb76ac5
commit
066bc42a89
3 changed files with 23 additions and 23 deletions
12
proxy.go
12
proxy.go
|
|
@ -111,10 +111,10 @@ type ConfigWatcher struct {
|
|||
// rl := proxy.NewRateLimiter(cfg.RateLimit)
|
||||
// if !rl.Allow("1.2.3.4") { conn.Close(); return }
|
||||
type RateLimiter struct {
|
||||
limitConfig RateLimit
|
||||
buckets map[string]*tokenBucket
|
||||
banned map[string]time.Time
|
||||
mu sync.Mutex
|
||||
config RateLimit
|
||||
buckets map[string]*tokenBucket
|
||||
banned map[string]time.Time
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
// tokenBucket is a simple token bucket for one IP.
|
||||
|
|
@ -129,8 +129,8 @@ type tokenBucket struct {
|
|||
// cd := proxy.NewCustomDiff(cfg.CustomDiff)
|
||||
// bus.Subscribe(proxy.EventLogin, cd.OnLogin)
|
||||
type CustomDiff struct {
|
||||
globalDifficulty uint64
|
||||
mu sync.RWMutex
|
||||
globalDiff uint64
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
var splitterFactories = map[string]func(*Config, *EventBus) Splitter{
|
||||
|
|
|
|||
|
|
@ -48,7 +48,7 @@ func TestProxy_Reload_Good(t *testing.T) {
|
|||
if proxyValue.config.CustomDiff != 250 {
|
||||
t.Fatalf("expected custom diff to reload, got %d", proxyValue.config.CustomDiff)
|
||||
}
|
||||
if proxyValue.customDifficulty == nil || proxyValue.customDifficulty.globalDifficulty != 250 {
|
||||
if proxyValue.customDifficulty == nil || proxyValue.customDifficulty.globalDiff != 250 {
|
||||
t.Fatalf("expected live custom diff to update, got %+v", proxyValue.customDifficulty)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -11,9 +11,9 @@ import (
|
|||
// rl := proxy.NewRateLimiter(proxy.RateLimit{MaxConnectionsPerMinute: 30, BanDurationSeconds: 300})
|
||||
func NewRateLimiter(config RateLimit) *RateLimiter {
|
||||
return &RateLimiter{
|
||||
limitConfig: config,
|
||||
buckets: make(map[string]*tokenBucket),
|
||||
banned: make(map[string]time.Time),
|
||||
config: config,
|
||||
buckets: make(map[string]*tokenBucket),
|
||||
banned: make(map[string]time.Time),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -26,7 +26,7 @@ func (rateLimiter *RateLimiter) SetConfig(config RateLimit) {
|
|||
}
|
||||
|
||||
rateLimiter.mu.Lock()
|
||||
rateLimiter.limitConfig = config
|
||||
rateLimiter.config = config
|
||||
rateLimiter.mu.Unlock()
|
||||
}
|
||||
|
||||
|
|
@ -44,7 +44,7 @@ func (rateLimiter *RateLimiter) Allow(ip string) bool {
|
|||
rateLimiter.mu.Lock()
|
||||
defer rateLimiter.mu.Unlock()
|
||||
|
||||
if rateLimiter.limitConfig.MaxConnectionsPerMinute <= 0 {
|
||||
if rateLimiter.config.MaxConnectionsPerMinute <= 0 {
|
||||
return true
|
||||
}
|
||||
|
||||
|
|
@ -58,7 +58,7 @@ func (rateLimiter *RateLimiter) Allow(ip string) bool {
|
|||
bucket, exists := rateLimiter.buckets[host]
|
||||
if !exists {
|
||||
bucket = &tokenBucket{
|
||||
tokens: rateLimiter.limitConfig.MaxConnectionsPerMinute,
|
||||
tokens: rateLimiter.config.MaxConnectionsPerMinute,
|
||||
lastRefill: now,
|
||||
}
|
||||
rateLimiter.buckets[host] = bucket
|
||||
|
|
@ -66,8 +66,8 @@ func (rateLimiter *RateLimiter) Allow(ip string) bool {
|
|||
|
||||
rateLimiter.refillBucket(bucket, now)
|
||||
if bucket.tokens <= 0 {
|
||||
if rateLimiter.limitConfig.BanDurationSeconds > 0 {
|
||||
rateLimiter.banned[host] = now.Add(time.Duration(rateLimiter.limitConfig.BanDurationSeconds) * time.Second)
|
||||
if rateLimiter.config.BanDurationSeconds > 0 {
|
||||
rateLimiter.banned[host] = now.Add(time.Duration(rateLimiter.config.BanDurationSeconds) * time.Second)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
|
@ -88,7 +88,7 @@ func (rateLimiter *RateLimiter) Tick() {
|
|||
rateLimiter.mu.Lock()
|
||||
defer rateLimiter.mu.Unlock()
|
||||
|
||||
if rateLimiter.limitConfig.MaxConnectionsPerMinute <= 0 {
|
||||
if rateLimiter.config.MaxConnectionsPerMinute <= 0 {
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -104,11 +104,11 @@ func (rateLimiter *RateLimiter) Tick() {
|
|||
}
|
||||
|
||||
func (rateLimiter *RateLimiter) refillBucket(bucket *tokenBucket, now time.Time) {
|
||||
if bucket == nil || rateLimiter.limitConfig.MaxConnectionsPerMinute <= 0 {
|
||||
if bucket == nil || rateLimiter.config.MaxConnectionsPerMinute <= 0 {
|
||||
return
|
||||
}
|
||||
|
||||
refillEvery := time.Minute / time.Duration(rateLimiter.limitConfig.MaxConnectionsPerMinute)
|
||||
refillEvery := time.Minute / time.Duration(rateLimiter.config.MaxConnectionsPerMinute)
|
||||
if refillEvery <= 0 {
|
||||
refillEvery = time.Second
|
||||
}
|
||||
|
|
@ -120,8 +120,8 @@ func (rateLimiter *RateLimiter) refillBucket(bucket *tokenBucket, now time.Time)
|
|||
|
||||
tokensToAdd := int(elapsed / refillEvery)
|
||||
bucket.tokens += tokensToAdd
|
||||
if bucket.tokens > rateLimiter.limitConfig.MaxConnectionsPerMinute {
|
||||
bucket.tokens = rateLimiter.limitConfig.MaxConnectionsPerMinute
|
||||
if bucket.tokens > rateLimiter.config.MaxConnectionsPerMinute {
|
||||
bucket.tokens = rateLimiter.config.MaxConnectionsPerMinute
|
||||
}
|
||||
bucket.lastRefill = bucket.lastRefill.Add(time.Duration(tokensToAdd) * refillEvery)
|
||||
}
|
||||
|
|
@ -130,7 +130,7 @@ func (rateLimiter *RateLimiter) refillBucket(bucket *tokenBucket, now time.Time)
|
|||
//
|
||||
// cd := proxy.NewCustomDiff(50000)
|
||||
func NewCustomDiff(globalDiff uint64) *CustomDiff {
|
||||
return &CustomDiff{globalDifficulty: globalDiff}
|
||||
return &CustomDiff{globalDiff: globalDiff}
|
||||
}
|
||||
|
||||
// SetGlobalDiff updates the default custom difficulty override.
|
||||
|
|
@ -142,7 +142,7 @@ func (customDiff *CustomDiff) SetGlobalDiff(globalDiff uint64) {
|
|||
}
|
||||
|
||||
customDiff.mu.Lock()
|
||||
customDiff.globalDifficulty = globalDiff
|
||||
customDiff.globalDiff = globalDiff
|
||||
customDiff.mu.Unlock()
|
||||
}
|
||||
|
||||
|
|
@ -172,7 +172,7 @@ func (customDiff *CustomDiff) OnLogin(event Event) {
|
|||
}
|
||||
|
||||
customDiff.mu.RLock()
|
||||
globalDiff := customDiff.globalDifficulty
|
||||
globalDiff := customDiff.globalDiff
|
||||
customDiff.mu.RUnlock()
|
||||
if globalDiff > 0 {
|
||||
event.Miner.SetCustomDiff(globalDiff)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue