记goroutine泄露

程序说明 监听 TCP 与 UDP 服务,TCP 服务保持客户端长连接,UDP 服务由业务端调用发消息到客户端。 发现问题 通过监控系统发现 goroutine 数量居高不下,正常情况下是一个用户一个 goroutine 才是,明显出现了异常情况。 定位问题 这种情况应该是次 goroutine 泄露现象,需要知道哪个位置导致的。 初步猜测 TCP 连接断开重连导致,翻看日志,看到存在很多高频率重连,只是不符合导致暴涨现象。 回头直接看 goroutine 占用情况,方式 https://caiwp.github.io/post/golang利用pprof定位问题/ 原因也就一目了然,是 UDP 服务发全服消息时调用的 goroutine 无法执行结束。 上代码 // Handle 执行 func (t *UdpTransport) Handle(ctx context.Context, addr net.Addr, reader *Reader) { ctx, cancel := context.WithTimeout(ctx, 5*time.Second) defer cancel() ch := make(chan struct{}, 0) go func() { t.run(ctx, addr, reader) ch <- struct{}{} // ERROR 会堵在这 }() select { case <-ch: case <-ctx.
Read more →

火焰图

火焰图安装与使用
Read more →