Skip to content

Commit

Permalink
Apply temporary TCP optimizations for Linux to handle massive connect…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
Musixal committed Oct 7, 2024
1 parent 3da1f16 commit 04f4526
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
56 changes: 56 additions & 0 deletions cmd/optimization.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package cmd

import (
"os/exec"
"runtime"
"syscall"
)

// applyTCPTuning applies temporary TCP optimizations for Linux to handle massive connections
func ApplyTCPTuning() {
if runtime.GOOS == "linux" {
logger.Info("Applying TCP optimizations for Linux...")

// Commands for optimizing TCP parameters
commands := [][]string{
{"sysctl", "-w", "net.ipv4.ip_local_port_range=1024 65535"}, // Increase ephemeral ports
{"sysctl", "-w", "net.ipv4.tcp_tw_reuse=1"}, // Reuse TIME_WAIT sockets
{"sysctl", "-w", "net.ipv4.tcp_fin_timeout=15"}, // Reduce TCP FIN timeout
{"sysctl", "-w", "net.core.somaxconn=4096"}, // Increase max queue length of incoming connections
{"sysctl", "-w", "net.ipv4.tcp_max_syn_backlog=8192"}, // Increase SYN request backlog
{"sysctl", "-w", "net.ipv4.tcp_window_scaling=1"}, // Enable TCP window scaling
{"sysctl", "-w", "net.ipv4.tcp_fastopen=3"}, // Enable TCP Fast Open
}

// Execute the sysctl commands
for _, cmd := range commands {
err := exec.Command(cmd[0], cmd[1:]...).Run()
if err != nil {
logger.Errorf("Failed to apply TCP tuning: %v", err)
} else {
logger.Debugf("Successfully applied: %s", cmd)
}
}

// Set file descriptor limit programmatically
var rLimit syscall.Rlimit
err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err != nil {
logger.Errorf("Error getting Rlimit: %v", err)
} else {
logger.Debugf("Current file descriptor limit: %d", rLimit.Cur)

// Set the maximum and current file descriptor limits to 1048576
rLimit.Max = 1048576
rLimit.Cur = 1048576
err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err != nil {
logger.Errorf("Error setting Rlimit: %v", err)
} else {
logger.Debugf("Successfully set file descriptor limit to: %d", rLimit.Cur)
}
}
} else {
logger.Info("Non-Linux system detected, skipping TCP optimizations.")
}
}
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ func main() {
logger.Fatalf("Usage: %s -c /path/to/config.toml", flag.CommandLine.Name())
}

// Apply temporary TCP optimizations at startup
cmd.ApplyTCPTuning()

// Create a context for graceful shutdown handling
ctx, cancel := context.WithCancel(context.Background())

Expand Down

0 comments on commit 04f4526

Please sign in to comment.