+static struct {
+ long chunk_bytes;
+ double chunk_start;
+ double sleep_adjust;
+} limit_data;
+
+static void
+limit_bandwidth_reset (void)
+{
+ limit_data.chunk_bytes = 0;
+ limit_data.chunk_start = 0;
+}
+
+/* Limit the bandwidth by pausing the download for an amount of time.
+ BYTES is the number of bytes received from the network, and TIMER
+ is the timer that started at the beginning of download. */
+
+static void
+limit_bandwidth (long bytes, struct wget_timer *timer)
+{
+ double delta_t = wtimer_read (timer) - limit_data.chunk_start;
+ double expected;
+
+ limit_data.chunk_bytes += bytes;
+
+ /* Calculate the amount of time we expect downloading the chunk
+ should take. If in reality it took less time, sleep to
+ compensate for the difference. */
+ expected = 1000.0 * limit_data.chunk_bytes / opt.limit_rate;
+
+ if (expected > delta_t)
+ {
+ double slp = expected - delta_t + limit_data.sleep_adjust;
+ double t0, t1;
+ if (slp < 200)
+ {
+ DEBUGP (("deferring a %.2f ms sleep (%ld/%.2f).\n",
+ slp, limit_data.chunk_bytes, delta_t));
+ return;
+ }
+ DEBUGP (("\nsleeping %.2f ms for %ld bytes, adjust %.2f ms\n",
+ slp, limit_data.chunk_bytes, limit_data.sleep_adjust));
+
+ t0 = wtimer_read (timer);
+ xsleep (slp / 1000);
+ wtimer_update (timer);
+ t1 = wtimer_read (timer);
+
+ /* Due to scheduling, we probably slept slightly longer (or
+ shorter) than desired. Calculate the difference between the
+ desired and the actual sleep, and adjust the next sleep by
+ that amount. */
+ limit_data.sleep_adjust = slp - (t1 - t0);
+ }