grafana/pkg/services/quota/context.go
Sofia Papagiannaki 9855e74b92
Chore: Refactor quota service (#58643)
Chore: Refactor quota service (#57586)

* Chore: refactore quota service

* Apply suggestions from code review
2022-11-14 21:08:10 +02:00

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
}