ruby_timeout_safe 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/ruby_timeout_safe/version.rb +1 -1
- data/lib/ruby_timeout_safe.rb +16 -12
- data/ruby-timeout-safe.gemspec +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5d30dd51132db09c3107ad7e6fc71d95bd7c85ae0f3eff1959234075824f972c
|
4
|
+
data.tar.gz: eb1ac80ea077fd5203aac39bc2c613aed7dc7890e3c61d1c6048695e2a1fd712
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7f76f10c6b55a0975f16be1f1c0320afa7544a7db6191e860f09923da550449fff49beabebc5f1f7efc05829bbf8c84795271dd67357024a8149db8c471deb69
|
7
|
+
data.tar.gz: 5b6b833ee878f43f13c17573d82f49cda53c69d4fa6d5cb1938aa4a90ff5f409b1eb9373b164a09351d51605be106c31000dd07003e398cb0294e05dc454c51d
|
data/lib/ruby_timeout_safe.rb
CHANGED
@@ -5,21 +5,25 @@ require_relative 'ruby_timeout_safe/version'
|
|
5
5
|
|
6
6
|
# A safe timeout implementation for Ruby using monotonic time.
|
7
7
|
module RubyTimeoutSafe
|
8
|
-
def self.timeout(seconds) # rubocop:disable Metrics/MethodLength
|
9
|
-
|
8
|
+
def self.timeout(seconds=nil) # rubocop:disable Metrics/MethodLength
|
9
|
+
return yield if seconds.nil? || seconds.zero?
|
10
|
+
|
11
|
+
raise ArgumentError, 'timeout value must be at least 0.1 second' if seconds < 0.1
|
12
|
+
current_thread = Thread.current
|
10
13
|
|
11
14
|
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
15
|
+
s_thread = Thread.new do
|
16
|
+
loop do
|
17
|
+
elapsed_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time
|
18
|
+
break if elapsed_time >= seconds
|
16
19
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
elapsed_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time
|
21
|
-
remaining_time = seconds - elapsed_time
|
22
|
-
thread.kill if remaining_time.positive?
|
20
|
+
sleep(0.05) # Sleep briefly to prevent busy-waiting
|
21
|
+
end
|
22
|
+
current_thread.raise Timeout::Error, 'execution expired'
|
23
23
|
end
|
24
|
+
|
25
|
+
yield
|
26
|
+
ensure
|
27
|
+
s_thread.kill if s_thread&.alive?
|
24
28
|
end
|
25
29
|
end
|
data/ruby-timeout-safe.gemspec
CHANGED
@@ -17,7 +17,7 @@ Gem::Specification.new do |spec|
|
|
17
17
|
|
18
18
|
The gem defines a `RubyTimeoutSafe` module with a `timeout` method that executes a given
|
19
19
|
Ruby block with a specified timeout duration. If the block execution exceeds the timeout,
|
20
|
-
a `
|
20
|
+
a `Timeout::Error` exception is raised.
|
21
21
|
|
22
22
|
This implementation leverages Ruby's built-in threading and monotonic time functions to
|
23
23
|
provide a robust timeout mechanism.
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby_timeout_safe
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- sebi
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-07-
|
11
|
+
date: 2024-07-05 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: |
|
14
14
|
ruby-timeout-safe is a Ruby library that provides a safe and reliable timeout
|
@@ -18,7 +18,7 @@ description: |
|
|
18
18
|
|
19
19
|
The gem defines a `RubyTimeoutSafe` module with a `timeout` method that executes a given
|
20
20
|
Ruby block with a specified timeout duration. If the block execution exceeds the timeout,
|
21
|
-
a `
|
21
|
+
a `Timeout::Error` exception is raised.
|
22
22
|
|
23
23
|
This implementation leverages Ruby's built-in threading and monotonic time functions to
|
24
24
|
provide a robust timeout mechanism.
|