-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathfd_monitor.go
More file actions
171 lines (153 loc) · 4.5 KB
/
Copy pathfd_monitor.go
File metadata and controls
171 lines (153 loc) · 4.5 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
package main
import (
"bufio"
"io"
"lyrics-api-go/logcolors"
"lyrics-api-go/services/notifier"
"os"
"runtime"
"strings"
"sync/atomic"
"syscall"
"time"
log "github.com/sirupsen/logrus"
)
const (
fdMonitorInterval = 2 * time.Minute
fdAlertThresholdRatio = 0.8 // alert once open fds reach 80% of the limit
)
var fdAlertNotified atomic.Bool
// startFDMonitor launches a background goroutine that watches the process's
// open file-descriptor count against RLIMIT_NOFILE.
//
// This exists because a leak of idle/stuck connections once exhausted the fd
// limit and took the API fully down with "accept: too many open files". A slow
// climb like that is invisible until it hits the ceiling, so this fires a
// one-time notification at 80% (with a socket-state breakdown) while there is
// still headroom to react.
func startFDMonitor() {
limit := getFDLimit()
if limit == 0 {
log.Warnf("%s FD monitor disabled: could not read RLIMIT_NOFILE", logcolors.LogMemory)
return
}
go func() {
for {
open := getOpenFDCount()
if open < 0 {
// /proc/self/fd unavailable (non-Linux dev host): nothing to watch.
time.Sleep(fdMonitorInterval)
continue
}
pct := float64(open) / float64(limit) * 100
if fdThresholdExceeded(open, limit) {
states := getSocketStates()
log.Warnf("%s FD usage high: %d/%d (%.0f%%) | sockets: %v | goroutines: %d",
logcolors.LogMemoryAlert, open, limit, pct, states, runtime.NumGoroutine())
if fdAlertNotified.CompareAndSwap(false, true) {
notifier.PublishFDThresholdExceeded(open, limit, map[string]interface{}{
"percent": int(pct),
"socket_states": states,
"goroutines": runtime.NumGoroutine(),
})
}
} else {
fdAlertNotified.Store(false)
log.Infof("%s FDs: %d/%d (%.0f%%)", logcolors.LogMemory, open, limit, pct)
}
time.Sleep(fdMonitorInterval)
}
}()
log.Infof("%s FD monitor started (limit: %d, alert: %.0f%%, interval: %v)",
logcolors.LogMemory, limit, fdAlertThresholdRatio*100, fdMonitorInterval)
}
// getOpenFDCount returns the number of open file descriptors for this process,
// or -1 when /proc is unavailable (e.g. macOS development).
func getOpenFDCount() int {
return countDirEntries("/proc/self/fd")
}
// countDirEntries returns the number of entries in dir, or -1 on error.
func countDirEntries(dir string) int {
entries, err := os.ReadDir(dir)
if err != nil {
return -1
}
return len(entries)
}
// getFDLimit returns the soft RLIMIT_NOFILE for this process, or 0 on error.
func getFDLimit() uint64 {
var rl syscall.Rlimit
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rl); err != nil {
return 0
}
return rl.Cur
}
// fdThresholdExceeded reports whether open fds have reached the alert ratio of limit.
func fdThresholdExceeded(open int, limit uint64) bool {
if open < 0 || limit == 0 {
return false
}
return float64(open) >= float64(limit)*fdAlertThresholdRatio
}
// getSocketStates returns a count of TCP sockets by connection state, merged
// across IPv4 and IPv6. Empty when /proc is unavailable.
//
// Note: /proc/self/net/tcp reflects the whole network namespace, not only fds
// this process holds. In production (one process per container/namespace) that
// is effectively per-process; on a shared-netns host the counts include peers.
func getSocketStates() map[string]int {
merged := make(map[string]int)
for _, path := range []string{"/proc/self/net/tcp", "/proc/self/net/tcp6"} {
f, err := os.Open(path)
if err != nil {
continue
}
for state, n := range parseSocketStates(f) {
merged[state] += n
}
f.Close()
}
return merged
}
// parseSocketStates parses the /proc/net/tcp format and counts rows by TCP state.
func parseSocketStates(r io.Reader) map[string]int {
states := make(map[string]int)
scanner := bufio.NewScanner(r)
for scanner.Scan() {
fields := strings.Fields(scanner.Text())
if len(fields) < 4 || fields[0] == "sl" {
continue // too short, or the header row
}
states[tcpStateName(fields[3])]++
}
return states
}
// tcpStateName maps the hex state from /proc/net/tcp to its human name.
func tcpStateName(hex string) string {
switch strings.ToUpper(hex) {
case "01":
return "ESTABLISHED"
case "02":
return "SYN_SENT"
case "03":
return "SYN_RECV"
case "04":
return "FIN_WAIT1"
case "05":
return "FIN_WAIT2"
case "06":
return "TIME_WAIT"
case "07":
return "CLOSE"
case "08":
return "CLOSE_WAIT"
case "09":
return "LAST_ACK"
case "0A":
return "LISTEN"
case "0B":
return "CLOSING"
default:
return "STATE_" + hex
}
}