secret_locks.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. Copyright © The ESO Authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // Package locks provides locking mechanisms to prevent lost updates when accessing secrets.
  14. package locks
  15. import (
  16. "errors"
  17. "fmt"
  18. "sync"
  19. )
  20. var (
  21. // ErrConflict is returned when a secret is locked and cannot be accessed.
  22. ErrConflict = errors.New("unable to access secret since it is locked")
  23. sharedLocks = &secretLocks{}
  24. )
  25. // TryLock tries to acquire a lock for a given provider and secret.
  26. // It returns an unlock function to release the lock and an error if the lock
  27. // could not be acquired.
  28. func TryLock(providerName, secretName string) (unlock func(), _ error) {
  29. key := fmt.Sprintf("%s#%s", providerName, secretName)
  30. unlockFunc, ok := sharedLocks.tryLock(key)
  31. if !ok {
  32. return nil, fmt.Errorf(
  33. "failed to acquire lock: provider: %s, secret: %s: %w",
  34. providerName,
  35. secretName,
  36. ErrConflict,
  37. )
  38. }
  39. return unlockFunc, nil
  40. }
  41. // secretLocks is a collection of locks for secrets to prevent lost update.
  42. type secretLocks struct {
  43. locks sync.Map
  44. }
  45. // tryLock tries to hold lock for a given secret and returns true if succeeded.
  46. func (s *secretLocks) tryLock(key string) (func(), bool) {
  47. lock, _ := s.locks.LoadOrStore(key, &sync.Mutex{})
  48. mu, _ := lock.(*sync.Mutex)
  49. return mu.Unlock, mu.TryLock()
  50. }