You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

lock_operator.go 826 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package lock
  2. type LockChainOperator struct {
  3. chainList []Lock
  4. lockedList []Lock
  5. ctx *LockContext
  6. }
  7. func NewLockChainOperator(ctx *LockContext) *LockChainOperator {
  8. return &LockChainOperator{ctx: ctx}
  9. }
  10. func (b *LockChainOperator) Add(l Lock) *LockChainOperator {
  11. b.chainList = append(b.chainList, l)
  12. return b
  13. }
  14. func (b *LockChainOperator) Lock() string {
  15. for i := 0; i < len(b.chainList); i++ {
  16. l := b.chainList[i]
  17. if !l.IsMatch(b.ctx) {
  18. continue
  19. }
  20. if errCode := l.Lock(b.ctx); errCode != "" {
  21. b.Unlock()
  22. return errCode
  23. }
  24. b.lockedList = append(b.lockedList, l)
  25. }
  26. return ""
  27. }
  28. func (b *LockChainOperator) Unlock() error {
  29. if b.chainList == nil || len(b.chainList) == 0 {
  30. return nil
  31. }
  32. for j := len(b.lockedList) - 1; j >= 0; j-- {
  33. b.lockedList[j].Unlock(b.ctx)
  34. }
  35. return nil
  36. }