forked from mtusi2026/mtusi2026lab6
feat: add bounded observability and verified SLO incident alerts
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
package admission
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
)
|
||||
|
||||
// ErrRate means the request exhausted a shared rate budget.
|
||||
var ErrRate = errors.New("shared request rate exceeded")
|
||||
|
||||
// ErrSlots means the request exhausted a shared concurrency budget.
|
||||
var ErrSlots = errors.New("shared execution slots exhausted")
|
||||
|
||||
// Distributed defines request admission independently of the backing database.
|
||||
type Distributed interface {
|
||||
// Enter reserves global/group or user/group budgets and returns a release operation.
|
||||
Enter(ctx context.Context, group, user string) (func(), error)
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
// Package admission bounds request rate and concurrent execution independently of transport.
|
||||
package admission
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Options defines the rate and execution bounds for one limiter.
|
||||
type Options struct {
|
||||
// MaxInFlight limits concurrent executions without an admission queue.
|
||||
MaxInFlight int
|
||||
// RequestsPerSecond is the token refill rate.
|
||||
RequestsPerSecond int
|
||||
// Burst is the maximum number of immediately available tokens.
|
||||
Burst int
|
||||
}
|
||||
|
||||
// Limiter owns synchronization for the token bucket and execution slots.
|
||||
// It knows neither HTTP response codes nor application shutdown state.
|
||||
type Limiter struct {
|
||||
mu sync.Mutex
|
||||
rate float64
|
||||
burst float64
|
||||
tokens float64
|
||||
lastRefill time.Time
|
||||
slots chan struct{}
|
||||
}
|
||||
|
||||
// New validates limits and creates a full token bucket with bounded execution slots.
|
||||
func New(cfg Options) (*Limiter, error) {
|
||||
if cfg.MaxInFlight <= 0 || cfg.RequestsPerSecond <= 0 || cfg.Burst <= 0 {
|
||||
return nil, fmt.Errorf("admission limits must be positive")
|
||||
}
|
||||
return &Limiter{
|
||||
rate: float64(cfg.RequestsPerSecond),
|
||||
burst: float64(cfg.Burst),
|
||||
tokens: float64(cfg.Burst),
|
||||
lastRefill: time.Now(),
|
||||
slots: make(chan struct{}, cfg.MaxInFlight),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Allow refills the token bucket and consumes a token when one is available.
|
||||
func (limiter *Limiter) Allow(now time.Time) bool {
|
||||
limiter.mu.Lock()
|
||||
defer limiter.mu.Unlock()
|
||||
elapsed := max(0, now.Sub(limiter.lastRefill).Seconds())
|
||||
limiter.tokens = min(limiter.burst, limiter.tokens+elapsed*limiter.rate)
|
||||
limiter.lastRefill = now
|
||||
if limiter.tokens < 1 {
|
||||
return false
|
||||
}
|
||||
limiter.tokens--
|
||||
return true
|
||||
}
|
||||
|
||||
// Acquire reserves an execution slot without waiting.
|
||||
func (limiter *Limiter) Acquire() bool {
|
||||
select {
|
||||
case limiter.slots <- struct{}{}:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// Release returns an execution slot previously reserved by Acquire.
|
||||
// Each successful Acquire must be paired with exactly one Release.
|
||||
func (limiter *Limiter) Release() {
|
||||
<-limiter.slots
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
package admission
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"testing/synctest"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TestLimiter_Allow checks token exhaustion and refill with deterministic time.
|
||||
func TestLimiter_Allow(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
elapsed time.Duration
|
||||
want bool
|
||||
}{
|
||||
{"Negative: exhausted bucket", 0, false},
|
||||
{"Negative: partial token", time.Millisecond * 500, false},
|
||||
{"Positive: refilled token", time.Second, true},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
// Arrange.
|
||||
limiter := newTestLimiter(t, Options{MaxInFlight: 1, RequestsPerSecond: 1, Burst: 1})
|
||||
if !limiter.Allow(time.Now()) {
|
||||
t.Fatal("fixture token missing")
|
||||
}
|
||||
time.Sleep(tc.elapsed)
|
||||
|
||||
// Act.
|
||||
got := limiter.Allow(time.Now())
|
||||
|
||||
// Assert.
|
||||
if got != tc.want {
|
||||
t.Fatalf("Allow after %s=%v, want=%v", tc.elapsed, got, tc.want)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestLimiter_Acquire checks available, occupied and released execution slots independently.
|
||||
func TestLimiter_Acquire(t *testing.T) {
|
||||
t.Run("Positive: free slot", func(t *testing.T) {
|
||||
// Arrange.
|
||||
limiter := newTestLimiter(t, Options{MaxInFlight: 1, RequestsPerSecond: 1, Burst: 1})
|
||||
|
||||
want := true
|
||||
|
||||
// Act.
|
||||
got := limiter.Acquire()
|
||||
if got {
|
||||
t.Cleanup(limiter.Release)
|
||||
}
|
||||
|
||||
// Assert.
|
||||
if got != want {
|
||||
t.Fatalf("Acquire=%v, want=%v", got, want)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Negative: occupied slot", func(t *testing.T) {
|
||||
// Arrange.
|
||||
limiter := newTestLimiter(t, Options{MaxInFlight: 1, RequestsPerSecond: 1, Burst: 1})
|
||||
|
||||
if !limiter.Acquire() {
|
||||
t.Fatal("fixture slot unavailable")
|
||||
}
|
||||
|
||||
t.Cleanup(limiter.Release)
|
||||
|
||||
want := false
|
||||
|
||||
// Act.
|
||||
got := limiter.Acquire()
|
||||
if got {
|
||||
t.Cleanup(limiter.Release)
|
||||
}
|
||||
|
||||
// Assert.
|
||||
if got != want {
|
||||
t.Fatalf("Acquire=%v, want=%v", got, want)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Positive: released slot", func(t *testing.T) {
|
||||
// Arrange.
|
||||
limiter := newTestLimiter(t, Options{MaxInFlight: 1, RequestsPerSecond: 1, Burst: 1})
|
||||
|
||||
if !limiter.Acquire() {
|
||||
t.Fatal("fixture slot unavailable")
|
||||
}
|
||||
|
||||
limiter.Release()
|
||||
|
||||
want := true
|
||||
|
||||
// Act.
|
||||
got := limiter.Acquire()
|
||||
if got {
|
||||
t.Cleanup(limiter.Release)
|
||||
}
|
||||
|
||||
// Assert.
|
||||
if got != want {
|
||||
t.Fatalf("Acquire=%v, want=%v", got, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// newTestLimiter creates a limiter or fails the test on invalid settings.
|
||||
func newTestLimiter(t *testing.T, options Options) *Limiter {
|
||||
t.Helper()
|
||||
limiter, err := New(options)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return limiter
|
||||
}
|
||||
|
||||
// TestLimiter_New rejects each nonpositive limit independently.
|
||||
func TestLimiter_New(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
options Options
|
||||
}{
|
||||
{"Negative: zero concurrency", Options{MaxInFlight: 0, RequestsPerSecond: 1, Burst: 1}},
|
||||
{"Negative: negative concurrency", Options{MaxInFlight: -1, RequestsPerSecond: 1, Burst: 1}},
|
||||
{"Negative: zero refill rate", Options{MaxInFlight: 1, RequestsPerSecond: 0, Burst: 1}},
|
||||
{"Negative: negative refill rate", Options{MaxInFlight: 1, RequestsPerSecond: -1, Burst: 1}},
|
||||
{"Negative: zero burst", Options{MaxInFlight: 1, RequestsPerSecond: 1, Burst: 0}},
|
||||
{"Negative: negative burst", Options{MaxInFlight: 1, RequestsPerSecond: 1, Burst: -1}},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
// Arrange.
|
||||
options := tc.options
|
||||
|
||||
// Act.
|
||||
limiter, err := New(options)
|
||||
|
||||
// Assert.
|
||||
if err == nil || limiter != nil {
|
||||
t.Fatalf("invalid options accepted: %v, %v", limiter, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user