* Determine how long should we let ServerLoop sleep. * * In normal conditions we wait at most one minute, to ensure that the other * background tasks handled by ServerLoop get done even when no requests are * arriving. However, if there are background workers waiting to be started, * we don't actually sleep so that they are quickly serviced. Other exception * cases are as shown in the code
| 1848 | * cases are as shown in the code. |
| 1849 | */ |
| 1850 | static void |
| 1851 | DetermineSleepTime(struct timeval *timeout) |
| 1852 | { |
| 1853 | TimestampTz next_wakeup = 0; |
| 1854 | |
| 1855 | /* |
| 1856 | * Normal case: either there are no background workers at all, or we're in |
| 1857 | * a shutdown sequence (during which we ignore bgworkers altogether). |
| 1858 | */ |
| 1859 | if (Shutdown > NoShutdown || |
| 1860 | (!StartWorkerNeeded && !HaveCrashedWorker)) |
| 1861 | { |
| 1862 | if (AbortStartTime != 0) |
| 1863 | { |
| 1864 | /* time left to abort; clamp to 0 in case it already expired */ |
| 1865 | timeout->tv_sec = SIGKILL_CHILDREN_AFTER_SECS - |
| 1866 | (time(NULL) - AbortStartTime); |
| 1867 | timeout->tv_sec = Max(timeout->tv_sec, 0); |
| 1868 | timeout->tv_usec = 0; |
| 1869 | } |
| 1870 | else |
| 1871 | { |
| 1872 | timeout->tv_sec = 60; |
| 1873 | timeout->tv_usec = 0; |
| 1874 | } |
| 1875 | return; |
| 1876 | } |
| 1877 | |
| 1878 | if (StartWorkerNeeded) |
| 1879 | { |
| 1880 | timeout->tv_sec = 0; |
| 1881 | timeout->tv_usec = 0; |
| 1882 | return; |
| 1883 | } |
| 1884 | |
| 1885 | if (HaveCrashedWorker) |
| 1886 | { |
| 1887 | slist_mutable_iter siter; |
| 1888 | |
| 1889 | /* |
| 1890 | * When there are crashed bgworkers, we sleep just long enough that |
| 1891 | * they are restarted when they request to be. Scan the list to |
| 1892 | * determine the minimum of all wakeup times according to most recent |
| 1893 | * crash time and requested restart interval. |
| 1894 | */ |
| 1895 | slist_foreach_modify(siter, &BackgroundWorkerList) |
| 1896 | { |
| 1897 | RegisteredBgWorker *rw; |
| 1898 | TimestampTz this_wakeup; |
| 1899 | |
| 1900 | rw = slist_container(RegisteredBgWorker, rw_lnode, siter.cur); |
| 1901 | |
| 1902 | if (rw->rw_crashed_at == 0) |
| 1903 | continue; |
| 1904 | |
| 1905 | if (rw->rw_worker.bgw_restart_time == BGW_NEVER_RESTART |
| 1906 | || rw->rw_terminate) |
| 1907 | { |
no test coverage detected