closeable_queue 0.1.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 +7 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +49 -0
- data/Rakefile +10 -0
- data/bin/console +12 -0
- data/bin/setup +7 -0
- data/closeable_queue.gemspec +26 -0
- data/lib/closeable_queue/version.rb +3 -0
- data/lib/closeable_queue.rb +129 -0
- metadata +111 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 61fbb50b1a346c221c930aeaa5a1edf0f952dbfd
|
4
|
+
data.tar.gz: 78e4c2130f9b22e205dbf8b514ffa68ae4b2a86b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 86621e560893b5ef314dd2a07083647ab9bb1cb1cc7829a3289b27d0fb7ccf0b657004b003d2ce601e5587be8de5605a404943a68ce58e4ae8822962b7491769
|
7
|
+
data.tar.gz: 3af8cb15ee0ec3dc2448c56c06b73cc12fa51d609ddc1b31155eebfc8d7a03c2e39cc8926dff3fb8ca29a2991e689b37649f8477cb81c53decf4d9fc9bcd09e3
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Thomas Hurst
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# CloseableQueue
|
2
|
+
|
3
|
+
Wrapper around Queue and SizedQueue adding a `#close` method.
|
4
|
+
|
5
|
+
Closed queues allow `#pop` until the queue is drained, then forever return nil
|
6
|
+
or raise StopIteration. Pushes to a closed queue also raise an exception.
|
7
|
+
|
8
|
+
Ruby 2.3 is expected to support a native `#close` method which works similarly:
|
9
|
+
|
10
|
+
https://bugs.ruby-lang.org/issues/10600
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
Add this line to your application's Gemfile:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
gem 'closeable_queue'
|
18
|
+
```
|
19
|
+
|
20
|
+
And then execute:
|
21
|
+
|
22
|
+
$ bundle
|
23
|
+
|
24
|
+
Or install it yourself as:
|
25
|
+
|
26
|
+
$ gem install closeable_queue
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
queue = CloseableQueue.new
|
32
|
+
queue.push(an_object) # => queue
|
33
|
+
queue.pop # => an_object
|
34
|
+
queue.close
|
35
|
+
queue.pop # => nil
|
36
|
+
queue.close(true)
|
37
|
+
queue.pop # => raises CloseableQueue::ClosedQueue (is_a? StopIteration)
|
38
|
+
```
|
39
|
+
|
40
|
+
## Development
|
41
|
+
|
42
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
43
|
+
|
44
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
45
|
+
|
46
|
+
## Contributing
|
47
|
+
|
48
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Freaky/closeable_queue
|
49
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
data/bin/setup
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'closeable_queue/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "closeable_queue"
|
8
|
+
spec.version = CloseableQueue::VERSION
|
9
|
+
spec.authors = ["Thomas Hurst"]
|
10
|
+
spec.email = ["tom@hur.st"]
|
11
|
+
spec.license = "MIT"
|
12
|
+
|
13
|
+
spec.summary = %q{Thread-safe queues with safer shutdown semantics}
|
14
|
+
spec.description = %q{Adds a #close method to the standard library Queue and SizedQueue to close down consumers}
|
15
|
+
spec.homepage = "https://github.com/Freaky/closeable_queue"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "minitest"
|
25
|
+
spec.add_dependency "concurrent-ruby"
|
26
|
+
end
|
@@ -0,0 +1,129 @@
|
|
1
|
+
|
2
|
+
require 'thread'
|
3
|
+
require 'concurrent'
|
4
|
+
require 'closeable_queue/version'
|
5
|
+
|
6
|
+
# A wrapper around +Queue+ to provide support for `#close`.
|
7
|
+
#
|
8
|
+
# Once closed, threads waiting on dequeue will drain the queue and then receive
|
9
|
+
# nil on future dequeues. If close(true) is used, pop from an empty closed queue
|
10
|
+
# or attempts to push raise +ClosedQueue+, a subclass of +StopIteration+.
|
11
|
+
#
|
12
|
+
# Example usage:
|
13
|
+
#
|
14
|
+
# queue = ClosableQueue.new
|
15
|
+
# consumer = Thread.new { while number = queue.pop ; puts number ; end }
|
16
|
+
# 5.times {|x| queue.push(x) }
|
17
|
+
# queue.close
|
18
|
+
# consumer.join
|
19
|
+
#
|
20
|
+
# `#close` is thread-safe and can be called safely multiple times.
|
21
|
+
#
|
22
|
+
# This is anticipated to be obsolete by Ruby 2.3 with Queue#close.
|
23
|
+
#
|
24
|
+
class CloseableQueue
|
25
|
+
class ClosedQueueError < ThreadError
|
26
|
+
end
|
27
|
+
|
28
|
+
class ClosedQueue < StopIteration
|
29
|
+
end
|
30
|
+
|
31
|
+
# Set up a new queue. +limit+ will use a SizedQueue, default unbounded Queue.
|
32
|
+
def initialize(limit = nil)
|
33
|
+
@mutex = Mutex.new
|
34
|
+
@waiting = Set.new
|
35
|
+
@num_waiting = Concurrent::AtomicFixnum.new
|
36
|
+
@closed = Concurrent::AtomicBoolean.new
|
37
|
+
@raise_exception = Concurrent::AtomicBoolean.new(false)
|
38
|
+
|
39
|
+
if limit
|
40
|
+
@queue = SizedQueue.new(Integer(limit))
|
41
|
+
else
|
42
|
+
@queue = Queue.new
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def inspect
|
47
|
+
"<#{name} size=#{length} closed=#{closed?} waiting=#{num_waiting}>"
|
48
|
+
end
|
49
|
+
|
50
|
+
# Take the first element off the queue.
|
51
|
+
#
|
52
|
+
# If the queue is empty and closed?, return nil, or optionally raise
|
53
|
+
# ClosedQueue (a subclass of StopIteration)
|
54
|
+
def pop
|
55
|
+
@queue.pop(true)
|
56
|
+
rescue ThreadError
|
57
|
+
if closed?
|
58
|
+
raise ClosedQueue if @raise_exception.true?
|
59
|
+
return nil
|
60
|
+
else
|
61
|
+
sleep
|
62
|
+
retry
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
[:deq, :shift].each { |name| alias_method name, :pop }
|
67
|
+
|
68
|
+
# Add an item to the queue and wakeup any sleeping consumers.
|
69
|
+
#
|
70
|
+
# If the queue is closed, raises +ClosedQueueError+.
|
71
|
+
def push(item)
|
72
|
+
fail ClosedQueueError if closed?
|
73
|
+
|
74
|
+
@queue.push(item)
|
75
|
+
wakeup
|
76
|
+
self
|
77
|
+
end
|
78
|
+
|
79
|
+
[:enq, :<<].each { |name| alias_method name, :push }
|
80
|
+
|
81
|
+
# Get an atomic snapshot if the number of threads waiting on the queue.
|
82
|
+
def num_waiting
|
83
|
+
@num_waiting.value
|
84
|
+
end
|
85
|
+
|
86
|
+
# Get the number of items remaining on the queue
|
87
|
+
def length
|
88
|
+
@queue.length
|
89
|
+
end
|
90
|
+
|
91
|
+
# Return true if the queue is empty
|
92
|
+
def empty?
|
93
|
+
@queue.empty?
|
94
|
+
end
|
95
|
+
|
96
|
+
# Return true if the queue has been closed
|
97
|
+
def closed?
|
98
|
+
@closed.true?
|
99
|
+
end
|
100
|
+
|
101
|
+
# Close the queue if it hasn't been already. Wake up waiting threads if any.
|
102
|
+
def close(raise_exception = false)
|
103
|
+
@raise_exception.make_true if raise_exception
|
104
|
+
@mutex.synchronize { @waiting.each(&:wakeup) } if @closed.make_true
|
105
|
+
self
|
106
|
+
end
|
107
|
+
|
108
|
+
private
|
109
|
+
|
110
|
+
def sleep
|
111
|
+
@mutex.synchronize do
|
112
|
+
@waiting << Thread.current
|
113
|
+
@num_waiting.increment
|
114
|
+
@mutex.sleep
|
115
|
+
@waiting.delete(Thread.current)
|
116
|
+
@num_waiting.decrement
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
def wakeup
|
121
|
+
return if @num_waiting.value.zero?
|
122
|
+
|
123
|
+
@mutex.synchronize do
|
124
|
+
@waiting.first.wakeup if @waiting.any?
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
ClosableQueue = CloseableQueue
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: closeable_queue
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Thomas Hurst
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-09-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: concurrent-ruby
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: 'Adds a #close method to the standard library Queue and SizedQueue to
|
70
|
+
close down consumers'
|
71
|
+
email:
|
72
|
+
- tom@hur.st
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- bin/console
|
82
|
+
- bin/setup
|
83
|
+
- closeable_queue.gemspec
|
84
|
+
- lib/closeable_queue.rb
|
85
|
+
- lib/closeable_queue/version.rb
|
86
|
+
homepage: https://github.com/Freaky/closeable_queue
|
87
|
+
licenses:
|
88
|
+
- MIT
|
89
|
+
metadata: {}
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubyforge_project:
|
106
|
+
rubygems_version: 2.4.5
|
107
|
+
signing_key:
|
108
|
+
specification_version: 4
|
109
|
+
summary: Thread-safe queues with safer shutdown semantics
|
110
|
+
test_files: []
|
111
|
+
has_rdoc:
|