frugal_timeout 0.0.3 → 0.0.4
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.
- data/lib/frugal_timeout.rb +37 -9
- metadata +2 -2
data/lib/frugal_timeout.rb
CHANGED
@@ -3,11 +3,36 @@
|
|
3
3
|
require 'thread'
|
4
4
|
require 'timeout'
|
5
5
|
|
6
|
+
#--
|
7
|
+
# {{{1 Rdoc
|
8
|
+
#++
|
9
|
+
# Timeout.timeout() replacement using only 2 threads
|
10
|
+
# = Example
|
11
|
+
#
|
12
|
+
# require 'frugal_timeout'
|
13
|
+
#
|
14
|
+
# begin
|
15
|
+
# FrugalTimeout.timeout(0.1) { sleep }
|
16
|
+
# rescue Timeout::Error
|
17
|
+
# puts 'it works!'
|
18
|
+
# end
|
19
|
+
#
|
20
|
+
# # Ensure that calling timeout() will use FrugalTimeout.timeout().
|
21
|
+
# FrugalTimeout.dropin!
|
22
|
+
#
|
23
|
+
# # Rescue frugal-specific exception if needed.
|
24
|
+
# begin
|
25
|
+
# timeout(0.1) { sleep }
|
26
|
+
# rescue FrugalTimeout::Error
|
27
|
+
# puts 'yay!'
|
28
|
+
# end
|
29
|
+
#--
|
30
|
+
# }}}1
|
6
31
|
module FrugalTimeout
|
7
|
-
# {{{
|
32
|
+
# {{{2 Error
|
8
33
|
class Error < Timeout::Error; end
|
9
34
|
|
10
|
-
# {{{
|
35
|
+
# {{{2 Request
|
11
36
|
class Request
|
12
37
|
include Comparable
|
13
38
|
@@mutex = Mutex.new
|
@@ -35,10 +60,10 @@ module FrugalTimeout
|
|
35
60
|
end
|
36
61
|
end
|
37
62
|
|
38
|
-
# {{{
|
63
|
+
# {{{2 Main code
|
39
64
|
@in = Queue.new
|
40
65
|
|
41
|
-
# {{{
|
66
|
+
# {{{3 Timeout request and expiration processing thread
|
42
67
|
Thread.new {
|
43
68
|
nearestTimeout, requests = nil, []
|
44
69
|
loop {
|
@@ -84,7 +109,7 @@ module FrugalTimeout
|
|
84
109
|
}
|
85
110
|
}
|
86
111
|
|
87
|
-
# {{{
|
112
|
+
# {{{3 Closest expiration notifier thread
|
88
113
|
@sleeperDelays, @sleeperMutex = Queue.new, Mutex.new
|
89
114
|
@sleeper = Thread.new {
|
90
115
|
loop {
|
@@ -100,8 +125,9 @@ module FrugalTimeout
|
|
100
125
|
}
|
101
126
|
}
|
102
127
|
|
103
|
-
# {{{
|
104
|
-
|
128
|
+
# {{{3 Methods
|
129
|
+
|
130
|
+
# Ensure that calling timeout() will use FrugalTimeout.timeout()
|
105
131
|
def self.dropin!
|
106
132
|
Object.class_eval \
|
107
133
|
'def timeout t, klass=nil, &b
|
@@ -109,14 +135,16 @@ module FrugalTimeout
|
|
109
135
|
end'
|
110
136
|
end
|
111
137
|
|
112
|
-
def self.setupSleeper sleepFor
|
138
|
+
def self.setupSleeper sleepFor # :nodoc:
|
113
139
|
@sleeperMutex.synchronize {
|
114
140
|
sleep 0.1 until @sleeper.status == 'sleep'
|
115
141
|
@sleeperDelays.push sleepFor
|
116
142
|
@sleeper.wakeup
|
117
143
|
}
|
118
144
|
end
|
145
|
+
private :setupSleeper
|
119
146
|
|
147
|
+
# Same as Timeout.timeout()
|
120
148
|
def self.timeout t, klass=nil
|
121
149
|
@in.push request = Request.new(Thread.current, Time.now + t, klass)
|
122
150
|
begin
|
@@ -125,5 +153,5 @@ module FrugalTimeout
|
|
125
153
|
request.done! unless $!.is_a? FrugalTimeout::Error
|
126
154
|
end
|
127
155
|
end
|
128
|
-
#}}}
|
156
|
+
# }}}2
|
129
157
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: frugal_timeout
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-07-
|
12
|
+
date: 2013-07-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|