Skip to content

Commit

Permalink
mcs/resourcemanager: delete expire tokenSlot
Browse files Browse the repository at this point in the history
Signed-off-by: guo-shaoge <[email protected]>
  • Loading branch information
guo-shaoge committed Nov 9, 2023
1 parent 01fb56b commit 427e76c
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pkg/mcs/resourcemanager/server/token_buckets.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type TokenSlot struct {
// tokenCapacity is the number of tokens in the slot.
tokenCapacity float64
lastTokenCapacity float64
lastReqTime time.Time
}

// GroupTokenBucketState is the running state of TokenBucket.
Expand Down Expand Up @@ -108,10 +109,12 @@ func (gts *GroupTokenBucketState) resetLoan() {
}

evenTokens := gts.Tokens * evenRatio
now := time.Now()
for _, slot := range gts.tokenSlots {
slot.requireTokensSum = 0
slot.tokenCapacity = evenTokens
slot.lastTokenCapacity = evenTokens
slot.lastReqTime = now
}
}

Expand All @@ -127,8 +130,10 @@ func (gts *GroupTokenBucketState) balanceSlotTokens(
slot = &TokenSlot{}
gts.tokenSlots[clientUniqueID] = slot
gts.clientConsumptionTokensSum = 0
slot.lastReqTime = time.Now()
}
} else {
slot.lastReqTime = time.Now()
if gts.clientConsumptionTokensSum >= maxAssignTokens {
gts.clientConsumptionTokensSum = 0
}
Expand All @@ -142,6 +147,11 @@ func (gts *GroupTokenBucketState) balanceSlotTokens(
if len(gts.tokenSlots) == 0 {
return
}
for clientUniqueID, slot := range gts.tokenSlots {
if time.Since(slot.lastReqTime).Minutes() >= 10 {
delete(gts.tokenSlots, clientUniqueID)
}
}
evenRatio := 1 / float64(len(gts.tokenSlots))
if settings.GetBurstLimit() <= 0 {
for _, slot := range gts.tokenSlots {
Expand Down

0 comments on commit 427e76c

Please sign in to comment.