mirror of
https://github.com/grafana/grafana.git
synced 2024-11-29 20:24:18 -06:00
9855e74b92
Chore: Refactor quota service (#57586) * Chore: refactore quota service * Apply suggestions from code review
43 lines
744 B
Go
43 lines
744 B
Go
package quota
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
)
|
|
|
|
type Context struct {
|
|
context.Context
|
|
TargetToSrv *TargetToSrv
|
|
}
|
|
|
|
func FromContext(ctx context.Context, targetToSrv *TargetToSrv) Context {
|
|
if targetToSrv == nil {
|
|
targetToSrv = NewTargetToSrv()
|
|
}
|
|
return Context{Context: ctx, TargetToSrv: targetToSrv}
|
|
}
|
|
|
|
type TargetToSrv struct {
|
|
mutex sync.RWMutex
|
|
m map[Target]TargetSrv
|
|
}
|
|
|
|
func NewTargetToSrv() *TargetToSrv {
|
|
return &TargetToSrv{m: make(map[Target]TargetSrv)}
|
|
}
|
|
|
|
func (m *TargetToSrv) Get(target Target) (TargetSrv, bool) {
|
|
m.mutex.RLock()
|
|
defer m.mutex.RUnlock()
|
|
|
|
srv, ok := m.m[target]
|
|
return srv, ok
|
|
}
|
|
|
|
func (m *TargetToSrv) Set(target Target, srv TargetSrv) {
|
|
m.mutex.Lock()
|
|
defer m.mutex.Unlock()
|
|
|
|
m.m[target] = srv
|
|
}
|