diff --git a/svc/svc_lock.go b/svc/svc_lock.go new file mode 100644 index 0000000..6f49990 --- /dev/null +++ b/svc/svc_lock.go @@ -0,0 +1,23 @@ +package svc + +import ( + "errors" + "fmt" +) + +// HandleDistributedLockForVideo 处理短视频更新时获取锁和释放锁 如果加锁成功,使用语句 ` defer cb() ` 释放锁 +func HandleDistributedLockForComm(uid, fix, requestIdPrefix string) (cb func(), err error) { + // 获取虚拟币更新锁 + balanceLockKey := fmt.Sprintf(fix, uid) + requestId := GetDistributedLockRequestId(requestIdPrefix) + balanceLockOk := TryGetDistributedLock(balanceLockKey, requestId, true) + if !balanceLockOk { + return nil, errors.New("系统繁忙,请稍后再试") + } + + cb = func() { + _, _ = ReleaseDistributedLock(balanceLockKey, requestId) + } + + return cb, nil +}