dongyuandou2521 2014-12-09 02:44
浏览 57

如何在Go中计算已建立的连接?

I'm trying to do basically this in Go:

netstat -an | grep 2375 -c

I need to count the number of connections to the Docker daemon in my regression test for a connection leak bug. However, because I run this in multiple places in different OS (local dev box, CI, etc), I cannot rely on the "netstat" tool, so I wonder how can I do this in a more programmatic way in Go?

I looked around the net package and could not find anything that would help. There are some libraries that basically replace netstat:

https://github.com/drael/GOnetstat https://github.com/dominikh/netstat-nat

But they are not cross-platform compliant (Mac and *nix). Any idea how can I achieve this?

  • 写回答

1条回答 默认 最新

  • douyu4535 2015-02-03 06:07
    关注

    In linux this info is exposed in the /proc filesystem. Use os.Getpid and query the info in /proc/<pid>/fd. Most likely a simple count is good here, if you need more see the proc man page.

    Cross platform compatibility for this kind of thing is going to be roll your own, as the ways of identifying open fd's for a process are very per platform. If you simply need to compile, and pass some tests for this on non linux platforms you can use Go's per platform support to make this a no-op on other platforms, or implement an appropriate solution.

    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记