Skip to content

Commit 16d678a

Browse files
committed
chore: refine display logic for node ranking
1 parent 33357c9 commit 16d678a

5 files changed

Lines changed: 53 additions & 21 deletions

File tree

adapter/outboundgroup/smart.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,12 @@ func (s *Smart) InitializeCache() {
371371

372372
s.startTimedTask(5*time.Minute, checkInterval, "Clean up nodes", s.cleanupOrphanedNodeCache, true)
373373
s.startTimedTask(10*time.Second, checkInterval, "Preload frequent data", func() {
374-
s.store.PreloadFrequentData(s.Name(), s.configName)
374+
proxies := s.GetProxies(false)
375+
proxyNames := make([]string, 0, len(proxies))
376+
for _, p := range proxies {
377+
proxyNames = append(proxyNames, p.Name())
378+
}
379+
s.store.PreloadFrequentData(s.Name(), s.configName, proxyNames)
375380
}, true)
376381
s.startTimedTask(5*time.Minute, prefetchInterval, "prefetch", s.runPrefetch, false)
377382
s.startTimedTask(5*time.Minute, rankingInterval, "ranking", s.updateNodeRanking, false)
@@ -462,7 +467,12 @@ func (s *Smart) updateNodeRanking() {
462467

463468
log.Debugln("[Smart] Starting node ranking update for policy group [%s]", s.Name())
464469

465-
ranking, err := s.store.GetNodeWeightRanking(s.Name(), s.configName, false)
470+
proxies := s.GetProxies(false)
471+
proxyNames := make([]string, 0, len(proxies))
472+
for _, p := range proxies {
473+
proxyNames = append(proxyNames, p.Name())
474+
}
475+
ranking, err := s.store.GetNodeWeightRanking(s.Name(), s.configName, false, proxyNames)
466476
if err != nil {
467477
log.Warnln("[Smart] Failed to update node ranking: %v", err)
468478
return

component/smart/common.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -318,20 +318,20 @@ func getSystemMemoryLimit() float64 {
318318
}
319319
}
320320
}
321-
} else if runtime.GOOS == "linux" || runtime.GOOS == "darwin" {
322-
output, err = cmd.ExecCmd("grep MemTotal /proc/meminfo")
323-
if err == nil {
324-
parts := strings.Fields(output)
325-
if len(parts) >= 2 {
326-
memStr := strings.TrimSuffix(parts[1], "kB")
327-
memStr = strings.TrimSpace(memStr)
328-
memKB, parseErr := strconv.ParseFloat(memStr, 64)
329-
if parseErr == nil {
330-
memTotal = memKB / 1024.0
321+
} else if runtime.GOOS == "linux" || runtime.GOOS == "android" || runtime.GOOS == "darwin" || runtime.GOOS == "freebsd" {
322+
output, err = cmd.ExecCmd("grep MemTotal /proc/meminfo")
323+
if err == nil {
324+
parts := strings.Fields(output)
325+
if len(parts) >= 2 {
326+
memStr := strings.TrimSuffix(parts[1], "kB")
327+
memStr = strings.TrimSpace(memStr)
328+
memKB, parseErr := strconv.ParseFloat(memStr, 64)
329+
if parseErr == nil {
330+
memTotal = memKB / 1024.0
331+
}
331332
}
332333
}
333334
}
334-
}
335335

336336
memTotal = memTotal / 4.0
337337

component/smart/memory.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ func (s *Store) AdjustCacheParameters() {
507507
}
508508

509509
// 预加载频繁使用的数据
510-
func (s *Store) PreloadFrequentData(group, config string) {
510+
func (s *Store) PreloadFrequentData(group, config string, proxies []string) {
511511
log.Infoln("[SmartStore] Starting data preloading for group [%s], config [%s]", group, config)
512512

513513
memUsage := GetSystemMemoryUsage()
@@ -535,7 +535,7 @@ func (s *Store) PreloadFrequentData(group, config string) {
535535
nodeStatesCount = len(stateData)
536536
}
537537

538-
ranking, _ := s.GetNodeWeightRanking(group, config, true)
538+
ranking, _ := s.GetNodeWeightRanking(group, config, true, proxies)
539539

540540
domains, err := s.GetActiveDomains(group, config, domainLimit)
541541
if err == nil && len(domains) > 0 {

component/smart/stats.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ func (r *AtomicStatsRecord) SetWeight(weightType string, value float64) {
276276
}
277277

278278
// 获取节点权重排名
279-
func (s *Store) GetNodeWeightRanking(group, config string, onlyCache bool) (map[string]string, error) {
279+
func (s *Store) GetNodeWeightRanking(group, config string, onlyCache bool, proxies []string) (map[string]string, error) {
280280
cacheKey := FormatCacheKey(KeyTypeRanking, config, group, "")
281281

282282
cachedData, ok := GetCacheValue(cacheKey)
@@ -305,7 +305,12 @@ func (s *Store) GetNodeWeightRanking(group, config string, onlyCache bool) (map[
305305
return make(map[string]string), nil
306306
}
307307

308-
allNodes, _ := s.GetAllNodesForGroup(group, config)
308+
var allNodes []string
309+
if len(proxies) > 0 {
310+
allNodes = proxies
311+
} else {
312+
allNodes, _ = s.GetAllNodesForGroup(group, config)
313+
}
309314

310315
nodeDataMap := make(map[string]*struct{
311316
tcpWeights float64
@@ -553,9 +558,11 @@ func (s *Store) GetNodeWeightRanking(group, config string, onlyCache bool) (map[
553558
}
554559
}
555560

556-
for _, nodeName := range allNodes {
557-
if _, exists := nodeWeights[nodeName]; !exists {
558-
result[nodeName] = RankRarelyUsed
561+
if len(nodeWeights) > 0 {
562+
for _, nodeName := range allNodes {
563+
if _, exists := nodeWeights[nodeName]; !exists {
564+
result[nodeName] = RankRarelyUsed
565+
}
559566
}
560567
}
561568

hub/route/groups.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,23 @@ func getGroupWeights(w http.ResponseWriter, r *http.Request) {
131131
})
132132
return
133133
}
134+
135+
proxies := smartGroup.GetProxies(false)
136+
proxyNames := make([]string, 0, len(proxies))
137+
for _, p := range proxies {
138+
proxyNames = append(proxyNames, p.Name())
139+
}
140+
141+
if len(proxyNames) == 0 {
142+
render.Status(r, http.StatusInternalServerError)
143+
render.JSON(w, r, render.M{
144+
"weights": map[string]string{},
145+
"message": "No available proxies in this group",
146+
})
147+
return
148+
}
134149

135-
weights, err := smartStore.GetStore().GetNodeWeightRanking(groupName, configName, false)
150+
weights, err := smartStore.GetStore().GetNodeWeightRanking(groupName, configName, false, proxyNames)
136151

137152
if err != nil {
138153
log.Warnln("[Smart] Failed to get weight ranking: %s", err.Error())

0 commit comments

Comments
 (0)