MCPcopy Index your code
hub / github.com/cppla/moto / lazyRevalidate

Function lazyRevalidate

controller/boost.go:63–102  ·  view source on GitHub ↗

不再单独提供显式 drop 接口,超时或拨号失败自动失效。 lazyRevalidate 在后台重新跑一次竞速,不打断现有请求;若发现更快线路则更新缓存。

(rule *config.Rule)

Source from the content-addressed store, hash-verified

61
62// lazyRevalidate 在后台重新跑一次竞速,不打断现有请求;若发现更快线路则更新缓存。
63func lazyRevalidate(rule *config.Rule) {
64 // 只有多于一个目标才有意义
65 if len(rule.Targets) < 2 {
66 return
67 }
68 // 设定一个较短的决策超时,避免后台任务堆积。
69 ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
70 defer cancel()
71 switchBetter := make(chan dialResult, 1)
72 // 启动并发拨号
73 for _, v := range rule.Targets {
74 addr := v.Address
75 go func(a string) {
76 if c, err := outboundDial(a); err == nil {
77 select {
78 case switchBetter <- dialResult{conn: c, addr: a}:
79 case <-ctx.Done():
80 c.Close()
81 }
82 }
83 }(addr)
84 }
85 var best dialResult
86 select {
87 case best = <-switchBetter:
88 cancel()
89 case <-ctx.Done():
90 return
91 }
92 if tc, ok := best.conn.(*net.TCPConn); ok {
93 _ = tc.SetNoDelay(true)
94 _ = tc.SetKeepAlive(true)
95 _ = tc.SetKeepAlivePeriod(30 * time.Second)
96 }
97 storeBoostWinner(rule.Name, best.addr)
98 utils.Logger.Debug("懒惰刷新winner",
99 zap.String("ruleName", rule.Name),
100 zap.String("targetAddr", best.conn.RemoteAddr().String()))
101 best.conn.Close()
102}
103
104// HandleBoost 同时发起多路拨号,挑选最先成功的连接并套上单边加速。
105func HandleBoost(conn net.Conn, rule *config.Rule) {

Callers 1

HandleBoostFunction · 0.85

Calls 2

outboundDialFunction · 0.85
storeBoostWinnerFunction · 0.85

Tested by

no test coverage detected