From 89ed2ee4c0234a21183192fd14d0ed7aedc2e252 Mon Sep 17 00:00:00 2001 From: Sydonian <794346190@qq.com> Date: Tue, 13 Jun 2023 14:30:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0mutex=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/distlock/service/mutex.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 pkg/distlock/service/mutex.go 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) +}