-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: 当网络异常时,如果配置了自动重连, 还是执行一次自动重连. #607
base: master
Are you sure you want to change the base?
Conversation
@@ -184,6 +184,7 @@ static void connect_timeout_cb(htimer_t* timer) { | |||
static void reconnect_timer_cb(htimer_t* timer) { | |||
mqtt_client_t* cli = (mqtt_client_t*)hevent_userdata(timer); | |||
if (cli == NULL) return; | |||
if(cli->timer)htimer_del(cli->timer); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
一次性定时器,到期后内部会自己释放,可以不用显式删除
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我知道,但多次重连会多次创建。 因此这里还是需要显式删除的。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reconnect_timer_cb这个回调触发说明这个定时器已经到期了,回调执行完后内部会自动释放掉的。
多次创建,再保存的也是下次创建出来的了,上次的已经释放了。
@@ -491,7 +492,15 @@ int mqtt_client_connect(mqtt_client_t* cli, const char* host, int port, int ssl) | |||
cli->port = port; | |||
cli->ssl = ssl; | |||
hio_t* io = hio_create_socket(cli->loop, host, port, HIO_TYPE_TCP, HIO_CLIENT_SIDE); | |||
if (io == NULL) return -1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
想了解下你是什么原因/场景会导致返回错误?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
场景一: 正常情况下互联网断开,比如插拔网线,重启网卡等操作、网络掉线。 会导致hio_create_socket 返回 null。重连函数 只是简单的调用了一次 连接方法。 造成的现象是。 网络掉线一次,哪怕后续路由器重新拨号上了网络。重连也不会再继续了。
场景二: 作为 windows 后台服务运行的时候,可能windows 进入桌面了 网络还没有就绪。此时后台服务早已经启动。因为 连接函数 中断在 hio_t* io = hio_create_socket(cli->loop, host, port, HIO_TYPE_TCP, HIO_CLIENT_SIDE); 返回的nullptr 而导致配置的重连机制不会生效。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
应该是域名解析这一步报错了?正常来说socket创建是不会报错的
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
是的。
之前存在的问题, 如果网络异常导致的连接断开,会收到一次onClose. 如果配置了自动重连会去重连. 然而 当重连的时候 socket 创建失败(网络异常导致). 则无法收到任何异常信息,且不会再次执行重连。
本次修改后, 即使网卡未就绪,网络连接未通,如果配置了重连,依旧会根据重连规则去重试,如果未配置自动重连,则返回-1.
即使是网络异常导致的产生了一次onClose 重连失败时,重连动作依有效。