-
Notifications
You must be signed in to change notification settings - Fork 36
/
fc_state.go
55 lines (48 loc) · 1.22 KB
/
fc_state.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package flowctrl
import (
"sync/atomic"
"time"
)
/*
状态模式
*/
// state 流控状态接口
type state interface {
// tryAccept 判断当前是否处理请求
tryAccept() bool
// trySwitch 尝试切换到下一个状态,可能未满足条件而保持不变
trySwitch() bool
// setContext 设置流控上下文
setContext(context *Context)
}
/*
模板方法模式
*/
// stateTemplate 流控状态模板
type stateTemplate struct {
ctx *Context
// 判断当前状态是否与other一致,由实际状态定义
isSameTo func(other state) bool
}
// 模板方法,尝试切换到下个状态,其中isSameTo方法由子类实现
func (s *stateTemplate) trySwitch() bool {
now := time.Now().Unix()
interval := now - atomic.LoadInt64(&s.ctx.lastUpdateTimestamp)
// 未到1s则不需要切换状态
if interval < 1 {
return false
}
atomic.StoreInt64(&s.ctx.lastUpdateTimestamp, now)
reqCount := atomic.LoadUint64(&s.ctx.reqCount)
atomic.CompareAndSwapUint64(&s.ctx.reqCount, reqCount, 0)
rate := reqCount / uint64(interval)
nextState := factory.create(rate)
if s.isSameTo(nextState) {
return false
}
s.ctx.switchTo(nextState)
return true
}
func (s *stateTemplate) setContext(context *Context) {
s.ctx = context
}