mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-02-02 16:28:39 +08:00
added code comments (#4219)
This commit is contained in:
parent
ec86f22cd6
commit
775b105ab2
@ -23,12 +23,12 @@ const (
|
|||||||
Name = "p2c_ewma"
|
Name = "p2c_ewma"
|
||||||
|
|
||||||
decayTime = int64(time.Second * 10) // default value from finagle
|
decayTime = int64(time.Second * 10) // default value from finagle
|
||||||
forcePick = int64(time.Second)
|
forcePick = int64(time.Second) // If a node is not selected for a period of time, it is forcibly selected.
|
||||||
initSuccess = 1000
|
initSuccess = 1000 // Initial success count
|
||||||
throttleSuccess = initSuccess / 2
|
throttleSuccess = initSuccess / 2 // Success count to trigger throttling
|
||||||
penalty = int64(math.MaxInt32)
|
penalty = int64(math.MaxInt32) // Penalty value for load calculation
|
||||||
pickTimes = 3
|
pickTimes = 3 // Number of pick attempts
|
||||||
logInterval = time.Minute
|
logInterval = time.Minute // Log interval for statistics
|
||||||
)
|
)
|
||||||
|
|
||||||
var emptyPickResult balancer.PickResult
|
var emptyPickResult balancer.PickResult
|
||||||
@ -121,6 +121,10 @@ func (p *p2cPicker) buildDoneFunc(c *subConn) func(info balancer.DoneInfo) {
|
|||||||
if td < 0 {
|
if td < 0 {
|
||||||
td = 0
|
td = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// As the td/decayTime value increases, indicating an increase in delay, the value of w (y axis) will decrease, inversely proportional.
|
||||||
|
// The function curve of y = x^(-x) is as follows.
|
||||||
|
// https://github.com/zeromicro/zero-doc/blob/main/doc/images/y_e_x.png?raw=true
|
||||||
w := math.Exp(float64(-td) / float64(decayTime))
|
w := math.Exp(float64(-td) / float64(decayTime))
|
||||||
lag := int64(now) - start
|
lag := int64(now) - start
|
||||||
if lag < 0 {
|
if lag < 0 {
|
||||||
@ -130,6 +134,8 @@ func (p *p2cPicker) buildDoneFunc(c *subConn) func(info balancer.DoneInfo) {
|
|||||||
if olag == 0 {
|
if olag == 0 {
|
||||||
w = 0
|
w = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The smaller the value of w, the lower the impact of historical data.
|
||||||
atomic.StoreUint64(&c.lag, uint64(float64(olag)*w+float64(lag)*(1-w)))
|
atomic.StoreUint64(&c.lag, uint64(float64(olag)*w+float64(lag)*(1-w)))
|
||||||
success := initSuccess
|
success := initSuccess
|
||||||
if info.Err != nil && !codes.Acceptable(info.Err) {
|
if info.Err != nil && !codes.Acceptable(info.Err) {
|
||||||
@ -182,7 +188,10 @@ func (p *p2cPicker) logStats() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type subConn struct {
|
type subConn struct {
|
||||||
lag uint64
|
// The request latency measured by the weighted moving average algorithm.
|
||||||
|
lag uint64
|
||||||
|
|
||||||
|
// The value represents the number of requests that are either pending or just starting at the current node, and it is obtained through atomic addition.
|
||||||
inflight int64
|
inflight int64
|
||||||
success uint64
|
success uint64
|
||||||
requests int64
|
requests int64
|
||||||
|
Loading…
Reference in New Issue
Block a user