-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofiling_api.go
More file actions
109 lines (95 loc) · 2.7 KB
/
profiling_api.go
File metadata and controls
109 lines (95 loc) · 2.7 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
package main
import (
"encoding/json"
"fmt"
"net/http"
"runtime"
"github.com/donomii/clusterF/types"
)
// handleProfilingAPI handles profiling control API.
func (c *Cluster) handleProfilingAPI(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
c.profilingMutex.Lock()
active := c.profilingActive
c.profilingMutex.Unlock()
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]bool{
"active": active,
})
case http.MethodPost:
body, err := types.ReadAll(r.Body)
if err != nil {
http.Error(w, "Failed to read request body", http.StatusBadRequest)
return
}
var request map[string]interface{}
if err := json.Unmarshal(body, &request); err != nil {
http.Error(w, "Invalid JSON", http.StatusBadRequest)
return
}
action, ok := request["action"].(string)
if !ok {
http.Error(w, "Missing action field", http.StatusBadRequest)
return
}
c.profilingMutex.Lock()
switch action {
case "start":
if !c.profilingActive {
if err := c.startProfiling(); err != nil {
c.profilingMutex.Unlock()
http.Error(w, fmt.Sprintf("Failed to start profiling: %v", err), http.StatusInternalServerError)
return
}
c.profilingActive = true
c.Logger().Printf("[PROFILING] Started")
}
case "stop":
if c.profilingActive {
c.stopProfiling()
c.profilingActive = false
c.Logger().Printf("[PROFILING] Stopped")
}
default:
c.profilingMutex.Unlock()
http.Error(w, "Invalid action", http.StatusBadRequest)
return
}
c.profilingMutex.Unlock()
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]interface{}{
"success": true,
"action": action,
})
default:
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
}
}
// startProfiling enables Go's built-in profiling.
func (c *Cluster) startProfiling() error {
runtime.SetBlockProfileRate(1)
runtime.SetMutexProfileFraction(1)
runtime.MemProfileRate = 512 * 1024 // Set memory profiling rate to every 512KB
c.Logger().Printf("[PROFILING] Enabled block, mutex, and memory profiling")
return nil
}
// stopProfiling disables Go's built-in profiling.
func (c *Cluster) stopProfiling() {
runtime.SetBlockProfileRate(0)
runtime.SetMutexProfileFraction(0)
runtime.MemProfileRate = 0 // Disable memory profiling
c.Logger().Printf("[PROFILING] Disabled block, mutex, and memory profiling")
}
// enableProfiling enables profiling with proper mutex handling
func (c *Cluster) enableProfiling() error {
c.profilingMutex.Lock()
defer c.profilingMutex.Unlock()
if !c.profilingActive {
if err := c.startProfiling(); err != nil {
return err
}
c.profilingActive = true
}
return nil
}