@@ -168,6 +168,7 @@ def initialize(config_or_deprecated_connection, deprecated_logger = nil, depreca
168
168
@default_timezone = self . class . validate_default_timezone ( @config [ :default_timezone ] )
169
169
170
170
@raw_connection_dirty = false
171
+ @last_activity = nil
171
172
@verified = false
172
173
end
173
174
@@ -343,6 +344,13 @@ def seconds_idle # :nodoc:
343
344
Process . clock_gettime ( Process ::CLOCK_MONOTONIC ) - @idle_since
344
345
end
345
346
347
+ # Seconds since this connection last communicated with the server
348
+ def seconds_since_last_activity # :nodoc:
349
+ if @raw_connection && @last_activity
350
+ Process . clock_gettime ( Process ::CLOCK_MONOTONIC ) - @last_activity
351
+ end
352
+ end
353
+
346
354
def unprepared_statement
347
355
cache = prepared_statements_disabled_cache . add? ( object_id ) if @prepared_statements
348
356
yield
@@ -670,6 +678,7 @@ def reconnect!(restore_transactions: false)
670
678
671
679
enable_lazy_transactions!
672
680
@raw_connection_dirty = false
681
+ @last_activity = Process . clock_gettime ( Process ::CLOCK_MONOTONIC )
673
682
@verified = true
674
683
675
684
reset_transaction ( restore : restore_transactions ) do
@@ -689,6 +698,7 @@ def reconnect!(restore_transactions: false)
689
698
end
690
699
end
691
700
701
+ @last_activity = nil
692
702
@verified = false
693
703
694
704
raise translated_exception
@@ -763,6 +773,7 @@ def verify!
763
773
@raw_connection = @unconfigured_connection
764
774
@unconfigured_connection = nil
765
775
configure_connection
776
+ @last_activity = Process . clock_gettime ( Process ::CLOCK_MONOTONIC )
766
777
@verified = true
767
778
return
768
779
end
@@ -1033,6 +1044,7 @@ def with_raw_connection(allow_retry: false, materialize_transactions: true)
1033
1044
# Barring a known-retryable error inside the query (regardless of
1034
1045
# whether we were in a _position_ to retry it), we should infer that
1035
1046
# there's likely a real problem with the connection.
1047
+ @last_activity = nil
1036
1048
@verified = false
1037
1049
end
1038
1050
@@ -1047,6 +1059,7 @@ def with_raw_connection(allow_retry: false, materialize_transactions: true)
1047
1059
# `with_raw_connection` block only when the block is guaranteed to
1048
1060
# exercise the raw connection.
1049
1061
def verified!
1062
+ @last_activity = Process . clock_gettime ( Process ::CLOCK_MONOTONIC )
1050
1063
@verified = true
1051
1064
end
1052
1065
0 commit comments