timed_semaphore 0.0.1 → 0.0.2
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/timed_semaphore.rb +36 -3
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bab3b772c57f7baf507fd6ed56a86fd52bfa2115
|
4
|
+
data.tar.gz: 5a75209d8eaa5653c94dc0d1bba19c87cc9db74c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 42722a9b4048dd59583f8574569dee3870dbf0016b1f8370f406aff14ed1dc3bdf203976135c425f8c2f6f5e6fdcd4d8217afc40094845a846d7e219f511c44a
|
7
|
+
data.tar.gz: 82f7a221df7da977c0aa80448d278cbcf3b2c76d2379af0054ec2e64ef2f7a29421cf7d1ad2d986029befc006fa942cfc61455b1d8d0816d638f19e46aa3e3cd
|
data/lib/timed_semaphore.rb
CHANGED
@@ -1,4 +1,30 @@
|
|
1
|
+
# TimedSemaphore is a specialized implementation of a Semaphore
|
2
|
+
# that gives a number of permits in a given time frame. A use case
|
3
|
+
# for it is to limit the load on a resource.
|
4
|
+
# The idea is taken from the Apache Commons Lang package.
|
5
|
+
# @see https://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/concurrent/TimedSemaphore.html
|
6
|
+
# Apache Commons Lang
|
7
|
+
#
|
8
|
+
# @author ssuljic
|
9
|
+
#
|
10
|
+
# @example
|
11
|
+
# threads = []
|
12
|
+
# semaphore = TimedSemaphore.new(2, 3)
|
13
|
+
#
|
14
|
+
# 10.times do
|
15
|
+
# threads << Thread.new do
|
16
|
+
# semaphore.acquire
|
17
|
+
# puts Time.now
|
18
|
+
# end
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# threads.map(&:join)
|
1
22
|
class TimedSemaphore
|
23
|
+
# @param num_of_ops [Fixnum] Number of operations
|
24
|
+
# which should be allowed in a specified time frame.
|
25
|
+
# @param num_of_seconds [Fixnum] Period in seconds after all
|
26
|
+
# permits are released.
|
27
|
+
# @return [TimedSemaphore] a new instance of TimedSemaphore.
|
2
28
|
def initialize(num_of_ops, num_of_seconds)
|
3
29
|
@count = 0
|
4
30
|
@limit = num_of_ops
|
@@ -8,7 +34,10 @@ class TimedSemaphore
|
|
8
34
|
@timer = nil
|
9
35
|
end
|
10
36
|
|
11
|
-
#
|
37
|
+
# Tries to acquire a permit from the semaphore. This method
|
38
|
+
# will block if the limit for the current period has already
|
39
|
+
# been reached. The first call starts a timer thread for releasing
|
40
|
+
# all permits, which makes the semaphore active
|
12
41
|
def acquire
|
13
42
|
synchronize do
|
14
43
|
@condition.wait while @limit > 0 && @count == @limit
|
@@ -19,7 +48,8 @@ class TimedSemaphore
|
|
19
48
|
|
20
49
|
private
|
21
50
|
|
22
|
-
# Starts a thread which releases
|
51
|
+
# Starts a timer thread which releases
|
52
|
+
# all permits after @period seconds.
|
23
53
|
def start_timer
|
24
54
|
synchronize do
|
25
55
|
@timer = Thread.new do
|
@@ -29,7 +59,8 @@ class TimedSemaphore
|
|
29
59
|
end
|
30
60
|
end
|
31
61
|
|
32
|
-
# Releases all permits and notifies all
|
62
|
+
# Releases all permits and notifies all
|
63
|
+
# waiting threads to try acquire again.
|
33
64
|
def release_permits
|
34
65
|
synchronize do
|
35
66
|
@timer = nil
|
@@ -38,6 +69,8 @@ class TimedSemaphore
|
|
38
69
|
end
|
39
70
|
end
|
40
71
|
|
72
|
+
# Method used for synchronizing on @lock.
|
73
|
+
# Syntactic sugar
|
41
74
|
def synchronize(&block)
|
42
75
|
fail 'No block given' unless block_given?
|
43
76
|
@lock.synchronize(&block)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: timed_semaphore
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sadžid Suljić
|
@@ -45,3 +45,4 @@ specification_version: 4
|
|
45
45
|
summary: Implementation of TimedSemaphore
|
46
46
|
test_files:
|
47
47
|
- test/timed_semaphore_test.rb
|
48
|
+
has_rdoc:
|