duan5731 2017-08-04 15:18
浏览 69
已采纳

如何处理低级的net / http错误?

With my golang net/http server, I occasionally get errors like

2017/08/04 15:06:25 http: Accept error: accept tcp 127.0.0.1:80: accept4: too many open files; retrying in 1s

on stderr. There might be a bug with a missing Close() somewhere, but this is not what my question is about.

What I would like to know is whether there is an easy way to catch these or similar errors programmatically in go? I only know about this error because it's logged. ListenAndServe() does not return an error. Is there some other way to get notified of errors like these? Specifically: how do I access the errors of the underlying listener? ListenAndServe() does not return errors from the underlying listener (at least not all of them).

(edited)

  • 写回答

2条回答 默认 最新

  • drugs3550 2017-08-04 18:05
    关注

    If you look at how (*http.Server).Serve() is implemented here, it is clear that you can just define your own class that implements net.Listener and give it to the Serve() function, instead of using ListenAndServe(), which puts a default listener there. Make it panic on error and later recover from that panic in your code, or implement any other error handling logic.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度