0

I'm designing a Python SSL server that needs to be able to handle up to a few thousand connections per minute. If a client doesn't send any data in a given period of time, the server should close their connection in order to free up resources.

Since I need to check if each connection has expired, would it be more efficient to make the sockets non-blocking and check all the sockets for data in a loop while simultaneously checking if they've timed out, or would it be better to use select() to get sockets that have data and maintain some kind of priority queue ordered by the time data was received on a socket to handle connection timeout?

Alternatively, is there a better method of doing this I haven't thought of, or are there existing libraries I could use that have the features I need?

4

1 回答 1

1

我会使用优先级队列来跟踪谁一直处于休眠状态。

但是请注意,如果您只想使在特定固定时间内处于非活动状态的连接超时,则实际上并不需要完整的优先级队列。您可以改用链表:

  • 链表按上次看到活动的排序顺序存储所有套接字。
  • 当一个套接字接收到数据时,您更新每个套接字的“数据最后一次出现在”成员并将其列表条目移动到列表的后面。
  • 通过select()时间,直到列表的头部过期。
  • select()循环的迭代结束时,您弹出所有过期的列表节点(它们按排序顺序)并关闭连接。

重要的是,如果您希望套接字在正确的时间到期,请使用单调时钟。如果时钟恰好在某个时间点倒退,则列表可能会丢失其排序顺序。

于 2013-06-14T03:10:20.103 回答