* Increment the network transfer counter by the given number of bytes, * and sleep if necessary to comply with the requested network transfer * rate. */
| 2092 | * rate. |
| 2093 | */ |
| 2094 | static void |
| 2095 | throttle(size_t increment) |
| 2096 | { |
| 2097 | TimeOffset elapsed_min; |
| 2098 | |
| 2099 | if (throttling_counter < 0) |
| 2100 | return; |
| 2101 | |
| 2102 | throttling_counter += increment; |
| 2103 | if (throttling_counter < throttling_sample) |
| 2104 | return; |
| 2105 | |
| 2106 | /* How much time should have elapsed at minimum? */ |
| 2107 | elapsed_min = elapsed_min_unit * |
| 2108 | (throttling_counter / throttling_sample); |
| 2109 | |
| 2110 | /* |
| 2111 | * Since the latch could be set repeatedly because of concurrently WAL |
| 2112 | * activity, sleep in a loop to ensure enough time has passed. |
| 2113 | */ |
| 2114 | for (;;) |
| 2115 | { |
| 2116 | TimeOffset elapsed, |
| 2117 | sleep; |
| 2118 | int wait_result; |
| 2119 | |
| 2120 | /* Time elapsed since the last measurement (and possible wake up). */ |
| 2121 | elapsed = GetCurrentTimestamp() - throttled_last; |
| 2122 | |
| 2123 | /* sleep if the transfer is faster than it should be */ |
| 2124 | sleep = elapsed_min - elapsed; |
| 2125 | if (sleep <= 0) |
| 2126 | break; |
| 2127 | |
| 2128 | ResetLatch(MyLatch); |
| 2129 | |
| 2130 | /* We're eating a potentially set latch, so check for interrupts */ |
| 2131 | CHECK_FOR_INTERRUPTS(); |
| 2132 | |
| 2133 | /* |
| 2134 | * (TAR_SEND_SIZE / throttling_sample * elapsed_min_unit) should be |
| 2135 | * the maximum time to sleep. Thus the cast to long is safe. |
| 2136 | */ |
| 2137 | wait_result = WaitLatch(MyLatch, |
| 2138 | WL_LATCH_SET | WL_TIMEOUT | WL_EXIT_ON_PM_DEATH, |
| 2139 | (long) (sleep / 1000), |
| 2140 | WAIT_EVENT_BASE_BACKUP_THROTTLE); |
| 2141 | |
| 2142 | if (wait_result & WL_LATCH_SET) |
| 2143 | CHECK_FOR_INTERRUPTS(); |
| 2144 | |
| 2145 | /* Done waiting? */ |
| 2146 | if (wait_result & WL_TIMEOUT) |
| 2147 | break; |
| 2148 | } |
| 2149 | |
| 2150 | /* |
| 2151 | * As we work with integers, only whole multiple of throttling_sample was |
no test coverage detected