func ProcessTx(userID string) bool { // called for every incoming request
now, cutoff := time.Now().Unix(), time.Now().Unix()-WindowSec // what second are we in? where does the window start?
last, _ := rdb.Get(ctx, "rate:"+userID+":last_clean").Int64() // last second we already cleaned up
for ts := last+1; ts <= cutoff; ts++ { // loop over seconds that just fell outside the window
n, _ := rdb.GetDel(ctx, fmt.Sprintf("rate:%s:%d", userID, ts)).Int64() // get & delete that old second's counter
rdb.DecrBy(ctx, "rate:"+userID+":sum", n) } // remove its count from the running total
rdb.Set(ctx, "rate:"+userID+":last_clean", cutoff, 0) // remember we cleaned up to this point
rdb.Incr(ctx, fmt.Sprintf("rate:%s:%d", userID, now)) // add 1 to this second's counter
total, _ := rdb.Incr(ctx, "rate:"+userID+":sum").Result() // add 1 to the running total, read it back
return total <= LIMIT } // allowed if total is within limit — one check, no loop