Skip to content

Commit ad96014

Browse files
kongfei605Copilot
andauthored
refactor(snmp_zabbix): migrate scheduler to Min-Heap with consistent … (#1379)
* refactor(snmp_zabbix): migrate scheduler to Min-Heap with consistent hashing * fix(snmp_zabbix): robust macro expansion and dependency tree building * chore(snmp_zabbix): robust pointer judgement Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * refactor(snmp_zabbix): optimize scheduler and fix thread safety * refactor(snmp_zabbix): use copy remove (stable) instead of swap remove * refactor(snmp_zabbix): enhance scheduler stability and thread safety --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 2aa2178 commit ad96014

File tree

7 files changed

+639
-348
lines changed

7 files changed

+639
-348
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package snmp_zabbix
2+
3+
import (
4+
"time"
5+
)
6+
7+
// ScheduledTask represents a group of items to be collected from a single agent at a specific interval.
8+
// It serves as the element in the Priority Queue.
9+
type ScheduledTask struct {
10+
Agent string
11+
Interval time.Duration
12+
NextRun time.Time // Priority Key
13+
14+
// Payload: Items belonging to this (Agent, Interval) group
15+
Items []MonitorItem
16+
17+
// Heap Index implementation (managed by heap.Interface)
18+
index int
19+
}
20+
21+
// ItemHeap implements heap.Interface for []*ScheduledTask
22+
type ItemHeap []*ScheduledTask
23+
24+
func (pq ItemHeap) Len() int { return len(pq) }
25+
26+
func (pq ItemHeap) Less(i, j int) bool {
27+
// Min-Heap: The item with the earliest NextRun comes first.
28+
return pq[i].NextRun.Before(pq[j].NextRun)
29+
}
30+
31+
func (pq ItemHeap) Swap(i, j int) {
32+
pq[i], pq[j] = pq[j], pq[i]
33+
pq[i].index = i
34+
pq[j].index = j
35+
}
36+
37+
func (pq *ItemHeap) Push(x interface{}) {
38+
n := len(*pq)
39+
item := x.(*ScheduledTask)
40+
item.index = n
41+
*pq = append(*pq, item)
42+
}
43+
44+
func (pq *ItemHeap) Pop() interface{} {
45+
old := *pq
46+
n := len(old)
47+
item := old[n-1]
48+
old[n-1] = nil // avoid memory leak
49+
item.index = -1 // for safety
50+
*pq = old[0 : n-1]
51+
return item
52+
}

0 commit comments

Comments
 (0)