import "sync var once sync. Once *RoundRobinIndexCounterTypetypo var RoundRobin Index Counter Round Robin Index CounterType struct { mu sync.Mutex V map[string]int len int 1// return the current index and increment index. key will be experiment ID func (counter "RoundRobin Index CounterType) Get Index (key string) int { // Lock so only one goroutine at a time can accesS the map c.v. counter.mu.Lock() defer counter.mu. Unlock() current Idx: counter.v[key] // reset counter if it crosses max length length if nextIdx counter.v[key] + 1; nextIdx > counter.len [ counter. Reset Counter(key) } else { counter.v[key] nextIdx } return currentIdx func (counter *RoundRobin Index CounterType) Reset Counter(key string) { counter.v[key] = 0 } func Initialize Round RobinIndexCounte { once. Do (func() { C Round Robin Index CounterType{v: make(map[string]int), len: 1000} RoundRobinIndexCounter = &c }) J
0 Comments: