async-tools 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 41eb0e26bf599fdc141110a0507482e69f7fc94156671b748e8dd4ee894e166a
4
+ data.tar.gz: ff6b4f893d7c056d2382819d01c7391e6342ecec0bfb5df25fa62401d3954bbe
5
+ SHA512:
6
+ metadata.gz: 872c278f14fbe00c820432835333157130758422880476c1b549bb1ceab7c2256cd5c9bed064aee40a69e7250cf0df92738cb6781cb73d3e66d0f62383271f27
7
+ data.tar.gz: 921da88950692ff0f64d3da9d0ce709726815982d869c707b07781ef99f4e4aefb590f502864bf9b1916f8428d137737da024b663dd81332648af4d23558e665
@@ -0,0 +1,68 @@
1
+ name: Ruby
2
+
3
+ on: [push]
4
+
5
+ jobs:
6
+ lint:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+
11
+ - uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 3.2
14
+ bundler-cache: true
15
+
16
+ - name: Run the default task
17
+ run: |
18
+ gem install bundler -v 2.2.15
19
+ bundle install
20
+ bundle exec rubocop
21
+ test:
22
+ runs-on: ubuntu-latest
23
+ strategy:
24
+ matrix:
25
+ ruby: ["3.1", "3.2"]
26
+ steps:
27
+ - uses: actions/checkout@v2
28
+
29
+ - uses: ruby/setup-ruby@v1
30
+ with:
31
+ ruby-version: ${{ matrix.ruby }}
32
+ bundler-cache: true
33
+
34
+ - name: Install deps
35
+ run: |
36
+ gem install bundler -v 2.2.15
37
+ bundle install
38
+
39
+ - name: Run tests
40
+ run: bundle exec rspec
41
+ publish:
42
+ runs-on: ubuntu-latest
43
+ needs:
44
+ - lint
45
+ - test
46
+ if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')
47
+ steps:
48
+ - uses: actions/checkout@v2
49
+
50
+ - uses: ruby/setup-ruby@v1
51
+ with:
52
+ ruby-version: 3.2
53
+ bundler-cache: true
54
+
55
+ - name: Build gem
56
+ run: gem build
57
+
58
+ - name: Create credentials
59
+ run: |
60
+ mkdir ~/.gem
61
+ cat << EOF > ~/.gem/credentials
62
+ ---
63
+ :rubygems_api_key: ${{ secrets.RUBYGEMS_TOKEN }}
64
+ EOF
65
+ chmod 0600 /home/runner/.gem/credentials
66
+
67
+ - name: Push gem
68
+ run: gem push *gem
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.overcommit.yml ADDED
@@ -0,0 +1,4 @@
1
+ PreCommit:
2
+ RuboCop:
3
+ enabled: true
4
+ on_warn: fail # Treat all warnings as failures
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format progress
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,53 @@
1
+ require:
2
+ - rubocop-rake
3
+ - rubocop-rspec
4
+ - rubocop-performance
5
+
6
+ AllCops:
7
+ TargetRubyVersion: 3.1
8
+ NewCops: enable
9
+
10
+ Style/StringLiterals:
11
+ Enabled: true
12
+ EnforcedStyle: double_quotes
13
+
14
+ Style/StringLiteralsInInterpolation:
15
+ Enabled: true
16
+ EnforcedStyle: double_quotes
17
+
18
+ Layout/LineLength:
19
+ Max: 120
20
+
21
+ Style/Documentation:
22
+ Enabled: false
23
+
24
+ RSpec/NamedSubject:
25
+ Enabled: false
26
+
27
+ Metrics/BlockLength:
28
+ Exclude:
29
+ - spec/**/*_spec.rb
30
+
31
+ RSpec/MultipleExpectations:
32
+ Enabled: false
33
+
34
+ RSpec/ExampleLength:
35
+ Enabled: false
36
+
37
+ RSpec/NestedGroups:
38
+ Max: 5
39
+
40
+ Naming/MethodParameterName:
41
+ AllowedNames:
42
+ - e
43
+ - n
44
+ - id
45
+
46
+ Style/ClassAndModuleChildren:
47
+ EnforcedStyle: compact
48
+
49
+ Metrics/MethodLength:
50
+ Max: 15
51
+
52
+ Lint/UnusedBlockArgument:
53
+ Enabled: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2021-10-14
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at zhulik.gleb@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in async-worker-pool.gemspec
6
+ gemspec
7
+
8
+ gem "rake"
9
+
10
+ gem "async-http"
11
+ gem "async-rspec"
12
+ gem "rspec"
13
+
14
+ gem "overcommit"
15
+
16
+ gem "rubocop"
17
+ gem "rubocop-performance"
18
+ gem "rubocop-rake"
19
+ gem "rubocop-rspec"
20
+
21
+ gem "simplecov"
22
+
23
+ gem "solargraph"
data/Gemfile.lock ADDED
@@ -0,0 +1,160 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ async-tools (0.1.0)
5
+ async (~> 2.3)
6
+ zeitwerk (~> 2.6)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ ast (2.4.2)
12
+ async (2.3.1)
13
+ console (~> 1.10)
14
+ io-event (~> 1.1)
15
+ timers (~> 4.1)
16
+ async-http (0.59.4)
17
+ async (>= 1.25)
18
+ async-io (>= 1.28)
19
+ async-pool (>= 0.2)
20
+ protocol-http (~> 0.23.1)
21
+ protocol-http1 (~> 0.14.0)
22
+ protocol-http2 (~> 0.14.0)
23
+ traces (>= 0.8.0)
24
+ async-io (1.34.1)
25
+ async
26
+ async-pool (0.3.12)
27
+ async (>= 1.25)
28
+ async-rspec (1.16.1)
29
+ rspec (~> 3.0)
30
+ rspec-files (~> 1.0)
31
+ rspec-memory (~> 1.0)
32
+ backport (1.2.0)
33
+ benchmark (0.2.1)
34
+ childprocess (4.1.0)
35
+ console (1.16.2)
36
+ fiber-local
37
+ diff-lcs (1.5.0)
38
+ docile (1.4.0)
39
+ e2mmap (0.1.0)
40
+ fiber-local (1.0.0)
41
+ iniparse (1.5.0)
42
+ io-event (1.1.5)
43
+ jaro_winkler (1.5.4)
44
+ json (2.6.3)
45
+ kramdown (2.4.0)
46
+ rexml
47
+ kramdown-parser-gfm (1.1.0)
48
+ kramdown (~> 2.0)
49
+ mini_portile2 (2.8.1)
50
+ nokogiri (1.13.10)
51
+ mini_portile2 (~> 2.8.0)
52
+ racc (~> 1.4)
53
+ overcommit (0.59.1)
54
+ childprocess (>= 0.6.3, < 5)
55
+ iniparse (~> 1.4)
56
+ rexml (~> 3.2)
57
+ parallel (1.22.1)
58
+ parser (3.2.0.0)
59
+ ast (~> 2.4.1)
60
+ protocol-hpack (1.4.2)
61
+ protocol-http (0.23.12)
62
+ protocol-http1 (0.14.6)
63
+ protocol-http (~> 0.22)
64
+ protocol-http2 (0.14.2)
65
+ protocol-hpack (~> 1.4)
66
+ protocol-http (~> 0.18)
67
+ racc (1.6.2)
68
+ rainbow (3.1.1)
69
+ rake (13.0.6)
70
+ regexp_parser (2.6.1)
71
+ reverse_markdown (2.1.1)
72
+ nokogiri
73
+ rexml (3.2.5)
74
+ rspec (3.12.0)
75
+ rspec-core (~> 3.12.0)
76
+ rspec-expectations (~> 3.12.0)
77
+ rspec-mocks (~> 3.12.0)
78
+ rspec-core (3.12.0)
79
+ rspec-support (~> 3.12.0)
80
+ rspec-expectations (3.12.1)
81
+ diff-lcs (>= 1.2.0, < 2.0)
82
+ rspec-support (~> 3.12.0)
83
+ rspec-files (1.1.3)
84
+ rspec (~> 3.0)
85
+ rspec-memory (1.0.3)
86
+ rspec (~> 3.0)
87
+ rspec-mocks (3.12.1)
88
+ diff-lcs (>= 1.2.0, < 2.0)
89
+ rspec-support (~> 3.12.0)
90
+ rspec-support (3.12.0)
91
+ rubocop (1.42.0)
92
+ json (~> 2.3)
93
+ parallel (~> 1.10)
94
+ parser (>= 3.1.2.1)
95
+ rainbow (>= 2.2.2, < 4.0)
96
+ regexp_parser (>= 1.8, < 3.0)
97
+ rexml (>= 3.2.5, < 4.0)
98
+ rubocop-ast (>= 1.24.1, < 2.0)
99
+ ruby-progressbar (~> 1.7)
100
+ unicode-display_width (>= 1.4.0, < 3.0)
101
+ rubocop-ast (1.24.1)
102
+ parser (>= 3.1.1.0)
103
+ rubocop-performance (1.15.2)
104
+ rubocop (>= 1.7.0, < 2.0)
105
+ rubocop-ast (>= 0.4.0)
106
+ rubocop-rake (0.6.0)
107
+ rubocop (~> 1.0)
108
+ rubocop-rspec (2.16.0)
109
+ rubocop (~> 1.33)
110
+ ruby-progressbar (1.11.0)
111
+ simplecov (0.22.0)
112
+ docile (~> 1.1)
113
+ simplecov-html (~> 0.11)
114
+ simplecov_json_formatter (~> 0.1)
115
+ simplecov-html (0.12.3)
116
+ simplecov_json_formatter (0.1.4)
117
+ solargraph (0.48.0)
118
+ backport (~> 1.2)
119
+ benchmark
120
+ bundler (>= 1.17.2)
121
+ diff-lcs (~> 1.4)
122
+ e2mmap
123
+ jaro_winkler (~> 1.5)
124
+ kramdown (~> 2.3)
125
+ kramdown-parser-gfm (~> 1.1)
126
+ parser (~> 3.0)
127
+ reverse_markdown (>= 1.0.5, < 3)
128
+ rubocop (>= 0.52)
129
+ thor (~> 1.0)
130
+ tilt (~> 2.0)
131
+ yard (~> 0.9, >= 0.9.24)
132
+ thor (1.2.1)
133
+ tilt (2.0.11)
134
+ timers (4.3.5)
135
+ traces (0.8.0)
136
+ unicode-display_width (2.4.2)
137
+ webrick (1.7.0)
138
+ yard (0.9.28)
139
+ webrick (~> 1.7.0)
140
+ zeitwerk (2.6.6)
141
+
142
+ PLATFORMS
143
+ x86_64-linux
144
+
145
+ DEPENDENCIES
146
+ async-http
147
+ async-rspec
148
+ async-tools!
149
+ overcommit
150
+ rake
151
+ rspec
152
+ rubocop
153
+ rubocop-performance
154
+ rubocop-rake
155
+ rubocop-rspec
156
+ simplecov
157
+ solargraph
158
+
159
+ BUNDLED WITH
160
+ 2.4.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Gleb Sinyavskiy
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,66 @@
1
+ # Async::Tools
2
+
3
+ A set of useful [Async](github.com/socketry/async) tools.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'async-tools'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install async-tools
20
+
21
+ ## Usage
22
+
23
+ **More docs coming soon.**
24
+
25
+ ### Classes
26
+
27
+ #### Async::Q
28
+
29
+ A drop-in replacement for `Async::Queue` and `Async::LimitedQueue`. Can work as limited or unlimited queue depending on
30
+ initializer arguments. Supports mode switching and scaling in both directions.
31
+
32
+ #### Async::Channel
33
+
34
+ A thin wrapper around Async::Q that acts like a Go channel. Can be user for delivering messages and exceptions(using
35
+ `#error` method). Exceptions are being reraised in `#dequeue`, `#each` and `#async`. Can be closed. After being closed
36
+ automatically stops accepting new messages and schedules graceful stop of all consumers. Awaiing `#each` or `#async` will
37
+ return, `#dequeue` will raise `ChannelClosedError`. In compare to `Async::Queue`, `Async::LimitedQueue` and `Async:Q`,
38
+ `Async::Channel` can send `nil`'s.
39
+
40
+ #### Async::WorkerPool
41
+
42
+ A thin wrapper around `Async::Channel` and `Async::Semaphore`. WorkerPool can be used to perform concurrent actions with
43
+ limited strictly limited concurrency.
44
+
45
+ ## Development
46
+
47
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also
48
+ run `bin/console` for an interactive prompt that will allow you to experiment.
49
+
50
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version
51
+ number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git
52
+ commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
53
+
54
+ ## Contributing
55
+
56
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/async-tools. This project is intended
57
+ to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/async-tools/blob/main/CODE_OF_CONDUCT.md).
58
+
59
+ ## License
60
+
61
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
62
+
63
+ ## Code of Conduct
64
+
65
+ Everyone interacting in the Async::Tools project's codebases, issue trackers, chat rooms and mailing lists is expected to
66
+ follow the [code of conduct](https://github.com/[USERNAME]/async-tools/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/async/tools/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "async-tools"
7
+ spec.version = Async::Tools::VERSION
8
+ spec.authors = ["Gleb Sinyavskiy"]
9
+ spec.email = ["zhulik.gleb@gmail.com"]
10
+
11
+ spec.summary = "A set of useful tools for async programming with Async."
12
+ spec.description = "A set of useful tools for async programming with Async."
13
+ spec.homepage = "https://github.com/zhulik/async-tools"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 3.1.0")
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/zhulik/async-tools"
19
+ # spec.metadata["changelog_uri"] = "https://github.com/zhulik/async-tools/blob/main/CHANGELOG.md"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_dependency "async", "~> 2.3"
31
+ spec.add_dependency "zeitwerk", "~> 2.6"
32
+ spec.metadata["rubygems_mfa_required"] = "true"
33
+ end
data/bin/console ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "async/tools"
6
+
7
+ require "irb"
8
+ require "irb/completion"
9
+
10
+ Async do
11
+ IRB.start(__FILE__)
12
+ end
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,92 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Async::Bus::Bus
4
+ attr_reader :name
5
+
6
+ include Console
7
+
8
+ def initialize(name: :default, limit: 10)
9
+ @name = name
10
+ @limit = limit
11
+ @closed = false
12
+
13
+ @subscribers = Hash.new { |hash, key| hash[key] = [] }
14
+ end
15
+
16
+ # Blocks if any of output channels is full!
17
+ def publish(nameable_or_event, **payload)
18
+ check_if_open!
19
+ name, payload = normalize(nameable_or_event, payload)
20
+
21
+ subs = @subscribers[name]
22
+ return if subs.empty?
23
+
24
+ subs.each do |chan|
25
+ publishing_blocked if chan.full?
26
+ chan << [name, payload]
27
+ end
28
+ Async::Task.current.yield
29
+ end
30
+
31
+ # Blocks!
32
+ def subscribe(nameable, callable = nil, &block)
33
+ check_if_open!
34
+ callable ||= block
35
+ unless callable.respond_to?(:call)
36
+ raise ArgumentError, "callable or block must be provided. callable must respond to :call"
37
+ end
38
+
39
+ event_name = normalize(nameable).first
40
+
41
+ chan = Async::Channel.new(@limit)
42
+ @subscribers[event_name] << chan
43
+ serve(chan, event_name, callable)
44
+ end
45
+
46
+ def close
47
+ return if @closed
48
+
49
+ @closed = true
50
+
51
+ @subscribers.values.flatten.each(&:close)
52
+ @subscribers.clear
53
+ end
54
+
55
+ def on_event(&block) = @on_event_callback = block
56
+
57
+ private
58
+
59
+ def normalize(nameable, payload = nil)
60
+ return [nameable, payload] if nameable.is_a?(Symbol)
61
+ return [nameable.to_sym, payload] if nameable.is_a?(String)
62
+ return [nameable.event_name.to_sym, nameable] if nameable.respond_to?(:event_name)
63
+
64
+ n = nameable[:event_name] || nameable["event_name"]
65
+ return [n.to_sym, nameable] if n
66
+
67
+ raise ArgumentError, "cannot infer event name from #{nameable.inspect}"
68
+ end
69
+
70
+ def serve(chan, event_name, callable)
71
+ stopped = false
72
+ unsub = lambda {
73
+ chan.close
74
+ stopped = true
75
+ @subscribers[event_name].delete(chan)
76
+ }
77
+
78
+ chan.each do |name, payload|
79
+ @on_event_callback&.call(wrapper)
80
+ callable.call(payload, unsub:, meta: { bus: self })
81
+ break if stopped
82
+ end
83
+ end
84
+
85
+ def publishing_blocked
86
+ logger.warn(self) { "One of the subscribers is slow, blocking publishing. Event name: #{name}" }
87
+ end
88
+
89
+ def check_if_open!
90
+ raise "Bus is closed" if @closed
91
+ end
92
+ end
data/lib/async/bus.rb ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Threading is not supported!
4
+ module Async::Bus
5
+ class << self
6
+ def get(name = :default)
7
+ @buses ||= {}
8
+ @buses[name] ||= Bus.new(name:)
9
+ @buses[name]
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,97 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Async::Channel
4
+ extend Forwardable
5
+
6
+ attr_reader :subscribers
7
+
8
+ class Error < StandardError; end
9
+
10
+ class ChannelClosedError < Error; end
11
+
12
+ def initialize(limit = 1, **options)
13
+ @queue = Async::Q.new(limit, **options)
14
+ @subscribers = 0
15
+
16
+ @parent = options[:parent]
17
+ @closed = false
18
+ end
19
+
20
+ def_delegators :@queue, :count, :empty?, :length, :size, :full?
21
+ def_delegator :self, :enqueue, :<<
22
+
23
+ def enqueue(message)
24
+ check_channel_writeable!
25
+
26
+ @queue << [:message, message]
27
+ end
28
+
29
+ def enqueue_all(messages)
30
+ check_channel_writeable!
31
+
32
+ @queue.enqueue_all(messages.map { |message| [:message, message] })
33
+ end
34
+
35
+ def error(e)
36
+ check_channel_writeable!
37
+
38
+ @queue << [:error, e]
39
+ end
40
+
41
+ def close
42
+ @closed = true
43
+
44
+ @queue.expand(@subscribers)
45
+
46
+ @subscribers.times do
47
+ @queue << [:close]
48
+ end
49
+ end
50
+
51
+ def closed?
52
+ @closed
53
+ end
54
+
55
+ def open?
56
+ !closed?
57
+ end
58
+
59
+ def dequeue
60
+ check_channel_readable!
61
+
62
+ type, message = @queue.dequeue
63
+ raise ChannelClosedError, "Channel was closed" if type == :close
64
+ raise message if type == :error # TODO: fix backtrace
65
+
66
+ message
67
+ end
68
+
69
+ def each
70
+ check_channel_readable!
71
+
72
+ @subscribers += 1
73
+ while message = dequeue # rubocop:disable Lint/AssignmentInCondition
74
+ yield message
75
+ end
76
+ rescue ChannelClosedError
77
+ nil
78
+ ensure
79
+ @subscribers -= 1
80
+ end
81
+
82
+ def async(parent: (@parent || Task.current), &block)
83
+ each do |item|
84
+ parent.async(item, &block)
85
+ end
86
+ end
87
+
88
+ private
89
+
90
+ def check_channel_writeable!
91
+ raise ChannelClosedError, "Can't send to a closed channel" if closed?
92
+ end
93
+
94
+ def check_channel_readable!
95
+ raise ChannelClosedError, "Cannot receive from a closed channel" if closed? && empty?
96
+ end
97
+ end
data/lib/async/q.rb ADDED
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "forwardable"
4
+
5
+ require "async/notification"
6
+
7
+ class Async::Q
8
+ extend Forwardable
9
+
10
+ attr_reader :items, :limit
11
+
12
+ def initialize(limit = Float::INFINITY, items: [], parent: nil)
13
+ @limit = limit || Float::INFINITY
14
+ @parent = parent
15
+
16
+ raise ArgumentError, "Items size is greter than limit: #{items.count} > #{@limit}" if items.count > @limit
17
+
18
+ @items = items
19
+ @any_notification = Async::Notification.new
20
+ @free_notification = Async::Notification.new
21
+ end
22
+
23
+ def_delegators :@items, :count, :empty?, :length, :size
24
+
25
+ def_delegator :self, :enqueue, :<<
26
+ def_delegator :self, :full?, :limited?
27
+ def_delegator :self, :resize, :scale
28
+
29
+ def full?
30
+ size >= @limit
31
+ end
32
+
33
+ def resize(new_limit)
34
+ if new_limit > @limit
35
+ @limit = new_limit
36
+ @free_notification.signal
37
+ elsif new_limit <= 0
38
+ raise ArgumentError, "Limit cannot be <= 0: #{new_limit}"
39
+ elsif size > new_limit
40
+ raise ArgumentError, "New limit cannot be lower than the current size: #{size} > #{new_limit}"
41
+ else
42
+ @limit = new_limit
43
+ end
44
+ end
45
+
46
+ def expand(n)
47
+ resize(limit + n)
48
+ end
49
+
50
+ def shrink(n)
51
+ resize(limit - n)
52
+ end
53
+
54
+ def enqueue(item)
55
+ @free_notification.wait while full?
56
+
57
+ @items.push(item)
58
+ @any_notification.signal
59
+ end
60
+
61
+ def enqueue_all(items)
62
+ items.each { |item| enqueue(item) }
63
+ end
64
+
65
+ def dequeue
66
+ @any_notification.wait while empty?
67
+
68
+ item = @items.shift
69
+
70
+ @free_notification.signal
71
+
72
+ item
73
+ end
74
+
75
+ def each
76
+ while item = dequeue # rubocop:disable Lint/AssignmentInCondition
77
+ yield(item)
78
+ end
79
+ end
80
+
81
+ def async(parent: (@parent || Task.current), &block)
82
+ each do |item|
83
+ parent.async(item, &block)
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Async::ResultNotification
4
+ extend Forwardable
5
+
6
+ def initialize
7
+ @channel = Async::Channel.new
8
+ end
9
+
10
+ def signal(item = nil)
11
+ @channel << (block_given? ? yield : item)
12
+ rescue Async::Stop, StandardError => e
13
+ @channel.error(e)
14
+ end
15
+
16
+ def_delegator :@channel, :dequeue, :wait
17
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Async::Timer
4
+ extend Forwardable
5
+
6
+ attr_reader :dealay, :repeat
7
+
8
+ class Error < StandardError; end
9
+
10
+ class AlreadyStarted < Error; end
11
+
12
+ def initialize(delay, repeat: true, parent: Async::Task.current, &block)
13
+ @delay = delay
14
+ @repeat = repeat
15
+ @parent = parent
16
+ @block = block
17
+
18
+ start
19
+ end
20
+
21
+ def_delegator :@task, :stop
22
+ def_delegator :@block, :call, :execute
23
+
24
+ def active?
25
+ @active
26
+ end
27
+
28
+ def restart
29
+ stop
30
+ @task.wait
31
+ start
32
+ end
33
+
34
+ def schedule
35
+ @parent.async do
36
+ execute
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def start
43
+ raise AlreadyStarted, "Timer already started" if active?
44
+
45
+ @active = true
46
+
47
+ @task = @parent.async do
48
+ loop do
49
+ @parent.sleep(@delay)
50
+ schedule
51
+ break unless @repeat
52
+ rescue Async::Stop, Async::TimeoutError
53
+ break
54
+ ensure
55
+ @active = false
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Async # rubocop:disable Style/ClassAndModuleChildren
4
+ module Tools
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "tools/version"
4
+
5
+ require "async"
6
+ require "async/notification"
7
+ require "async/semaphore"
8
+
9
+ require "zeitwerk"
10
+
11
+ loader = Zeitwerk::Loader.new
12
+ loader.tag = File.basename(__FILE__, ".rb")
13
+ loader.inflector = Zeitwerk::GemInflector.new(__FILE__)
14
+ loader.push_dir(File.expand_path("..", __dir__.to_s))
15
+ loader.setup
16
+
17
+ module Async
18
+ # Your code goes here...
19
+ module Tools # rubocop:disable Style/ClassAndModuleChildren
20
+ class Error < StandardError
21
+ end
22
+ end
23
+
24
+ def self.map(collection, **params, &)
25
+ WorkerPool.with(queue_limit: collection.count, **params) do |pool|
26
+ pool.schedule_all(collection, &).map(&:wait)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Async::WorkerPool
4
+ extend Forwardable
5
+
6
+ class Error < StandardError; end
7
+
8
+ class StoppedError < Error; end
9
+
10
+ class << self
11
+ def start(...)
12
+ new(...)
13
+ end
14
+
15
+ def with(*args, **params, &)
16
+ new(*args, **params).with(&)
17
+ end
18
+ end
19
+
20
+ def initialize(workers: 1, queue_limit: 1, parent: Async::Task.current, &block)
21
+ @queue_limit = queue_limit
22
+ @parent = parent
23
+ @block = block
24
+
25
+ @semaphore = Async::Semaphore.new(workers, parent: @parent)
26
+ @channel = Async::Channel.new(@queue_limit, parent: @semaphore)
27
+ @task = start
28
+ end
29
+
30
+ def_delegator :@semaphore, :limit, :workers
31
+ def_delegator :@semaphore, :count, :busy
32
+
33
+ def_delegator :@task, :wait
34
+
35
+ def_delegator :@channel, :close, :stop
36
+ def_delegator :@channel, :open?, :running?
37
+ def_delegator :@channel, :closed?, :stopped?
38
+
39
+ def waiting
40
+ @semaphore.waiting.size
41
+ end
42
+
43
+ def call(*args, **params, &block)
44
+ block ||= @block
45
+ raise ArgumentError, "Block must be passed to #schedule if it's not passed to #initlaize" if block.nil?
46
+
47
+ raise StoppedError, "The pool was stopped" unless running?
48
+
49
+ Async::ResultNotification.new.tap do |notification|
50
+ @channel.enqueue([notification, [args, params], block])
51
+ end
52
+ end
53
+
54
+ def schedule_all(tasks, &block)
55
+ block ||= @block
56
+
57
+ raise ArgumentError, "Block must be passed to #schedule_all if it's not passed to #initlaize" if block.nil?
58
+
59
+ raise StoppedError, "The pool was stopped" unless running?
60
+
61
+ tasks = tasks.map { |task| [Async::ResultNotification.new, [[task], {}], block] }
62
+
63
+ @channel.enqueue_all(tasks)
64
+ tasks.map(&:first)
65
+ end
66
+
67
+ def with
68
+ yield self
69
+ ensure
70
+ stop
71
+ wait
72
+ end
73
+
74
+ private
75
+
76
+ def start
77
+ @parent.async do
78
+ @channel.async do |_, (notification, (args, params), block)|
79
+ notification.signal do
80
+ block.call(*args, **params)
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: async-tools
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Gleb Sinyavskiy
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-01-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: async
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: zeitwerk
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.6'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.6'
41
+ description: A set of useful tools for async programming with Async.
42
+ email:
43
+ - zhulik.gleb@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".github/workflows/main.yml"
49
+ - ".gitignore"
50
+ - ".overcommit.yml"
51
+ - ".rspec"
52
+ - ".rubocop.yml"
53
+ - CHANGELOG.md
54
+ - CODE_OF_CONDUCT.md
55
+ - Gemfile
56
+ - Gemfile.lock
57
+ - LICENSE.txt
58
+ - README.md
59
+ - Rakefile
60
+ - async-tools.gemspec
61
+ - bin/console
62
+ - bin/setup
63
+ - lib/async/bus.rb
64
+ - lib/async/bus/bus.rb
65
+ - lib/async/channel.rb
66
+ - lib/async/q.rb
67
+ - lib/async/result_notification.rb
68
+ - lib/async/timer.rb
69
+ - lib/async/tools.rb
70
+ - lib/async/tools/version.rb
71
+ - lib/async/worker_pool.rb
72
+ homepage: https://github.com/zhulik/async-tools
73
+ licenses:
74
+ - MIT
75
+ metadata:
76
+ homepage_uri: https://github.com/zhulik/async-tools
77
+ source_code_uri: https://github.com/zhulik/async-tools
78
+ rubygems_mfa_required: 'true'
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: 3.1.0
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubygems_version: 3.3.26
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: A set of useful tools for async programming with Async.
98
+ test_files: []