-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdiscord_notifier_network.go
More file actions
179 lines (164 loc) · 4.23 KB
/
Copy pathdiscord_notifier_network.go
File metadata and controls
179 lines (164 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"context"
"net"
"time"
)
func (n *discordNotifier) isNetworkOK() bool {
if n == nil {
return false
}
n.scheduleMu.Lock()
ignoreUntil := n.ignoreNetworkUntil
n.scheduleMu.Unlock()
if !ignoreUntil.IsZero() && time.Now().Before(ignoreUntil) {
// Ignore connectivity checks during the initial boot gate.
return true
}
n.netMu.Lock()
defer n.netMu.Unlock()
if !n.networkKnown {
// Until we have a signal either way, assume OK (startup will quickly check).
return true
}
return n.networkOK
}
func (n *discordNotifier) setNetworkOK(ok bool, now time.Time) {
if n == nil {
return
}
n.netMu.Lock()
prevKnown := n.networkKnown
prevOK := n.networkOK
n.networkKnown = true
n.networkOK = ok
n.netMu.Unlock()
// On any network transition (offline OR online), reset notifier state so
// we don't spam everyone due to our own connectivity blip.
//
// On first observation (prevKnown=false), do not reset state or emit a
// channel message; this avoids pushing the boot gate back and avoids a
// confusing "network back online" message at startup.
if prevKnown && prevOK != ok {
n.resetAllNotificationState(now)
if ok {
n.enqueueNotice("Network connectivity is back online; notifications will resume after the warm-up delay.")
} else {
n.enqueueNotice("Network connectivity appears offline; notifications are paused and state has been reset.")
}
}
}
func (n *discordNotifier) resetAllNotificationState(now time.Time) {
if n == nil {
return
}
// Clear per-user state so we re-seed without notifications after recovery.
n.stateMu.Lock()
n.statusByUser = nil
n.links = nil
n.linkIdx = 0
n.lastLinksRefresh = time.Time{}
n.lastSweepAt = time.Time{}
n.stateMu.Unlock()
// Clear queued pings.
n.pingMu.Lock()
n.pingQueue = nil
n.droppedQueuedLines = 0
n.lastDropNoticeAt = time.Time{}
n.pingMu.Unlock()
// Apply the same startup delay before resuming checks. When the network is
// offline, checks are already gated by isNetworkOK().
n.scheduleMu.Lock()
n.startChecksAt = now.Add(5 * time.Minute)
n.scheduleMu.Unlock()
}
func (n *discordNotifier) networkLoop(ctx context.Context) {
const (
onlineCheckInterval = 15 * time.Second
offlineCheckInterval = 15 * time.Second
streakThreshold = 4
)
for {
select {
case <-ctx.Done():
return
default:
now := time.Now()
// Ignore network connectivity during the initial boot gate to avoid
// disabling notifications due to transient startup conditions.
n.scheduleMu.Lock()
ignoreUntil := n.ignoreNetworkUntil
n.scheduleMu.Unlock()
if !ignoreUntil.IsZero() && now.Before(ignoreUntil) {
n.netMu.Lock()
n.netOKStreak = 0
n.netBadStreak = 0
n.netMu.Unlock()
select {
case <-ctx.Done():
return
case <-time.After(onlineCheckInterval):
}
continue
}
ok := checkNetworkConnectivity()
n.netMu.Lock()
if ok {
n.netOKStreak++
n.netBadStreak = 0
} else {
n.netBadStreak++
n.netOKStreak = 0
}
known := n.networkKnown
currentOK := n.networkOK
okStreak := n.netOKStreak
badStreak := n.netBadStreak
n.netMu.Unlock()
// Require a few consecutive results before flipping state to reduce
// churn from transient dial failures.
shouldFlip := false
targetOK := currentOK
if !known {
shouldFlip = true
targetOK = ok
} else if currentOK && badStreak >= streakThreshold {
shouldFlip = true
targetOK = false
} else if !currentOK && okStreak >= streakThreshold {
shouldFlip = true
targetOK = true
}
if shouldFlip {
n.setNetworkOK(targetOK, now)
}
interval := onlineCheckInterval
if !n.isNetworkOK() {
interval = offlineCheckInterval
}
select {
case <-ctx.Done():
return
case <-time.After(interval):
}
}
}
}
func checkNetworkConnectivity() bool {
// Simple routing-level test using IPs to avoid DNS dependency.
// We consider the network "up" if we can connect to any well-known host.
targets := []string{
"1.1.1.1:443", // Cloudflare
"8.8.8.8:443", // Google DNS
"9.9.9.9:443", // Quad9
}
d := net.Dialer{Timeout: 2 * time.Second}
for _, addr := range targets {
conn, err := d.Dial("tcp", addr)
if err == nil && conn != nil {
_ = conn.Close()
return true
}
}
return false
}