aboutsummaryrefslogtreecommitdiff
path: root/src/lib/daemon_get_timeout.c
blob: 604c52e2597f4e7bc2e3bbebaf23c046e2ed9f7c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
  This file is part of libmicrohttpd
  Copyright (C) 2007-2018 Daniel Pittman and Christian Grothoff

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

/**
 * @file lib/daemon_get_timeout.c
 * @brief function to obtain timeout for event loop
 * @author Christian Grothoff
 */
#include "internal.h"


/**
 * Obtain timeout value for polling function for this daemon.
 * This function set value to amount of milliseconds for which polling
 * function (`select()` or `poll()`) should at most block, not the
 * timeout value set for connections.
 * It is important to always use this function, even if connection
 * timeout is not set, as in some cases MHD may already have more
 * data to process on next turn (data pending in TLS buffers,
 * connections are already ready with epoll etc.) and returned timeout
 * will be zero.
 *
 * @param daemon daemon to query for timeout
 * @param timeout set to the timeout (in milliseconds)
 * @return #MHD_SC_OK on success, #MHD_SC_NO_TIMEOUT if timeouts are
 *        not used (or no connections exist that would
 *        necessitate the use of a timeout right now), otherwise
 *        an error code
 * @ingroup event
 */
enum MHD_StatusCode
MHD_daemon_get_timeout (struct MHD_Daemon *daemon,
                        MHD_UNSIGNED_LONG_LONG *timeout)
{
  time_t earliest_deadline;
  time_t now;
  struct MHD_Connection *pos;
  bool have_timeout;

  if (MHD_TM_EXTERNAL_EVENT_LOOP != daemon->threading_mode)
  {
#ifdef HAVE_MESSAGES
    MHD_DLOG (daemon,
              MHD_SC_CONFIGURATION_MISMATCH_FOR_GET_TIMEOUT,
              _ ("Illegal call to MHD_get_timeout\n"));
#endif
    return MHD_SC_CONFIGURATION_MISSMATCH_FOR_GET_TIMEOUT;
  }

  if (daemon->data_already_pending)
  {
    /* Some data already waiting to be processed. */
    *timeout = 0;
    return MHD_SC_OK;
  }

#ifdef EPOLL_SUPPORT
  if ( (MHD_ELS_EPOLL == daemon->event_loop_syscall) &&
       ((NULL != daemon->eready_head)
#if defined(UPGRADE_SUPPORT) && defined(HTTPS_SUPPORT)
        || (NULL != daemon->eready_urh_head)
#endif /* UPGRADE_SUPPORT && HTTPS_SUPPORT */
       ) )
  {
    /* Some connection(s) already have some data pending. */
    *timeout = 0;
    return MHD_SC_OK;
  }
#endif /* EPOLL_SUPPORT */

  have_timeout = false;
  earliest_deadline = 0; /* avoid compiler warnings */
  for (pos = daemon->manual_timeout_tail; NULL != pos; pos = pos->prevX)
  {
    if (0 != pos->connection_timeout)
    {
      if ( (! have_timeout) ||
           (earliest_deadline - pos->last_activity > pos->connection_timeout) )
        earliest_deadline = pos->last_activity + pos->connection_timeout;
      have_timeout = true;
    }
  }
  /* normal timeouts are sorted, so we only need to look at the 'tail' (oldest) */
  pos = daemon->normal_timeout_tail;
  if ( (NULL != pos) &&
       (0 != pos->connection_timeout) )
  {
    if ( (! have_timeout) ||
         (earliest_deadline - pos->connection_timeout > pos->last_activity) )
      earliest_deadline = pos->last_activity + pos->connection_timeout;
    have_timeout = true;
  }

  if (! have_timeout)
    return MHD_SC_NO_TIMEOUT;
  now = MHD_monotonic_sec_counter ();
  if (earliest_deadline < now)
    *timeout = 0;
  else
  {
    const time_t second_left = earliest_deadline - now;
    if (second_left > ULLONG_MAX / 1000)   /* Ignore compiler warning: 'second_left' is always positive. */
      *timeout = ULLONG_MAX;
    else
      *timeout = 1000LL * second_left;
  }
  return MHD_SC_OK;
}

/* end of daemon_get_timeout.c */