16
16
17
17
package bal_slb
18
18
19
+ import (
20
+ "time"
21
+ )
22
+
19
23
import (
20
24
"github.com/bfenetworks/bfe/bfe_balance/backend"
21
25
"github.com/bfenetworks/bfe/bfe_config/bfe_cluster_conf/cluster_table_conf"
22
26
)
23
27
28
+ type WeightSS struct {
29
+ final int // final target weight after slow-start
30
+ slowStartTime int // time for backend increases the weight to the full value, in seconds
31
+ startTime time.Time // time of the first request
32
+ }
33
+
24
34
type BackendRR struct {
25
- weight int // weight of this backend
26
- current int // current weight
27
- backend * backend.BfeBackend // point to BfeBackend
35
+ weight int // weight of this backend
36
+ current int // current weight
37
+ backend * backend.BfeBackend // point to BfeBackend
38
+ inSlowStart bool // indicate if in slow-start phase
39
+ weightSS WeightSS // slow_start related parameters
28
40
}
29
41
30
42
func NewBackendRR () * BackendRR {
@@ -36,15 +48,17 @@ func NewBackendRR() *BackendRR {
36
48
37
49
// Init initialize BackendRR with BackendConf
38
50
func (backRR * BackendRR ) Init (subClusterName string , conf * cluster_table_conf.BackendConf ) {
39
- backRR .weight = * conf .Weight
40
- backRR .current = * conf .Weight
51
+ // scale up 100 times from conf file
52
+ backRR .weight = * conf .Weight * 100
53
+ backRR .current = backRR .weight
54
+ backRR .weightSS .final = backRR .weight
41
55
42
56
back := backRR .backend
43
57
back .Init (subClusterName , conf )
44
58
}
45
59
46
60
func (backRR * BackendRR ) UpdateWeight (weight int ) {
47
- backRR .weight = weight
61
+ backRR .weight = weight * 100
48
62
49
63
// if weight > 0, don't touch backRR.current
50
64
if weight <= 0 {
@@ -60,3 +74,33 @@ func (backRR *BackendRR) MatchAddrPort(addr string, port int) bool {
60
74
back := backRR .backend
61
75
return back .Addr == addr && back .Port == port
62
76
}
77
+
78
+ func (backRR * BackendRR ) initSlowStart (ssTime int ) {
79
+ backRR .weightSS .slowStartTime = ssTime
80
+ if backRR .weightSS .slowStartTime == 0 {
81
+ backRR .inSlowStart = false
82
+ } else {
83
+ backRR .weightSS .startTime = time .Now ()
84
+ backRR .inSlowStart = true
85
+
86
+ // set weight/current to 1, to avoid no traffic allowed at the beginning of start
87
+ backRR .weight = 1
88
+ backRR .current = 1
89
+ }
90
+ }
91
+
92
+ func (backRR * BackendRR ) updateSlowStart () {
93
+ if backRR .inSlowStart {
94
+ current := time .Duration (backRR .weightSS .final ) * time .Since (backRR .weightSS .startTime )
95
+ if backRR .weightSS .slowStartTime != 0 {
96
+ current /= time .Duration (backRR .weightSS .slowStartTime ) * time .Second
97
+ backRR .weight = int (current )
98
+ } else {
99
+ backRR .weight = backRR .weightSS .final
100
+ }
101
+ if backRR .weight >= backRR .weightSS .final {
102
+ backRR .weight = backRR .weightSS .final
103
+ backRR .inSlowStart = false
104
+ }
105
+ }
106
+ }
0 commit comments