From 2bf33ead99e1c322e36967ed2ab87479cffbb218 Mon Sep 17 00:00:00 2001 From: huangjiajun <582604932@qq.com> Date: Mon, 9 Dec 2024 14:42:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E5=B8=83=E5=BC=8F=E9=94=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- svc/svc_lock.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 svc/svc_lock.go 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 +}