package priority
import (
"container/list"
"context"
"slices"
"sort"
"sync"
"time"
"github.com/failsafe-go/failsafe-go/internal/util"
)
const (
UserKey key = 2
maxLevel = 99.0
)
func ContextWithUser (ctx context .Context , userID string ) context .Context {
return context .WithValue (ctx , UserKey , userID )
}
func UserFromContext (ctx context .Context ) string {
if untypedUser := ctx .Value (UserKey ); untypedUser != nil {
if userID , ok := untypedUser .(string ); ok {
return userID
}
}
return ""
}
type UsageTracker interface {
RecordUsage (userID string , usage int64 )
GetUsage (userID string ) (int64 , bool )
GetLevel (userID string , priority Priority ) int
Calibrate ()
}
type usageTracker struct {
clock util .Clock
newWindowFn func () *usageWindow
maxUsers int
expirationDuration time .Duration
mu sync .RWMutex
users map [string ]*userEntry
lru *list .List
}
type userEntry struct {
window *usageWindow
quantile float64
lastActive time .Time
lruElement *list .Element
}
func NewUsageTracker (windowDuration time .Duration , maxUsers int ) UsageTracker {
return &usageTracker {
clock : util .WallClock ,
newWindowFn : func () *usageWindow {
return newUsageWindow (30 , windowDuration , util .WallClock )
},
maxUsers : maxUsers ,
expirationDuration : 2 * windowDuration ,
users : make (map [string ]*userEntry ),
lru : list .New (),
}
}
func (ut *usageTracker ) RecordUsage (userID string , usage int64 ) {
ut .mu .Lock ()
defer ut .mu .Unlock ()
entry := ut .users [userID ]
if entry == nil {
if len (ut .users ) >= ut .maxUsers {
ut .evictOldest ()
}
entry = &userEntry {
window : ut .newWindowFn (),
quantile : -1 ,
}
ut .users [userID ] = entry
entry .lruElement = ut .lru .PushFront (userID )
} else {
ut .lru .MoveToFront (entry .lruElement )
}
entry .lastActive = ut .clock .Now ()
entry .window .RecordUsage (usage )
}
func (ut *usageTracker ) GetLevel (userID string , priority Priority ) int {
ut .mu .RLock ()
entry := ut .users [userID ]
ut .mu .RUnlock ()
lRange := priority .levelRange ()
if entry == nil {
return lRange .upper
}
totalUsage := entry .window .TotalUsage ()
if totalUsage == 0 {
return lRange .upper
}
if entry .quantile < 0 {
return lRange .upper
}
fairnessScore := 1.0 - entry .quantile
return lRange .lower + int (maxLevel *fairnessScore )
}
func (ut *usageTracker ) GetUsage (userID string ) (int64 , bool ) {
ut .mu .RLock ()
defer ut .mu .RUnlock ()
entry := ut .users [userID ]
if entry == nil {
return 0 , false
}
return entry .window .TotalUsage (), true
}
func (ut *usageTracker ) Calibrate () {
ut .mu .Lock ()
defer ut .mu .Unlock ()
now := ut .clock .Now ()
cleanupThreshold := now .Add (-ut .expirationDuration )
usages := make ([]int64 , 0 , len (ut .users ))
for userID , entry := range ut .users {
entry .window .ExpireBuckets ()
if usage := entry .window .TotalUsage (); usage > 0 {
usages = append (usages , usage )
} else if entry .lastActive .Before (cleanupThreshold ) {
delete (ut .users , userID )
ut .lru .Remove (entry .lruElement )
}
}
slices .Sort (usages )
for _ , entry := range ut .users {
if usage := entry .window .TotalUsage (); usage > 0 {
entry .quantile = ut .computeQuantile (usage , usages )
} else {
entry .quantile = -1
}
}
}
func (ut *usageTracker ) computeQuantile (usage int64 , sortedUsages []int64 ) float64 {
if len (sortedUsages ) == 0 {
return 0
}
index := sort .Search (len (sortedUsages ), func (i int ) bool {
return sortedUsages [i ] >= usage
})
return util .Round (float64 (index ) / float64 (len (sortedUsages )))
}
func (ut *usageTracker ) evictOldest () {
if oldest := ut .lru .Back (); oldest != nil {
userID := oldest .Value .(string )
delete (ut .users , userID )
ut .lru .Remove (oldest )
}
}
type usageStat struct {
totalUsage int64
samples uint32
}
type usageWindow struct {
util .BucketedWindow [usageStat ]
}
func newUsageWindow(bucketCount int , thresholdingPeriod time .Duration , clock util .Clock ) *usageWindow {
buckets := make ([]usageStat , bucketCount )
return &usageWindow {
util .BucketedWindow [usageStat ]{
Clock : clock ,
BucketCount : int64 (bucketCount ),
BucketNanos : (thresholdingPeriod / time .Duration (bucketCount )).Nanoseconds (),
Buckets : buckets ,
Summary : usageStat {},
AddFn : func (summary *usageStat , bucket *usageStat ) {
summary .totalUsage += bucket .totalUsage
summary .samples += bucket .samples
},
RemoveFn : func (summary *usageStat , bucket *usageStat ) {
summary .totalUsage -= bucket .totalUsage
summary .samples -= bucket .samples
},
ResetFn : func (s *usageStat ) {
s .totalUsage = 0
s .samples = 0
},
},
}
}
func (w *usageWindow ) RecordUsage (usage int64 ) {
bucket := w .ExpireBuckets ()
bucket .totalUsage += usage
bucket .samples ++
w .Summary .totalUsage += usage
w .Summary .samples ++
}
func (w *usageWindow ) TotalUsage () int64 {
return w .Summary .totalUsage
}
func (w *usageWindow ) Samples () uint32 {
return w .Summary .samples
}
The pages are generated with Golds v0.8.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .