diff --git a/pkg/distlock/service/mutex.go b/pkg/distlock/service/mutex.go new file mode 100644 index 0000000..44e6d21 --- /dev/null +++ b/pkg/distlock/service/mutex.go @@ -0,0 +1,30 @@ +package service + +import "gitlink.org.cn/cloudream/common/pkg/distlock" + +type Mutex struct { + svc *Service + lockReq distlock.LockRequest + lockReqID string +} + +func NewMutex(svc *Service, lockReq distlock.LockRequest) *Mutex { + return &Mutex{ + svc: svc, + lockReq: lockReq, + } +} + +func (m *Mutex) Lock() error { + reqID, err := m.svc.Acquire(m.lockReq) + if err != nil { + return err + } + + m.lockReqID = reqID + return nil +} + +func (m *Mutex) Unlock() error { + return m.svc.Release(m.lockReqID) +}