refactor: take time only once when computing UPD timeouts

This commit is contained in:
yrutschle 2021-08-05 17:05:08 +02:00
parent 63f9c4a582
commit e4936454c5
3 changed files with 5 additions and 5 deletions

View File

@ -462,12 +462,14 @@ void cnx_accept_process(struct select_info* fd_info, struct listen_endpoint* lis
void udp_timeouts(struct select_info* fd_info)
{
time_t now = time(NULL);
for (int i = 0; i < fd_info->max_fd; i++) {
/* if it's either in read or write set, there is a connection
* behind that file descriptor */
if (FD_ISSET(i, &fd_info->fds_r) || FD_ISSET(i, &fd_info->fds_w)) {
struct connection* cnx = collection_get_cnx_from_fd(fd_info->collection, i);
if (cnx && udp_timedout(cnx)) {
if (cnx && udp_timedout(now, cnx)) {
close(cnx->target_sock);
FD_CLR(i, &fd_info->fds_r);
FD_CLR(i, &fd_info->fds_w);

View File

@ -130,10 +130,8 @@ void udp_s2c_forward(struct connection* cnx)
/* Checks if a connection timed out, in which case close the socket and return
* 1; otherwise return 0. */
int udp_timedout(struct connection* cnx)
int udp_timedout(time_t now, struct connection* cnx)
{
time_t now = time(NULL);
if (cnx->type != SOCK_DGRAM) return 0; /* Not a UDP connection */
if ((now - cnx->last_active > cnx->proto->udp_timeout)) {

View File

@ -22,6 +22,6 @@ void udp_s2c_forward(struct connection* cnx);
/* Checks if a connection timed out, in which case close the socket and return
* 1; otherwise return 0. */
int udp_timedout(struct connection* cnx);
int udp_timedout(time_t now, struct connection* cnx);
#endif /* UDPLISTENER_H */