more features

This commit is contained in:
Para Dox
2025-05-29 01:08:24 +07:00
parent 596dba9ad9
commit 3b43c15074

View File

@@ -965,6 +965,9 @@ func (sc *StatsCollector) printSummary() {
sc.totalRequests = 0 sc.totalRequests = 0
sc.totalWsConnections = 0 sc.totalWsConnections = 0
// Reset WebSocket connections to prevent memory leak
sc.wsConnections = sc.wsConnections[:0]
// Reset the interval start time for the next interval // Reset the interval start time for the next interval
sc.intervalStartTime = time.Now() sc.intervalStartTime = time.Now()
} }
@@ -1273,10 +1276,19 @@ func main() {
func handleRequest(w http.ResponseWriter, r *http.Request, backends []Backend, client *http.Client, enableDetailedLogs bool, statsCollector *StatsCollector) { func handleRequest(w http.ResponseWriter, r *http.Request, backends []Backend, client *http.Client, enableDetailedLogs bool, statsCollector *StatsCollector) {
startTime := time.Now() startTime := time.Now()
// Limit request body size to 10MB to prevent memory exhaustion
const maxBodySize = 10 * 1024 * 1024 // 10MB
r.Body = http.MaxBytesReader(w, r.Body, maxBodySize)
// Read the entire request body // Read the entire request body
body, err := io.ReadAll(r.Body) body, err := io.ReadAll(r.Body)
if err != nil { if err != nil {
// Check if the error is due to body size limit
if strings.Contains(err.Error(), "request body too large") {
http.Error(w, "Request body too large (max 10MB)", http.StatusRequestEntityTooLarge)
} else {
http.Error(w, "Error reading request body", http.StatusBadRequest) http.Error(w, "Error reading request body", http.StatusBadRequest)
}
return return
} }
defer r.Body.Close() defer r.Body.Close()