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 794 B

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