[Rt-commit] rt branch 5.0/statement-timeout created. rt-5.0.3-145-gfbde6b177a

BPS Git Server git at git.bestpractical.com
Wed Nov 16 22:22:16 UTC 2022


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "rt".

The branch, 5.0/statement-timeout has been created
        at  fbde6b177a37a2ad679ec26dbee54de6d14ff4ba (commit)

- Log -----------------------------------------------------------------
commit fbde6b177a37a2ad679ec26dbee54de6d14ff4ba
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Thu Nov 17 06:01:28 2022 +0800

    Add config $StatementTimeout to abort long running SQL queries from web ui

diff --git a/etc/RT_Config.pm.in b/etc/RT_Config.pm.in
index bd9c001d28..2790d24968 100644
--- a/etc/RT_Config.pm.in
+++ b/etc/RT_Config.pm.in
@@ -281,6 +281,20 @@ during upgrades.
 
 Set($DatabaseAdmin, "@DB_DBA@");
 
+=item C<$StatementTimeout>
+
+Time in seconds before a SQL statement times out. This is to abort
+long running SQL that can cause performance penality.
+
+It's disabled by default.
+
+Note that this affects RT web service only and only works with
+MariaDB/MySQL/PostgreSQL.
+
+=cut
+
+Set($StatementTimeout, undef);
+
 =back
 
 
diff --git a/lib/RT/Config.pm b/lib/RT/Config.pm
index b659fbc291..e180ee1c2f 100644
--- a/lib/RT/Config.pm
+++ b/lib/RT/Config.pm
@@ -1978,6 +1978,10 @@ our %META;
     SetOutgoingMailFrom => {
         Widget => '/Widgets/Form/String',
     },
+    StatementTimeout => {
+        Immutable => 1,
+        Widget    => '/Widgets/Form/String',
+    },
     Timezone => {
         Widget => '/Widgets/Form/String',
     },
diff --git a/lib/RT/Handle.pm b/lib/RT/Handle.pm
index f557832669..294f532ad6 100644
--- a/lib/RT/Handle.pm
+++ b/lib/RT/Handle.pm
@@ -130,11 +130,23 @@ sub Connect {
     if ( $db_type eq 'mysql' ) {
         # set the character set
         $self->dbh->do("SET NAMES 'utf8mb4'");
+        if ( defined $ENV{RT_STATEMENT_TIMEOUT} && length $ENV{RT_STATEMENT_TIMEOUT} ) {
+            if ( $self->_IsMariaDB ) {
+                $self->dbh->do("SET max_statement_time = $ENV{RT_STATEMENT_TIMEOUT}");
+            }
+            else {
+                # max_execution_time is defined in milliseconds
+                $self->dbh->do( "SET max_execution_time = " . int( $ENV{RT_STATEMENT_TIMEOUT} * 1000 ) );
+            }
+        }
     }
     elsif ( $db_type eq 'Pg' ) {
         my $version = $self->DatabaseVersion;
         ($version) = $version =~ /^(\d+\.\d+)/;
         $self->dbh->do("SET bytea_output = 'escape'") if $version >= 9.0;
+        # statement_timeout is defined in milliseconds
+        $self->dbh->do( "SET statement_timeout = " . int( $ENV{RT_STATEMENT_TIMEOUT} * 1000 ) )
+            if defined $ENV{RT_STATEMENT_TIMEOUT} && length $ENV{RT_STATEMENT_TIMEOUT};
     }
 
     $self->dbh->{'LongReadLen'} = RT->Config->Get('MaxAttachmentSize');
diff --git a/sbin/rt-server.in b/sbin/rt-server.in
index 871b66c6c8..310a5927ff 100644
--- a/sbin/rt-server.in
+++ b/sbin/rt-server.in
@@ -91,6 +91,8 @@ RT->LoadConfig();
 RT->InitPluginPaths();
 RT->InitLogging();
 
+$ENV{RT_STATEMENT_TIMEOUT} = RT->Config->Get('StatementTimeout');
+
 require RT::Handle;
 my ($integrity, $state, $msg) = RT::Handle->CheckIntegrity;
 

-----------------------------------------------------------------------


hooks/post-receive
-- 
rt


More information about the rt-commit mailing list