douchun1948 2018-03-06 13:31
浏览 17
已采纳

如果Go应用程序找不到外部依赖项/服务,是否应该恐慌[关闭]

How should a Golang app handle missing external dependencies ? When a app starts and it doesn't find the database it is supposed to persist the data on, knowing the app is useless in that state, should I panic the app ?

I can otherwise log infinitely something, print to stderr, or use another method to notify, but I'm not sure when to choose each method.

  • 写回答

2条回答 默认 最新

  • doupiao9318 2018-03-06 23:05
    关注

    An application that has no access to the external network service should not panic. This should be expected as networks tend to fail. I would wrap the error and pass it further.

    Consider the following scenario. You have multiple application servers connected to two database servers. You are upgrading the database servers one at a time. When one is turned off half of your application servers panicked and crashed. You upgrade the second database server and every application server is gone now. Instead, when the database is not available just report an error for instance by sending HTTP status 500. If you have a load balancer it will pass the request to the working applications servers. When the database server is back, the application servers reconnect and continue to work.

    Another scenario, you are running an interactive application that processes a database to create a report. The connection is not available. The application panicked and crashed. From the user perspective, it looks like a bug. I would expect a message that connection cannot be established.

    In the standard library it is accepted to panic when an internal resource is not available. See template.Must. This means something is wrong with the application itself.

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

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大