关闭 websocket 并释放文件描述符的最快方法
我正在使用 boost monster 来建立 websocket 连接,我的一个进程可能有大量的流/连接,在终止进程时,我使用以下方法在析构函数中调用阻塞关闭每个 websocket:
if ( m_ws.next_layer().next_layer().is_open() )
{
boost::system::error_code ec;
m_ws.close(boost::beast::websocket::close_code::normal, ec);
}
拥有大量 websocket,使得终止进程长时间阻塞,有没有办法强制终止(删除)连接并更快地释放底层资源?提前致谢。
I am using boost beast for making websocket connections, a process of mine may have a large number of streams/connections and while terminating the process I am calling blocking close of each websocket in destructor using :
if ( m_ws.next_layer().next_layer().is_open() )
{
boost::system::error_code ec;
m_ws.close(boost::beast::websocket::close_code::normal, ec);
}
Having a lot of websockets, makes terminating the process blocking for a long time, is there a way to force terminate(delete) a connection and free up underlying resources faster? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如我在评论中告诉您的那样,关闭连接将是套接字上的快速操作,但它不会花费时间并阻塞线程。在你的例子中,我不知道你的程序做了多少工作来关闭每个套接字,但请记住,如果你的主线程结束,这意味着进程已经结束,SO将释放它一直在使用的所有资源,在不关闭每个套接字的情况下,我使用此技术,WebSocket 客户端会检测连接的结束,但当协议出现问题或远程端点突然断开连接时,我会关闭套接字。
共享代码以查看代码正在执行哪些其他活动可能会很有用。
顺便跟大家分享一下我的代码,我关闭websocket没有问题:
就我而言,我使用的是异步方法读取和同步方法
编写时,我没有使用异步方法来编写,以避免出现两个同时写入操作的情况。
另外,重要的是要说我正在使用异步方式接受新连接。
接受套接字连接的代码,您可以在其中设置写入和读取的 TIMES OUTS,而不是使用计时器。
要读取的代码:
要写入的代码:
如果您想查看整个代码,这是链接。该项目仍处于开发阶段,但已经有效。
As I told you in the comments, closing the connection would be a fast operation on sockets but it doesn't take time and block the thread. In your case, I don't know how much work your program does to close each socket, but keep in mind that if your main thread ends, which means that the process has ended, the SO releases all the resources that it has been using, without closing each socket, I use this technic, and the WebSocket clients detect the end of the connections, but I close the socket when I have some problems with the protocol or the remote endpoint has been disconnected abruptly.
It could be useful that you share your code to see what other activities your code is doing.
By the way, let me share with you my code, where I have no problem to close websocket:
In my case, I'm using asynchronous methods to read and synchronous methods
to write, I'm not using an asynchronous method to write to avoid the scenery of two simultaneous write operations.
Also, it's important to say that I'm using the asynchronous way to accept a new connection.
The code to accept the socket connection, where you can set the TIMES OUTS to write and read, instead of using timers.
The code to read:
The code to write:
If you want to see the whole code, this is the Link. The project is still in the developer phase, but it works.