]> sjero.net Git - wget/commitdiff
[svn] Improved --limit-rate for small bandwidths.
authorhniksic <devnull@localhost>
Mon, 15 Sep 2003 21:48:43 +0000 (14:48 -0700)
committerhniksic <devnull@localhost>
Mon, 15 Sep 2003 21:48:43 +0000 (14:48 -0700)
Message-ID: <m3znh5n2og.fsf@hniksic.iskon.hr>

src/ChangeLog
src/connect.c
src/retr.c

index b3a321fe3768808d92e561656485380df66adde5..4b7b79e4e647086c8957452c22956a3c663b855c 100644 (file)
@@ -1,3 +1,12 @@
+2003-09-15  Hrvoje Niksic  <hniksic@xemacs.org>
+
+       * retr.c (get_contents): Reduce the buffer size to the amount of
+       data that may pass through for one second.  This prevents long
+       sleeps when limiting bandwidth.
+
+       * connect.c (connect_to_one): Reduce the socket's RCVBUF when
+       bandwidth limitation to small values is requested.
+
 2003-09-15  Hrvoje Niksic  <hniksic@xemacs.org>
 
        * progress.c (update_speed_ring): Moved the speed ring update to a
index 26dc404d830978ef81527d00e953450b137ec882..7ac7d3fba1321d135a47eb431330d0e91bd6660f 100644 (file)
@@ -176,6 +176,20 @@ connect_to_one (ip_address *addr, unsigned short port, int silent)
   if (sock < 0)
     goto out;
 
+#ifdef SO_RCVBUF
+  /* For very small rate limits, set the buffer size (and hence,
+     hopefully, the size of the kernel window) to the size of the
+     limit.  */
+  if (opt.limit_rate && opt.limit_rate < 8192)
+    {
+      int bufsize = opt.limit_rate;
+      if (bufsize < 512)
+       bufsize = 512;
+      setsockopt (sock, SOL_SOCKET, SO_RCVBUF,
+                 (char *)&bufsize, sizeof (bufsize));
+    }
+#endif
+
   resolve_bind_address ();
   if (bind_address_resolved)
     {
@@ -292,9 +306,12 @@ bindport (unsigned short *port, int family)
 
   if ((msock = socket (family, SOCK_STREAM, 0)) < 0)
     return CONSOCKERR;
+
+#ifdef SO_REUSEADDR
   if (setsockopt (msock, SOL_SOCKET, SO_REUSEADDR,
                  (char *)&optval, sizeof (optval)) < 0)
     return CONSOCKERR;
+#endif
 
   resolve_bind_address ();
   wget_sockaddr_set_address (&srv, ip_default_family, htons (*port),
index e619f1cbb74b9ef5b1d50131d8d4ad957c5a6684..4328aad661f024f3321db1d7af6a8fa860150029 100644 (file)
@@ -138,7 +138,10 @@ get_contents (int fd, FILE *fp, long *len, long restval, long expected,
              struct rbuf *rbuf, int use_expected, double *elapsed)
 {
   int res = 0;
-  static char c[16384];
+
+  static char dlbuf[16384];
+  int dlbufsize = sizeof (dlbuf);
+
   void *progress = NULL;
   struct wget_timer *timer = wtimer_allocate ();
   double dltime = 0, last_dltime = 0;
@@ -151,9 +154,9 @@ get_contents (int fd, FILE *fp, long *len, long restval, long expected,
   if (rbuf && RBUF_FD (rbuf) == fd)
     {
       int sz = 0;
-      while ((res = rbuf_flush (rbuf, c, sizeof (c))) != 0)
+      while ((res = rbuf_flush (rbuf, dlbuf, sizeof (dlbuf))) != 0)
        {
-         fwrite (c, sizeof (char), res, fp);
+         fwrite (dlbuf, 1, res, fp);
          *len += res;
          sz += res;
        }
@@ -172,6 +175,11 @@ get_contents (int fd, FILE *fp, long *len, long restval, long expected,
     limit_bandwidth_reset ();
   wtimer_reset (timer);
 
+  /* If we're limiting the download, set our buffer size to the
+     limit.  */
+  if (opt.limit_rate && opt.limit_rate < dlbufsize)
+    dlbufsize = opt.limit_rate;
+
   /* Read from fd while there is available data.
 
      Normally, if expected is 0, it means that it is not known how
@@ -180,18 +188,17 @@ get_contents (int fd, FILE *fp, long *len, long restval, long expected,
   while (!use_expected || (*len < expected))
     {
       int amount_to_read = (use_expected
-                           ? MIN (expected - *len, sizeof (c))
-                           : sizeof (c));
+                           ? MIN (expected - *len, dlbufsize) : dlbufsize);
 #ifdef HAVE_SSL
       if (rbuf->ssl!=NULL)
-       res = ssl_iread (rbuf->ssl, c, amount_to_read);
+       res = ssl_iread (rbuf->ssl, dlbufsize, amount_to_read);
       else
 #endif /* HAVE_SSL */
-       res = iread (fd, c, amount_to_read);
+       res = iread (fd, dlbuf, amount_to_read);
 
       if (res > 0)
        {
-         fwrite (c, sizeof (char), res, fp);
+         fwrite (dlbuf, 1, res, fp);
          /* Always flush the contents of the network packet.  This
             should not be adverse to performance, as the network
             packets typically won't be too tiny anyway.  */