关于mongoDB连接的问题
老师,我把docker里mongoDB是停止了,然后重启服务器,auth服务没有报错,但只要访问使用mongo的方法就会报server selection error: server selection timeout超时。
我的意思是auth.main方法里连接mongo客户端咋不报错cannot connect mongdb呀,需要额外加ping()方法检查连接超时么?
正在回答 回答被采纳积分+1
mongo,包括grpc client,都是在Connect的时候并没有真正去connect,而是放到了之后。Connect的文档说:
// The NewClient function does not do any I/O and returns an error if the given options are invalid.
// The Client.Connect method starts background goroutines to monitor the state of the deployment and does not do
// any I/O in the main goroutine to prevent the main goroutine from blocking. Therefore, it will not error if the
// deployment is down.
//
// The Client.Ping method can be used to verify that the deployment is successfully connected and the
// Client was correctly configured.
就是在Connect的时候只是开启了后台用来检测状态的goroutine,但不会i真正的去做I/O,所以不会在无法连接的时候报错。它也说了用Client.Ping可以检测链接是否健康。
相似问题
登录后可查看更多问答,登录/注册
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星