Skip to content

Commit

Permalink
Change semantic of waitMs to nanosToWait in TokenResult and polish re…
Browse files Browse the repository at this point in the history
…lated stat slots

Signed-off-by: Eric Zhao <[email protected]>
  • Loading branch information
sczyh30 committed Nov 26, 2020
1 parent f643352 commit 7bffa66
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 31 deletions.
53 changes: 27 additions & 26 deletions core/base/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package base

import (
"fmt"
"time"
)

type BlockType uint8
Expand Down Expand Up @@ -58,13 +59,13 @@ func (s TokenResultStatus) String() string {
type TokenResult struct {
status TokenResultStatus

blockErr *BlockError
waitMs uint64
blockErr *BlockError
nanosToWait time.Duration
}

func (r *TokenResult) DeepCopyFrom(newResult *TokenResult) {
r.status = newResult.status
r.waitMs = newResult.waitMs
r.nanosToWait = newResult.nanosToWait
if r.blockErr == nil {
r.blockErr = &BlockError{
blockType: newResult.blockErr.blockType,
Expand All @@ -84,7 +85,7 @@ func (r *TokenResult) DeepCopyFrom(newResult *TokenResult) {
func (r *TokenResult) ResetToPass() {
r.status = ResultStatusPass
r.blockErr = nil
r.waitMs = 0
r.nanosToWait = 0
}

func (r *TokenResult) ResetToBlocked(blockType BlockType) {
Expand All @@ -97,7 +98,7 @@ func (r *TokenResult) ResetToBlocked(blockType BlockType) {
r.blockErr.rule = nil
r.blockErr.snapshotValue = nil
}
r.waitMs = 0
r.nanosToWait = 0
}

func (r *TokenResult) ResetToBlockedWithMessage(blockType BlockType, blockMsg string) {
Expand All @@ -110,7 +111,7 @@ func (r *TokenResult) ResetToBlockedWithMessage(blockType BlockType, blockMsg st
r.blockErr.rule = nil
r.blockErr.snapshotValue = nil
}
r.waitMs = 0
r.nanosToWait = 0
}

func (r *TokenResult) ResetToBlockedWithCause(blockType BlockType, blockMsg string, rule SentinelRule, snapshot interface{}) {
Expand All @@ -123,7 +124,7 @@ func (r *TokenResult) ResetToBlockedWithCause(blockType BlockType, blockMsg stri
r.blockErr.rule = rule
r.blockErr.snapshotValue = snapshot
}
r.waitMs = 0
r.nanosToWait = 0
}

func (r *TokenResult) IsPass() bool {
Expand All @@ -142,8 +143,8 @@ func (r *TokenResult) BlockError() *BlockError {
return r.blockErr
}

func (r *TokenResult) WaitMs() uint64 {
return r.waitMs
func (r *TokenResult) NanosToWait() time.Duration {
return r.nanosToWait
}

func (r *TokenResult) String() string {
Expand All @@ -153,45 +154,45 @@ func (r *TokenResult) String() string {
} else {
blockMsg = r.blockErr.Error()
}
return fmt.Sprintf("TokenResult{status=%s, blockErr=%s, waitMs=%d}", r.status.String(), blockMsg, r.waitMs)
return fmt.Sprintf("TokenResult{status=%s, blockErr=%s, nanosToWait=%d}", r.status.String(), blockMsg, r.nanosToWait)
}

func NewTokenResultPass() *TokenResult {
return &TokenResult{
status: ResultStatusPass,
blockErr: nil,
waitMs: 0,
status: ResultStatusPass,
blockErr: nil,
nanosToWait: 0,
}
}

func NewTokenResultBlocked(blockType BlockType) *TokenResult {
return &TokenResult{
status: ResultStatusBlocked,
blockErr: NewBlockError(blockType),
waitMs: 0,
status: ResultStatusBlocked,
blockErr: NewBlockError(blockType),
nanosToWait: 0,
}
}

func NewTokenResultBlockedWithMessage(blockType BlockType, blockMsg string) *TokenResult {
return &TokenResult{
status: ResultStatusBlocked,
blockErr: NewBlockErrorWithMessage(blockType, blockMsg),
waitMs: 0,
status: ResultStatusBlocked,
blockErr: NewBlockErrorWithMessage(blockType, blockMsg),
nanosToWait: 0,
}
}

func NewTokenResultBlockedWithCause(blockType BlockType, blockMsg string, rule SentinelRule, snapshot interface{}) *TokenResult {
return &TokenResult{
status: ResultStatusBlocked,
blockErr: NewBlockErrorWithCause(blockType, blockMsg, rule, snapshot),
waitMs: 0,
status: ResultStatusBlocked,
blockErr: NewBlockErrorWithCause(blockType, blockMsg, rule, snapshot),
nanosToWait: 0,
}
}

func NewTokenResultShouldWait(waitMs uint64) *TokenResult {
func NewTokenResultShouldWait(waitNs time.Duration) *TokenResult {
return &TokenResult{
status: ResultStatusShouldWait,
blockErr: nil,
waitMs: waitMs,
status: ResultStatusShouldWait,
blockErr: nil,
nanosToWait: waitNs,
}
}
4 changes: 2 additions & 2 deletions core/flow/slot.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ func (s *Slot) Check(ctx *base.EntryContext) *base.TokenResult {
return r
}
if r.Status() == base.ResultStatusShouldWait {
if waitMs := r.WaitMs(); waitMs > 0 {
if nanosToWait := r.NanosToWait(); nanosToWait > 0 {
// Handle waiting action.
time.Sleep(time.Duration(waitMs) * time.Millisecond)
time.Sleep(nanosToWait)
}
continue
}
Expand Down
4 changes: 2 additions & 2 deletions core/hotspot/slot.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ func (s *Slot) Check(ctx *base.EntryContext) *base.TokenResult {
return r
}
if r.Status() == base.ResultStatusShouldWait {
if waitMs := r.WaitMs(); waitMs > 0 {
if nanosToWait := r.NanosToWait(); nanosToWait > 0 {
// Handle waiting action.
time.Sleep(time.Duration(waitMs) * time.Millisecond)
time.Sleep(nanosToWait)
}
continue
}
Expand Down
3 changes: 2 additions & 1 deletion core/hotspot/traffic_shaping.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"math"
"runtime"
"sync/atomic"
"time"

"github.com/alibaba/sentinel-golang/core/base"
"github.com/alibaba/sentinel-golang/core/hotspot/cache"
Expand Down Expand Up @@ -281,7 +282,7 @@ func (c *throttlingTrafficShapingController) PerformChecking(arg interface{}, ba
awaitTime := expectedTime - currentTimeInMs
if awaitTime > 0 {
atomic.StoreInt64(lastPassTimePtr, expectedTime)
return base.NewTokenResultShouldWait(uint64(awaitTime))
return base.NewTokenResultShouldWait(time.Duration(awaitTime) * time.Millisecond)
}
return nil
} else {
Expand Down

0 comments on commit 7bffa66

Please sign in to comment.