Ubuntu默认的open files为1024, 可以通过ulimit -n来查看;
在高并发场景下, 网络服务打开的文件描述符可能会超过这个限制, 那么我们需要来增大这个值
执行如下命令, 会增大open files值, 但是重启后会被恢复
| docker run -d --name <name> <image> sleep infinity |
| #!/bin/bash | |
| PID=35339 # 替换为你的进程 PID | |
| FD_DIR="/proc/${PID}/fd" | |
| declare -A remote_addrs # 用于统计远程地址出现次数 | |
| echo "🔍 正在分析进程 $PID 的所有 socket 连接..." | |
| for fd in $(ls "$FD_DIR" 2>/dev/null); do | |
| target=$(readlink "$FD_DIR/$fd" 2>/dev/null) |
| // delayIfStillRunning 让定时任务串行执行,上一次执行完毕后,至少等待interval后才会执行下一次 | |
| func (m *Monitor) delayIfStillRunning() cron.JobWrapper { | |
| return func(j cron.Job) cron.Job { | |
| // 保证任务的串行执行 | |
| var mu sync.RWMutex | |
| // cron会每隔interval创建一个goroutine来执行定时任务,如果简单使用mu | |
| // 来保证串行执行,并且当任务耗时比较严重时,会导致大量goroutine堆积在mu.Lock()上, | |
| // 随着服务长时间运行,可能出现goroutine耗尽,最终导致程序崩溃。事实上,只需要有一个goroutine等待上一次任务结束 | |
| var hasWaiter bool |
| #!/usr/bin/env bpftrace | |
| #include <linux/sched.h> | |
| BEGIN | |
| { | |
| printf("Trace new processes via exec() syscalls. Hit Ctrl-C to end.\n"); | |
| } | |
| tracepoint:syscalls:sys_enter_exec* |
| // 打印traceback | |
| // src/runtime/traceback.go | |
| func traceback2() { | |
| // Everything throw does should be recursively nosplit so it | |
| // can be called even when it's unsafe to grow the stack. | |
| pc := getcallerpc() | |
| sp := getcallersp() | |
| gp := getg() | |
| traceback(pc, sp, 0, gp) |
| # | |
| # /etc/sysctl.conf - Configuration file for setting system variables | |
| # See /etc/sysctl.d/ for additional system variables. | |
| # See sysctl.conf (5) for information. | |
| # | |
| #kernel.domainname = example.com | |
| # Uncomment the following to stop low-level messages on console | |
| #kernel.printk = 3 4 1 3 |