fiber_hook 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/.github/workflows/main.yml +16 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.rubocop.yml +17 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +11 -0
- data/Gemfile.lock +62 -0
- data/LICENSE.txt +21 -0
- data/README.md +72 -0
- data/Rakefile +10 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/fiber_hook.gemspec +33 -0
- data/lib/fiber_hook/version.rb +5 -0
- data/lib/fiber_hook.rb +79 -0
- metadata +62 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 3d3fc52fc0617cb7927219a9deffca2cd91ccfde06d37a33d491aa662fdc23bc
|
4
|
+
data.tar.gz: d7ddde91085bb24617ef642774cc26f2206f383a7c160159c0b7f15c716b5cc5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 69791eff1f24a54ae3358bb0e495944ec9e64763b973c3401450f6a96e630a4fdcaecadcc648a9adf5426bc4cc3d668365123aab2ec860dd2a39cba6d1e1550e
|
7
|
+
data.tar.gz: a200eaaa21532773171f9ac668564ba83146e9bfc8dc50f61b86f2d9d593eff70c178b8bad9033d187297a19636d5e702a1e76200f15f39e8e3f7bde973d9619
|
@@ -0,0 +1,16 @@
|
|
1
|
+
name: Ruby
|
2
|
+
|
3
|
+
on: [push,pull_request]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
build:
|
7
|
+
runs-on: ubuntu-latest
|
8
|
+
steps:
|
9
|
+
- uses: actions/checkout@v2
|
10
|
+
- name: Set up Ruby
|
11
|
+
uses: ruby/setup-ruby@v1
|
12
|
+
with:
|
13
|
+
ruby-version: 3.0.2
|
14
|
+
bundler-cache: true
|
15
|
+
- name: Run the default task
|
16
|
+
run: bundle exec rake
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.5
|
3
|
+
NewCops: enable
|
4
|
+
|
5
|
+
Style/StringLiterals:
|
6
|
+
Enabled: true
|
7
|
+
EnforcedStyle: double_quotes
|
8
|
+
|
9
|
+
Style/StringLiteralsInInterpolation:
|
10
|
+
Enabled: true
|
11
|
+
EnforcedStyle: double_quotes
|
12
|
+
|
13
|
+
Layout/LineLength:
|
14
|
+
Max: 100
|
15
|
+
|
16
|
+
Metrics/BlockLength:
|
17
|
+
Enabled: false
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
fiber_hook (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ast (2.4.2)
|
10
|
+
diff-lcs (1.4.4)
|
11
|
+
parallel (1.21.0)
|
12
|
+
parser (3.0.2.0)
|
13
|
+
ast (~> 2.4.1)
|
14
|
+
rainbow (3.0.0)
|
15
|
+
rake (13.0.6)
|
16
|
+
regexp_parser (2.1.1)
|
17
|
+
rexml (3.2.5)
|
18
|
+
rspec (3.10.0)
|
19
|
+
rspec-core (~> 3.10.0)
|
20
|
+
rspec-expectations (~> 3.10.0)
|
21
|
+
rspec-mocks (~> 3.10.0)
|
22
|
+
rspec-core (3.10.1)
|
23
|
+
rspec-support (~> 3.10.0)
|
24
|
+
rspec-expectations (3.10.1)
|
25
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
26
|
+
rspec-support (~> 3.10.0)
|
27
|
+
rspec-mocks (3.10.2)
|
28
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
29
|
+
rspec-support (~> 3.10.0)
|
30
|
+
rspec-support (3.10.3)
|
31
|
+
rubocop (1.23.0)
|
32
|
+
parallel (~> 1.10)
|
33
|
+
parser (>= 3.0.0.0)
|
34
|
+
rainbow (>= 2.2.2, < 4.0)
|
35
|
+
regexp_parser (>= 1.8, < 3.0)
|
36
|
+
rexml
|
37
|
+
rubocop-ast (>= 1.12.0, < 2.0)
|
38
|
+
ruby-progressbar (~> 1.7)
|
39
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
40
|
+
rubocop-ast (1.13.0)
|
41
|
+
parser (>= 3.0.1.1)
|
42
|
+
rubocop-rake (0.6.0)
|
43
|
+
rubocop (~> 1.0)
|
44
|
+
rubocop-rspec (2.6.0)
|
45
|
+
rubocop (~> 1.19)
|
46
|
+
ruby-progressbar (1.11.0)
|
47
|
+
unicode-display_width (2.1.0)
|
48
|
+
|
49
|
+
PLATFORMS
|
50
|
+
arm64-darwin-21
|
51
|
+
x86_64-linux
|
52
|
+
|
53
|
+
DEPENDENCIES
|
54
|
+
fiber_hook!
|
55
|
+
rake (~> 13.0)
|
56
|
+
rspec (~> 3.0)
|
57
|
+
rubocop (~> 1.23)
|
58
|
+
rubocop-rake
|
59
|
+
rubocop-rspec
|
60
|
+
|
61
|
+
BUNDLED WITH
|
62
|
+
2.2.31
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2021 Brian Morearty
|
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,72 @@
|
|
1
|
+
# FiberHook
|
2
|
+
|
3
|
+
With this gem you can hook into fiber creation to pass a
|
4
|
+
value from the parent fiber to the child fiber.
|
5
|
+
|
6
|
+
This is useful when you’ve got thread-local storage (which
|
7
|
+
is actually fiber-local) that you want also accessible from within
|
8
|
+
child fibers. It works without requiring that fibers go through
|
9
|
+
your own fiber-creation method, so that even fibers created inside
|
10
|
+
gems you don't own will work as expected.
|
11
|
+
|
12
|
+
A potential use case is using the [Async](https://github.com/socketry/async)
|
13
|
+
gem with the [Falcon](https://github.com/socketry/falcon) server
|
14
|
+
and you want to use [RequestStore](https://github.com/steveklabnik/request_store)
|
15
|
+
for per-request storage. For this particular use case,
|
16
|
+
take a look at the
|
17
|
+
[request_store-fibers](https://github.com/BMorearty/request_store-fibers) gem.
|
18
|
+
|
19
|
+
## Installation
|
20
|
+
|
21
|
+
Add this line to your application's Gemfile:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
gem 'fiber_hook'
|
25
|
+
```
|
26
|
+
|
27
|
+
And then execute:
|
28
|
+
|
29
|
+
$ bundle install
|
30
|
+
|
31
|
+
Or install it yourself as:
|
32
|
+
|
33
|
+
$ gem install fiber_hook
|
34
|
+
|
35
|
+
## Usage
|
36
|
+
|
37
|
+
In this example you’re passing a thread-local `:request_store`
|
38
|
+
value from parent fiber to child fiber. They're sharing the
|
39
|
+
same object in memory but there's no risk of a race condition
|
40
|
+
because these are fibers, not threads.
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
# `new` is a Proc that returns a value in the parent fiber context.
|
44
|
+
# `resume` is a Proc that takes that value as a param and runs
|
45
|
+
# in the child fiber context.
|
46
|
+
Fiber.hook(
|
47
|
+
new: -> { Thread.current[:request_store] },
|
48
|
+
resume: ->(value) { Thread.current[:request_store] = value }
|
49
|
+
)
|
50
|
+
```
|
51
|
+
|
52
|
+
Need to remove a hook? Call `Fiber.unhook`:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
hook_id = Fiber.hook(...)
|
56
|
+
...
|
57
|
+
Fiber.unhook(hook_id)
|
58
|
+
```
|
59
|
+
|
60
|
+
## Development
|
61
|
+
|
62
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
63
|
+
|
64
|
+
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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
65
|
+
|
66
|
+
## Contributing
|
67
|
+
|
68
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/BMorearty/fiber_hook.
|
69
|
+
|
70
|
+
## License
|
71
|
+
|
72
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "fiber_hook"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require "irb"
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/fiber_hook.gemspec
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/fiber_hook/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "fiber_hook"
|
7
|
+
spec.version = FiberHook::VERSION
|
8
|
+
spec.authors = ["Brian Morearty"]
|
9
|
+
spec.email = ["brian@morearty.org"]
|
10
|
+
|
11
|
+
spec.summary = "Lets you hook into Fiber.new and Fiber.resume"
|
12
|
+
spec.description = "Lets you hook into Fiber.new and Fiber.resume"
|
13
|
+
spec.homepage = "https://github.com/BMorearty/fiber_hook"
|
14
|
+
spec.license = "MIT"
|
15
|
+
spec.required_ruby_version = ">= 2.5.0"
|
16
|
+
|
17
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
18
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
19
|
+
spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/main/CHANGELOG.md"
|
20
|
+
spec.metadata["rubygems_mfa_required"] = "true"
|
21
|
+
|
22
|
+
# Specify which files should be added to the gem when it is released.
|
23
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
24
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
25
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
26
|
+
f.match(%r{\A(?:test|spec|features)/})
|
27
|
+
end
|
28
|
+
end
|
29
|
+
spec.require_paths = ["lib"]
|
30
|
+
|
31
|
+
# Uncomment to register a new dependency of your gem
|
32
|
+
# spec.add_dependency "example-gem", "~> 1.0"
|
33
|
+
end
|
data/lib/fiber_hook.rb
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "fiber"
|
4
|
+
require_relative "fiber_hook/version"
|
5
|
+
|
6
|
+
# Allows you to hook fiber creation so you can call a method from the parent
|
7
|
+
# fiber immediately before any child fiber is created, return a value,
|
8
|
+
# and then call another method from inside the child fiber the first time the
|
9
|
+
# fiber is resumed, passing in the value that was returned from the first
|
10
|
+
# method.
|
11
|
+
module FiberHook
|
12
|
+
class Error < StandardError; end
|
13
|
+
|
14
|
+
class << self
|
15
|
+
attr_reader :hooks
|
16
|
+
end
|
17
|
+
|
18
|
+
@hooks = {}
|
19
|
+
@prev_id = 0
|
20
|
+
|
21
|
+
# Add a hook and return its id.
|
22
|
+
# @param new [Proc] Method to be called in parent fiber context when +Fiber.new+ is called.
|
23
|
+
# Takes no params. Its return value will be passed into +resume+.
|
24
|
+
# @param resume [Proc] Method to be called in child fiber's context when +Fiber#resume+
|
25
|
+
# is called for the first time. Takes a single param: the value returned by +new+.
|
26
|
+
# @return [Integer] The id of the newly-created hook. Can be passed in to +has?+ or
|
27
|
+
# +remove+.
|
28
|
+
def self.add(new: nil, resume: nil)
|
29
|
+
@prev_id += 1
|
30
|
+
@hooks[@prev_id] = { new: new, resume: resume }
|
31
|
+
@prev_id
|
32
|
+
end
|
33
|
+
|
34
|
+
# Is this hook id valid?
|
35
|
+
def self.has?(hook_id)
|
36
|
+
@hooks.key?(hook_id)
|
37
|
+
end
|
38
|
+
|
39
|
+
# Remove a hook by its id. Afterward, newly-created fibers won't have this hook.
|
40
|
+
def self.remove(hook_id)
|
41
|
+
value = @hooks.delete(hook_id)
|
42
|
+
raise Error, "Hook #{hook_id} not found" unless value
|
43
|
+
end
|
44
|
+
|
45
|
+
# Class methods that will be added to +Fiber+.
|
46
|
+
module ClassMethods
|
47
|
+
def new(*args, &block)
|
48
|
+
# In Fiber.new, call the :new methods of all the hooks. Save the results.
|
49
|
+
values = FiberHook.hooks.transform_values { |hook| hook[:new]&.call }
|
50
|
+
|
51
|
+
fiber_proc = proc do |*block_args|
|
52
|
+
# In Fiber.resume, call the :resume methods of all the hooks.
|
53
|
+
# Pass in the values returned by the :new methods.
|
54
|
+
FiberHook.hooks.each { |id, hook| hook[:resume]&.call(values[id]) }
|
55
|
+
# Then call the original fiber block.
|
56
|
+
block.call(*block_args)
|
57
|
+
end
|
58
|
+
|
59
|
+
super(*args, &fiber_proc)
|
60
|
+
end
|
61
|
+
|
62
|
+
# @see FiberHook.add
|
63
|
+
def hook(**options)
|
64
|
+
FiberHook.add(**options)
|
65
|
+
end
|
66
|
+
|
67
|
+
# @see FiberHook.remove
|
68
|
+
def unhook(hook_id)
|
69
|
+
FiberHook.remove(hook_id)
|
70
|
+
end
|
71
|
+
|
72
|
+
# @see FiberHook.has?
|
73
|
+
def hook?(hook_id)
|
74
|
+
FiberHook.has?(hook_id)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
Fiber.extend FiberHook::ClassMethods
|
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fiber_hook
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brian Morearty
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-11-21 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Lets you hook into Fiber.new and Fiber.resume
|
14
|
+
email:
|
15
|
+
- brian@morearty.org
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".github/workflows/main.yml"
|
21
|
+
- ".gitignore"
|
22
|
+
- ".rspec"
|
23
|
+
- ".rubocop.yml"
|
24
|
+
- CHANGELOG.md
|
25
|
+
- Gemfile
|
26
|
+
- Gemfile.lock
|
27
|
+
- LICENSE.txt
|
28
|
+
- README.md
|
29
|
+
- Rakefile
|
30
|
+
- bin/console
|
31
|
+
- bin/setup
|
32
|
+
- fiber_hook.gemspec
|
33
|
+
- lib/fiber_hook.rb
|
34
|
+
- lib/fiber_hook/version.rb
|
35
|
+
homepage: https://github.com/BMorearty/fiber_hook
|
36
|
+
licenses:
|
37
|
+
- MIT
|
38
|
+
metadata:
|
39
|
+
homepage_uri: https://github.com/BMorearty/fiber_hook
|
40
|
+
source_code_uri: https://github.com/BMorearty/fiber_hook
|
41
|
+
changelog_uri: https://github.com/BMorearty/fiber_hook/blob/main/CHANGELOG.md
|
42
|
+
rubygems_mfa_required: 'true'
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: 2.5.0
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
requirements: []
|
58
|
+
rubygems_version: 3.2.31
|
59
|
+
signing_key:
|
60
|
+
specification_version: 4
|
61
|
+
summary: Lets you hook into Fiber.new and Fiber.resume
|
62
|
+
test_files: []
|