1. Shlomi Fish
  2. Maniac-Downloader

Commits

Shlomi Fish  committed 18bc9f3

Restart connections on a timeout.

  • Participants
  • Parent commits 1fdf59d
  • Branches default

Comments (0)

Files changed (3)

File App-ManiacDownloader/Changes

View file
 Revision history for App-ManiacDownloader
 
+v0.0.9  2014-04-30
+    - Restart connections on a timeout of 18 seconds.
+        - Not thoroughly tested.
+
 v0.0.8  2014-02-02
     - Fix the =encoding directive at the POD.
 

File App-ManiacDownloader/lib/App/ManiacDownloader.pm

View file
 
     sysseek( $r->_fh, $r->_start, SEEK_SET );
 
-    http_get $self->_url,
-    headers => { 'Range'
-        => sprintf("bytes=%d-%d", $r->_start, $r->_end-1)
-    },
-    on_body => sub {
+    # We do these to make sure the cancellation guard does not get
+    # preserved because it's in the context of the closures.
+    my $on_body = sub {
         my ($data, $hdr) = @_;
 
         my $ret = $r->_write_data(\$data);
             }
         }
         return $cont;
+    };
+
+    my $final_cb = sub { return ; };
+
+    my $guard = http_get $self->_url,
+    headers => { 'Range'
+        => sprintf("bytes=%d-%d", $r->_start, $r->_end-1)
     },
-    sub {
-        # Do nothing.
-        return;
-    }
-    ;
+    on_body => $on_body,
+    $final_cb;
+
+    $r->_guard($guard);
+
+    $guard = '';
+
+    return;
 }
 
+my $MAX_CHECKS = 6;
+
 sub _handle_stats_timer
 {
     my ($self) = @_;
     my ($num_dloaded, $total_downloaded)
         = $self->_downloaded->_flush_and_report;
 
-    foreach my $r (@{$self->_ranges()})
+    my $_ranges = $self->_ranges;
+    for my $idx (0 .. $#$_ranges)
     {
+        my $r = $_ranges->[$idx];
+
         $r->_flush_and_report;
+        if ($r->is_active && $r->_increment_check_count($MAX_CHECKS))
+        {
+            $r->_guard('');
+            $self->_start_connection($idx);
+        }
     }
 
     my $time = AnyEvent->now;

File App-ManiacDownloader/lib/App/ManiacDownloader/_SegmentTask.pm

View file
 has ['_start', '_end'] => (is => 'rw', isa => 'Int',);
 has '_fh' => (is => 'rw',);
 has 'is_active' => (is => 'rw', isa => 'Bool', default => 1);
+has '_downloaded' => (is => 'rw', isa => 'App::ManiacDownloader::_BytesDownloaded', default => sub { return App::ManiacDownloader::_BytesDownloaded->new; }, handles => ['_flush_and_report'],);
 
-has '_downloaded' => (is => 'rw', isa => 'App::ManiacDownloader::_BytesDownloaded', default => sub { return App::ManiacDownloader::_BytesDownloaded->new; }, handles => ['_flush_and_report'],);
+has '_guard' => (is => 'rw');
+
+has '_count_checks_without_download' => (is => 'rw', isa => 'Int', default => 0);
 
 sub _serialize
 {
     my $init_start = $self->_start;
     $self->_start($init_start + $written);
 
+    $self->_count_checks_without_download(0);
+
     return
     {
         should_continue => scalar($self->_start < $self->_end),
     close($self->_fh);
     $self->_fh(undef());
     $self->is_active(0);
+    $self->_guard('');
 
     return;
 }
     return;
 }
 
+sub _increment_check_count
+{
+    my ($self, $max_checks) = @_;
+
+    $self->_count_checks_without_download(
+        $self->_count_checks_without_download() + 1
+    );
+
+    return ($self->_count_checks_without_download >= $max_checks);
+}
 
 1;