]> git.sesse.net Git - skvidarsync/commitdiff
If nothing is happening, do some fake requests to keep the HTTP/TLS connections alive.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 11 Nov 2023 17:01:19 +0000 (18:01 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 11 Nov 2023 17:36:04 +0000 (18:36 +0100)
bin/sync.pl

index d8866a6c493f1fb3a65b2eab06fb731d39ce6854..3d355105bf05dcc1e0fca3380975f741bcae298f 100644 (file)
@@ -828,6 +828,7 @@ sub run {
 
        my $elapsed = Time::HiRes::tv_interval($total_start);
        printf "Tok %.0f ms.\n", 1e3 * $elapsed;
+       print "\n";
 }
 
 # Initialize the handles we need for communication.
@@ -838,13 +839,13 @@ if ($#ARGV >= 0 && $ARGV[0] eq '--daemon') {
        run($dbh, $ua);
 
        while (1) {
-               while (!defined($dbh)) {
+               while (!defined($dbh) || !$dbh->ping) {
                        print STDERR "Database connection lost, reconnecting...\n";
                        sleep 1;
                        $dbh = db_connect();
                }
                my $s = IO::Select->new($dbh->{pg_socket});
-               my @ready = $s->can_read(10.0);
+               my @ready = $s->can_read(150.0);  # slack.com HTTP timeout is ~3 minutes, sheets.googleapis.com is ~4 minutes.
                my @exceptions = $s->has_exception(0.0);
 
                if (scalar @exceptions > 0) {
@@ -860,6 +861,20 @@ if ($#ARGV >= 0 && $ARGV[0] eq '--daemon') {
                                warn "Died with: $@";
                                $dbh = undef;
                        }
+               } else {
+                       # Keep the connections alive and the token in the database fresh.
+                       # (The two URLs we use don't really exist. Note that the first time,
+                       # we might be making the initial connection to slack.com, since it's
+                       # not a given that run() needed to talk to them.)
+                       get_oauth_bearer_token($dbh, $ua);
+                       $dbh->commit;
+                       #my $start = [Time::HiRes::gettimeofday];
+                       $ua->get('https://sheets.googleapis.com/ping');
+                       #log_timing($start, 'sheets.googleapis.com (keepalive)');
+                       #$start = [Time::HiRes::gettimeofday];
+                       $ua->get('https://slack.com/api/ping');
+                       #log_timing($start, 'slack.com (keepalive)');
+                       #print STDERR "\n";
                }
        }
 } elsif ($#ARGV >= 0 && $ARGV[0] eq '--benchmark') {