Skip to content

Instantly share code, notes, and snippets.

View garenchan's full-sized avatar
:dependabot:
Focusing

Garen Chan garenchan

:dependabot:
Focusing
View GitHub Profile
@garenchan
garenchan / list-remote.sh
Created July 15, 2025 04:31
linux下查看进程连接的远程地址
#!/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)
@garenchan
garenchan / cron.go
Last active July 2, 2025 06:27
github.com/robfig/cron/v3 定时任务串行,并尽可能保证间隔interval执行
// 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
@garenchan
garenchan / bt
Last active July 20, 2023 02:48
execsnoop.bt
#!/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*
@garenchan
garenchan / go
Created May 7, 2022 08:20
go-utils
// 打印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)
@garenchan
garenchan / increase-open-files.md
Last active July 13, 2018 06:43
increase open files

增大open-files

Ubuntu默认的open files为1024, 可以通过ulimit -n来查看;

在高并发场景下, 网络服务打开的文件描述符可能会超过这个限制, 那么我们需要来增大这个值

临时性增大

执行如下命令, 会增大open files值, 但是重启后会被恢复

@garenchan
garenchan / sysctl.conf
Last active July 13, 2018 04:01
linux web server tuning
#
# /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