refactor(handlers): migrate remaining backend handlers to clock.Now() and transaction patterns
Apply clock.Now() migration, transaction wrapping, and minor refactors across admin, scheduling, today, user, auth handler, notifications, webhooks, services, portfolio, ratelimit, testutils, and main.go. Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
+24
-8
@@ -4,7 +4,9 @@
|
||||
package mw
|
||||
|
||||
import (
|
||||
"crussell/clock"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"sync"
|
||||
"net"
|
||||
@@ -28,8 +30,15 @@ func NewRateLimiter(limit int, window time.Duration) *RateLimiter {
|
||||
// Cleanup old entries periodically
|
||||
go func() {
|
||||
for {
|
||||
time.Sleep(window)
|
||||
rl.cleanup()
|
||||
func() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
log.Printf("Panic recovered in rate limiter cleanup: %v", r)
|
||||
}
|
||||
}()
|
||||
time.Sleep(window)
|
||||
rl.cleanup()
|
||||
}()
|
||||
}
|
||||
}()
|
||||
return rl
|
||||
@@ -38,7 +47,7 @@ func NewRateLimiter(limit int, window time.Duration) *RateLimiter {
|
||||
func (rl *RateLimiter) cleanup() {
|
||||
rl.mu.Lock()
|
||||
defer rl.mu.Unlock()
|
||||
now := time.Now()
|
||||
now := clock.Now()
|
||||
for key, times := range rl.requests {
|
||||
var valid []time.Time
|
||||
for _, t := range times {
|
||||
@@ -57,7 +66,7 @@ func (rl *RateLimiter) cleanup() {
|
||||
func (rl *RateLimiter) Allow(key string) bool {
|
||||
rl.mu.Lock()
|
||||
defer rl.mu.Unlock()
|
||||
now := time.Now()
|
||||
now := clock.Now()
|
||||
windowStart := now.Add(-rl.window)
|
||||
|
||||
var valid []time.Time
|
||||
@@ -94,8 +103,15 @@ func NewProgressiveRateLimiter() *ProgressiveRateLimiter {
|
||||
}
|
||||
go func() {
|
||||
for {
|
||||
time.Sleep(30 * time.Second)
|
||||
prl.cleanup()
|
||||
func() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
log.Printf("Panic recovered in progressive rate limiter cleanup: %v", r)
|
||||
}
|
||||
}()
|
||||
time.Sleep(30 * time.Second)
|
||||
prl.cleanup()
|
||||
}()
|
||||
}
|
||||
}()
|
||||
return prl
|
||||
@@ -104,7 +120,7 @@ func NewProgressiveRateLimiter() *ProgressiveRateLimiter {
|
||||
func (prl *ProgressiveRateLimiter) cleanup() {
|
||||
prl.mu.Lock()
|
||||
defer prl.mu.Unlock()
|
||||
cutoff := time.Now().Add(-60 * time.Second)
|
||||
cutoff := clock.Now().Add(-60 * time.Second)
|
||||
for ip, state := range prl.requests {
|
||||
var valid []time.Time
|
||||
for _, t := range state.timestamps {
|
||||
@@ -130,7 +146,7 @@ func (prl *ProgressiveRateLimiter) Check(ip string) (delayMs int) {
|
||||
prl.mu.Lock()
|
||||
defer prl.mu.Unlock()
|
||||
|
||||
now := time.Now()
|
||||
now := clock.Now()
|
||||
state, exists := prl.requests[ip]
|
||||
if !exists {
|
||||
prl.requests[ip] = &ipProgressiveState{
|
||||
|
||||
@@ -7,6 +7,8 @@ import (
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"crussell/clock"
|
||||
)
|
||||
|
||||
// TestProgressiveRateLimiter_SingleRequest verifies no delay for first request.
|
||||
@@ -60,12 +62,12 @@ func TestProgressiveRateLimiter_SustainedAllowsNormal(t *testing.T) {
|
||||
state, exists := prl.requests[ip]
|
||||
if !exists {
|
||||
prl.requests[ip] = &ipProgressiveState{
|
||||
timestamps: []time.Time{time.Now().Add(-time.Duration(60-i*2) * time.Second)},
|
||||
timestamps: []time.Time{clock.Now().Add(-time.Duration(60-i*2) * time.Second)},
|
||||
}
|
||||
prl.mu.Unlock()
|
||||
continue
|
||||
}
|
||||
state.timestamps = append(state.timestamps, time.Now().Add(-time.Duration(60-i*2)*time.Second))
|
||||
state.timestamps = append(state.timestamps, clock.Now().Add(-time.Duration(60-i*2)*time.Second))
|
||||
prl.mu.Unlock()
|
||||
}
|
||||
|
||||
@@ -81,7 +83,7 @@ func TestProgressiveRateLimiter_ExcessSustainedDelays(t *testing.T) {
|
||||
prl := NewProgressiveRateLimiter()
|
||||
ip := "192.168.1.3"
|
||||
|
||||
now := time.Now()
|
||||
now := clock.Now()
|
||||
prl.mu.Lock()
|
||||
state := &ipProgressiveState{timestamps: make([]time.Time, 130)}
|
||||
for i := 0; i < 130; i++ {
|
||||
@@ -118,7 +120,7 @@ func TestProgressiveRateLimiter_CleanupRemovesStaleEntries(t *testing.T) {
|
||||
|
||||
prl.mu.Lock()
|
||||
prl.requests["stale-ip"] = &ipProgressiveState{
|
||||
timestamps: []time.Time{time.Now().Add(-120 * time.Second)},
|
||||
timestamps: []time.Time{clock.Now().Add(-120 * time.Second)},
|
||||
}
|
||||
prl.mu.Unlock()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user