Browse Source

分布式锁

master
huangjiajun 1 week ago
parent
commit
2bf33ead99
1 changed files with 23 additions and 0 deletions
  1. +23
    -0
      svc/svc_lock.go

+ 23
- 0
svc/svc_lock.go View File

@@ -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
}

Loading…
Cancel
Save