memory.go 953 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package pricing
  2. import (
  3. "context"
  4. "errors"
  5. "sync"
  6. )
  7. type MemoryPricingStore struct {
  8. mu sync.RWMutex
  9. pricing *PricingSet
  10. }
  11. func NewMemoryPricingStore() *MemoryPricingStore {
  12. return &MemoryPricingStore{
  13. pricing: &PricingSet{},
  14. }
  15. }
  16. // GetPricingSet returns a deep copy of the stored pricing set so that callers
  17. // cannot mutate the store's internal state through the returned pointer.
  18. func (mps *MemoryPricingStore) GetPricingSet(ctx context.Context) (*PricingSet, error) {
  19. mps.mu.RLock()
  20. defer mps.mu.RUnlock()
  21. return mps.pricing.Clone(), nil
  22. }
  23. // SetPricingSet stores a deep copy of the provided pricing set so that the
  24. // caller cannot mutate the store's internal state after setting it.
  25. func (mps *MemoryPricingStore) SetPricingSet(ctx context.Context, pricing *PricingSet) error {
  26. if pricing == nil {
  27. return errors.New("nil pricing")
  28. }
  29. mps.mu.Lock()
  30. defer mps.mu.Unlock()
  31. mps.pricing = pricing.Clone()
  32. return nil
  33. }