You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spamassassin.apache.org by he...@apache.org on 2022/05/02 15:23:50 UTC

svn commit: r1900483 - /spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/DecodeShortURLs.pm

Author: hege
Date: Mon May  2 15:23:50 2022
New Revision: 1900483

URL: http://svn.apache.org/viewvc?rev=1900483&view=rev
Log:
Make TTL handling foolproof, do a cheap delete before select. Tidy things up a bit.

Modified:
    spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/DecodeShortURLs.pm

Modified: spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/DecodeShortURLs.pm
URL: http://svn.apache.org/viewvc/spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/DecodeShortURLs.pm?rev=1900483&r1=1900482&r2=1900483&view=diff
==============================================================================
--- spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/DecodeShortURLs.pm (original)
+++ spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/DecodeShortURLs.pm Mon May  2 15:23:50 2022
@@ -391,20 +391,17 @@ sub initialise_url_shortener_cache {
       ");
       $self->{sth_select} = $self->{dbh}->prepare("
         SELECT decoded_url FROM short_url_cache
-        WHERE short_url = ? AND created >= strftime('%s','now') - $conf->{url_shortener_cache_ttl}
+        WHERE short_url = ?
       ");
       $self->{sth_delete} = $self->{dbh}->prepare("
         DELETE FROM short_url_cache
+        WHERE short_url = ? AND created < strftime('%s','now') - $conf->{url_shortener_cache_ttl}
+      ");
+      $self->{sth_clean} = $self->{dbh}->prepare("
+        DELETE FROM short_url_cache
         WHERE created < strftime('%s','now') - $conf->{url_shortener_cache_ttl}
       ");
     };
-    if ($@) {
-      warn "DecodeShortURLs: cache connect failed: $@\n";
-      undef $self->{dbh};
-      undef $self->{sth_insert};
-      undef $self->{sth_select};
-      undef $self->{sth_delete};
-    }
   }
   ##
   ## MySQL/MariaDB
@@ -431,20 +428,17 @@ sub initialise_url_shortener_cache {
       ");
       $self->{sth_select} = $self->{dbh}->prepare("
         SELECT decoded_url FROM short_url_cache
-        WHERE short_url = ? AND created >= UNIX_TIMESTAMP() - $conf->{url_shortener_cache_ttl}
+        WHERE short_url = ?
       ");
       $self->{sth_delete} = $self->{dbh}->prepare("
         DELETE FROM short_url_cache
+        WHERE short_url = ? AND created < UNIX_TIMESTAMP() - $conf->{url_shortener_cache_ttl}
+      ");
+      $self->{sth_clean} = $self->{dbh}->prepare("
+        DELETE FROM short_url_cache
         WHERE created < UNIX_TIMESTAMP() - $conf->{url_shortener_cache_ttl}
       ");
     };
-    if ($@) {
-      warn "DecodeShortURLs: cache connect failed: $@\n";
-      undef $self->{dbh};
-      undef $self->{sth_insert};
-      undef $self->{sth_select};
-      undef $self->{sth_delete};
-    }
   }
   ##
   ## PostgreSQL
@@ -471,20 +465,17 @@ sub initialise_url_shortener_cache {
       ");
       $self->{sth_select} = $self->{dbh}->prepare("
         SELECT decoded_url FROM short_url_cache
-        WHERE short_url = ? AND created >= CAST(EXTRACT(epoch FROM NOW()) AS INT) - $conf->{url_shortener_cache_ttl}
+        WHERE short_url = ?
       ");
       $self->{sth_delete} = $self->{dbh}->prepare("
         DELETE FROM short_url_cache
+        WHERE short_url ? = AND created < CAST(EXTRACT(epoch FROM NOW()) AS INT) - $conf->{url_shortener_cache_ttl}
+      ");
+      $self->{sth_clean} = $self->{dbh}->prepare("
+        DELETE FROM short_url_cache
         WHERE created < CAST(EXTRACT(epoch FROM NOW()) AS INT) - $conf->{url_shortener_cache_ttl}
       ");
     };
-    if ($@) {
-      warn "DecodeShortURLs: cache connect failed: $@\n";
-      undef $self->{dbh};
-      undef $self->{sth_insert};
-      undef $self->{sth_select};
-      undef $self->{sth_delete};
-    }
   ##
   ## ...
   ##
@@ -492,6 +483,15 @@ sub initialise_url_shortener_cache {
     warn "DecodeShortURLs: invalid cache configuration\n";
     return;
   }
+
+  if ($@ || !$self->{sth_clean}) {
+    warn "DecodeShortURLs: cache connect failed: $@\n";
+    undef $self->{dbh};
+    undef $self->{sth_insert};
+    undef $self->{sth_select};
+    undef $self->{sth_delete};
+    undef $self->{sth_clean};
+  }
 }
 
 sub short_url {
@@ -596,7 +596,7 @@ sub check_dnsbl {
       && rand() < 1/$conf->{url_shortener_cache_autoclean})
   {
     dbg("cleaning stale cache entries");
-    eval { $self->{sth_delete}->execute(); };
+    eval { $self->{sth_clean}->execute(); };
     if ($@) { dbg("cache cleaning failed: $@"); }
   }
 }
@@ -696,6 +696,7 @@ sub cache_add {
   return if !$self->{dbh};
   return if length($short_url) > 256 || length($decoded_url) > 512;
 
+  # Upsert
   eval { $self->{sth_insert}->execute($short_url, $decoded_url); };
   if ($@) {
     dbg("could not add to cache: $@");
@@ -709,6 +710,16 @@ sub cache_get {
 
   return if !$self->{dbh};
 
+  # Make sure expired entries are gone.  Just a quick check for primary key,
+  # not that expensive.
+  eval { $self->{sth_delete}->execute($key); };
+  if ($@) {
+    dbg("cache delete failed: $@");
+    return;
+  }
+
+  # Now try to get it (don't bother parsing if something was deleted above,
+  # it would be rare event anyway)
   eval { $self->{sth_select}->execute($key); };
   if ($@) {
     dbg("cache get failed: $@");