ruby_timeout_safe 0.1.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7fc2f4c28c6c3ab39dda99ecbddfba1e0128954b7361ae18605594b84175ca36
4
- data.tar.gz: 35b8c41ab8c1f413b493c77d1b272fb532dfcb58c16e3dfac1a2aa6c67b5bab0
3
+ metadata.gz: 851e38c367c7d9625dd267f049938fe5761b0580597ad73b18c069983a2d24aa
4
+ data.tar.gz: 768f4eb26f2eb1c5c7fe0e251999fe9e58b1f52239d5e12c16440ac6b48e25d1
5
5
  SHA512:
6
- metadata.gz: 377bda663c266dacaf4d9c18227fdd176bbe868c5911abc6cf8ef07d274ac71ba6a6bc967964acd382d1f58b99eb3489c860d82710e6e070523eac00e6c7747c
7
- data.tar.gz: 29c8399c8f0a37752bb379535d8b0118ccd9e39d11e4a3aa05afe696f21428bce5dd6fcb95ab977fefbfe6cf7ca6ce135e07d2543c1d3797f651e523199294d2
6
+ metadata.gz: d8ef40a988be4c1366a6fd344d34c7eaf1184cbb1d57d65549fc663c1731c9fee92714d8539d08e4b0f71cd391cfca1ae94d1d3816e31ec451a7604e48181bb0
7
+ data.tar.gz: 95e5857db3003d69bd876e1db2ef12f2b22171310f0c802ac634beb16bea37ea425ce48bc53366eed70cd4788dad8977f98b48a1b071f6f16849297becc698c2
data/.rspec CHANGED
@@ -1,3 +1,3 @@
1
- --format documentation
1
+ --format progress
2
2
  --color
3
3
  --require spec_helper
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RubyTimeoutSafe
4
- VERSION = '0.1.0'
4
+ VERSION = '1.0.0'
5
5
  end
@@ -5,21 +5,24 @@ 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
- raise ArgumentError, 'timeout value must be at least 1 second' if seconds.nil? || seconds < 1.0
8
+ def self.timeout(seconds = nil)
9
+ return yield if seconds.nil? || seconds.zero?
10
+
11
+ current_thread = Thread.current
10
12
 
11
13
  start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
12
- thread = Thread.new do
13
- sleep(seconds)
14
- Thread.main.raise Timeout::Error, 'execution expired'
15
- end
14
+ s_thread = Thread.new do
15
+ loop do
16
+ elapsed_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time
17
+ break if elapsed_time >= seconds
16
18
 
17
- begin
18
- yield
19
- ensure
20
- elapsed_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time
21
- remaining_time = seconds - elapsed_time
22
- thread.kill if remaining_time.positive?
19
+ Thread.pass
20
+ end
21
+ current_thread.raise Timeout::Error, 'execution expired'
23
22
  end
23
+
24
+ yield
25
+ ensure
26
+ s_thread.kill if s_thread&.alive?
24
27
  end
25
28
  end
@@ -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 `TimeoutError` exception is raised.
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.1.0
4
+ version: 1.0.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-04 00:00:00.000000000 Z
11
+ date: 2024-08-27 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 `TimeoutError` exception is raised.
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.
@@ -29,7 +29,7 @@ extensions: []
29
29
  extra_rdoc_files: []
30
30
  files:
31
31
  - ".rspec"
32
- - ".rubcop.yml"
32
+ - ".rubocop.yml"
33
33
  - CODE_OF_CONDUCT.md
34
34
  - LICENSE.txt
35
35
  - README.md
File without changes