Skip to content

Commit

Permalink
replace global lock to distributed lock
Browse files Browse the repository at this point in the history
  • Loading branch information
457813723 committed Jul 10, 2024
1 parent 8270405 commit 9dd6191
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
19 changes: 16 additions & 3 deletions http_server/handle/sub_account_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handle

import (
"context"
"crypto/md5"
"das_sub_account/config"
"das_sub_account/internal"
"das_sub_account/tables"
Expand All @@ -20,6 +21,7 @@ import (
"net/http"
"strings"
"sync"
"time"
)

type ReqSubAccountInit struct {
Expand Down Expand Up @@ -322,12 +324,23 @@ func (h *HttpHandle) buildSubAccountInitTx(p *paramsSubAccountInitTx) (*txbuilde
return &txParams, nil
}

func (h *HttpHandle) getSvrBalance(p paramBalance) (uint64, []*indexer.LiveCell, error) {
func (h *HttpHandle) getSvrBalance(ctx context.Context, p paramBalance) (uint64, []*indexer.LiveCell, error) {
if p.capacityForNeed == 0 {
return 0, nil, fmt.Errorf("needCapacity is nil")
}
svrBalanceLock.Lock()
defer svrBalanceLock.Unlock()
svrLockKey, _ := p.svrLock.Serialize()
lockKey := fmt.Sprintf("%x", md5.Sum(svrLockKey))
if err := h.RC.Lock(lockKey, time.Second*5); err != nil {
return 0, nil, fmt.Errorf("get lock err")
}
defer func() {
if err := h.RC.UnLock(lockKey); err != nil {
log.Error(ctx, "UnLock err:", err.Error())
}
}()

//svrBalanceLock.Lock()
//defer svrBalanceLock.Unlock()

liveCells, total, err := h.DasCore.GetBalanceCells(&core.ParamGetBalanceCells{
DasCache: h.DasCache,
Expand Down
2 changes: 1 addition & 1 deletion http_server/handle/sub_account_init_free.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (h *HttpHandle) doSubAccountInitFree(ctx context.Context, req *ReqSubAccoun

capacityNeed, capacityForChange := subAccountBasicCapacity+subAccountPreparedFeeCapacity+subAccountCommonFee, common.DasLockWithBalanceTypeMinCkbCapacity

change, liveCells, err := h.getSvrBalance(paramBalance{
change, liveCells, err := h.getSvrBalance(ctx, paramBalance{
svrLock: h.ServerScript,
capacityForNeed: capacityNeed,
capacityForChange: capacityForChange,
Expand Down

0 comments on commit 9dd6191

Please sign in to comment.