iopromise-faraday 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: 29ef0d8ff9445a5b2b5df2f1c941042f93d67e321875a3dfa805a66b36a6ea59
4
+ data.tar.gz: '09188af815dd7f5f9f836e1c2ddf291d02ef044650f051b4dd8c896c681bafd0'
5
+ SHA512:
6
+ metadata.gz: d2159f8ac5cf31233c9f0dc4279f2331dbe3ca3c25ce1cd3080277c50d23aa479da04ed1f152b82209e9e9f212670d1ada579227fe8f7f6e613e9077f9b9267d
7
+ data.tar.gz: 0cb1930b3e308094fd17bf9a9c023e7e67a371271373106dccceca91f02aecc5b0945b5dc30d7dff5469947efdc26395988e906c29e9e1c5655aad1176807dfe
data/.gitignore ADDED
@@ -0,0 +1,57 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ # Ignore Byebug command history file.
17
+ .byebug_history
18
+
19
+ ## Specific to RubyMotion:
20
+ .dat*
21
+ .repl_history
22
+ build/
23
+ *.bridgesupport
24
+ build-iPhoneOS/
25
+ build-iPhoneSimulator/
26
+
27
+ ## Specific to RubyMotion (use of CocoaPods):
28
+ #
29
+ # We recommend against adding the Pods directory to your .gitignore. However
30
+ # you should judge for yourself, the pros and cons are mentioned at:
31
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
32
+ #
33
+ # vendor/Pods/
34
+
35
+ ## Documentation cache and generated files:
36
+ /.yardoc/
37
+ /_yardoc/
38
+ /doc/
39
+ /rdoc/
40
+
41
+ ## Environment normalization:
42
+ /.bundle/
43
+ /vendor/bundle
44
+ /lib/bundler/man/
45
+
46
+ # for a library or gem, you might want to ignore these files since the code is
47
+ # intended to run in multiple environments; otherwise, check them in:
48
+ # Gemfile.lock
49
+ # .ruby-version
50
+ # .ruby-gemset
51
+
52
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
53
+ .rvmrc
54
+
55
+ # Used by RuboCop. Remote config files pulled in from inherit_from directive.
56
+ # .rubocop-https?--*
57
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -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 theojulienne@github.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,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in iopromise.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ group :development, :test do
13
+ # benchmarking
14
+ gem "benchmark-ips"
15
+ gem "stackprof"
16
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,67 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ iopromise-faraday (0.1.0)
5
+ ethon (= 0.14.0)
6
+ faraday (= 1.4.2)
7
+ iopromise (~> 0.1.2)
8
+ typhoeus (= 1.4.0)
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ benchmark-ips (2.9.1)
14
+ diff-lcs (1.4.4)
15
+ ethon (0.14.0)
16
+ ffi (>= 1.15.0)
17
+ faraday (1.4.2)
18
+ faraday-em_http (~> 1.0)
19
+ faraday-em_synchrony (~> 1.0)
20
+ faraday-excon (~> 1.1)
21
+ faraday-net_http (~> 1.0)
22
+ faraday-net_http_persistent (~> 1.1)
23
+ multipart-post (>= 1.2, < 3)
24
+ ruby2_keywords (>= 0.0.4)
25
+ faraday-em_http (1.0.0)
26
+ faraday-em_synchrony (1.0.0)
27
+ faraday-excon (1.1.0)
28
+ faraday-net_http (1.0.1)
29
+ faraday-net_http_persistent (1.1.0)
30
+ ffi (1.15.3)
31
+ iopromise (0.1.2)
32
+ nio4r
33
+ promise.rb (~> 0.7.4)
34
+ multipart-post (2.1.1)
35
+ nio4r (2.5.7)
36
+ promise.rb (0.7.4)
37
+ rake (13.0.3)
38
+ rspec (3.10.0)
39
+ rspec-core (~> 3.10.0)
40
+ rspec-expectations (~> 3.10.0)
41
+ rspec-mocks (~> 3.10.0)
42
+ rspec-core (3.10.1)
43
+ rspec-support (~> 3.10.0)
44
+ rspec-expectations (3.10.1)
45
+ diff-lcs (>= 1.2.0, < 2.0)
46
+ rspec-support (~> 3.10.0)
47
+ rspec-mocks (3.10.2)
48
+ diff-lcs (>= 1.2.0, < 2.0)
49
+ rspec-support (~> 3.10.0)
50
+ rspec-support (3.10.2)
51
+ ruby2_keywords (0.0.4)
52
+ stackprof (0.2.17)
53
+ typhoeus (1.4.0)
54
+ ethon (>= 0.9.0)
55
+
56
+ PLATFORMS
57
+ x86_64-linux
58
+
59
+ DEPENDENCIES
60
+ benchmark-ips
61
+ iopromise-faraday!
62
+ rake (~> 13.0)
63
+ rspec (~> 3.0)
64
+ stackprof
65
+
66
+ BUNDLED WITH
67
+ 2.2.17
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 iopromise-ruby
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,57 @@
1
+ # iopromise-faraday
2
+
3
+ This gem provides a promise-based parallel interface to Faraday, based on [IOPromise](https://github.com/iopromise-ruby/iopromise).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'iopromise-faraday'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install iopromise-faraday
20
+
21
+ ## Usage
22
+
23
+ The pattern for using `IOPromise::Faraday` is very similar to regular Faraday, except rather than returning a response object, HTTP requests return promises that resolve to a response object. There is no explicit need to wrap parallel calls in a block, any pending requests will execute in parallel, automatically, when the promise(s) are synced.
24
+
25
+ ```ruby
26
+ require 'iopromise/faraday'
27
+
28
+ conn = IOPromise::Faraday.new('https://github.com/')
29
+
30
+ promises = (1..3).map do
31
+ conn.get('/status')
32
+ end
33
+
34
+ Promise.all(promises).then do |responses|
35
+ responses.each_with_index do |response, i|
36
+ puts "#{i}: #{response.body.strip} #{response.headers["x-github-request-id"]}"
37
+ end
38
+ end.sync
39
+ ```
40
+
41
+ ## Development
42
+
43
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
44
+
45
+ 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).
46
+
47
+ ## Contributing
48
+
49
+ Bug reports and pull requests are welcome on GitHub at https://github.com/iopromise-ruby/iopromise-faraday. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/iopromise-ruby/iopromise-faraday/blob/main/CODE_OF_CONDUCT.md).
50
+
51
+ ## License
52
+
53
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
54
+
55
+ ## Code of Conduct
56
+
57
+ Everyone interacting in the iopromise project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/iopromise-ruby/iopromise-faraday/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
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
+ task default: :spec
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 "iopromise/faraday"
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
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/iopromise/faraday/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "iopromise-faraday"
7
+ spec.version = IOPromise::Faraday::VERSION
8
+ spec.authors = ["Theo Julienne"]
9
+ spec.email = ["theo.julienne@gmail.com"]
10
+
11
+ spec.summary = "Parallel Faraday support via IOPromise"
12
+ spec.description = "This gem provides a promise-based interface to Faraday using IOPromise"
13
+ spec.homepage = "https://github.com/iopromise-ruby/iopromise-faraday"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = spec.homepage
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
24
+ end
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_dependency 'iopromise', '~> 0.1.2'
30
+
31
+ # we pin these very specifically while we are monkey patching support
32
+ spec.add_dependency 'faraday', '1.4.2'
33
+ spec.add_dependency 'typhoeus', '1.4.0'
34
+ spec.add_dependency 'ethon', '0.14.0'
35
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'iopromise'
4
+ require_relative 'faraday/client'
5
+
6
+ module IOPromise
7
+ module Faraday
8
+ class << self
9
+ def new(url = nil, options = {}, &block)
10
+ ::IOPromise::Faraday::Client.new(url, options, &block)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'connection'
4
+
5
+ module IOPromise
6
+ module Faraday
7
+ class Client
8
+ def initialize(url = nil, options = {}, &block)
9
+ options = ::Faraday.default_connection_options.merge(options)
10
+ @conn = ::IOPromise::Faraday::Connection.new(url, options) do |faraday|
11
+ faraday.adapter :typhoeus
12
+ block.call(faraday) unless block.nil?
13
+ end
14
+ end
15
+
16
+ (::Faraday::METHODS_WITH_QUERY + ::Faraday::METHODS_WITH_BODY).each do |method|
17
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
18
+ def #{method}(*args, **kwargs)
19
+ FaradayPromise.new(@conn.#{method}(*args, **kwargs))
20
+ end
21
+ RUBY
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'faraday'
4
+
5
+ require_relative 'promise'
6
+
7
+ module IOPromise
8
+ module Faraday
9
+ class Connection < ::Faraday::Connection
10
+ def initialize(*)
11
+ super
12
+
13
+ @parallel_manager = FaradayPromise.parallel_manager
14
+ end
15
+
16
+ def in_parallel(manager = nil)
17
+ # not supported, we're always in parallel
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'typhoeus'
4
+ require_relative 'multi_socket_action'
5
+
6
+ module IOPromise
7
+ module Faraday
8
+ class ContinuableHydra < Typhoeus::Hydra
9
+ class << self
10
+ def for_current_thread
11
+ Thread.current[:faraday_promise_typhoeus_hydra] ||= new
12
+ end
13
+ end
14
+
15
+ def initialize(options = {})
16
+ super(options)
17
+
18
+ @multi = MultiSocketAction.new(options.reject{|k,_| k==:max_concurrency})
19
+ end
20
+
21
+ def iop_handler=(iop_handler)
22
+ @multi.iop_handler = iop_handler
23
+ end
24
+
25
+ def socket_is_ready(io, readable, writable)
26
+ @multi.socket_is_ready(io, readable, writable)
27
+ end
28
+
29
+ def execute_continue
30
+ # fill up the curl easy handle as much as possible
31
+ dequeue_many
32
+
33
+ @multi.execute_continue
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'continuable_hydra'
4
+
5
+ module IOPromise
6
+ module Faraday
7
+ class FaradayExecutorPool < IOPromise::ExecutorPool::Base
8
+ def initialize(*)
9
+ super
10
+
11
+ @hydra = ContinuableHydra.for_current_thread
12
+ @hydra.iop_handler = self
13
+
14
+ @monitors = {}
15
+ end
16
+
17
+ def monitor_add(io)
18
+ @monitors[io] = ::IOPromise::ExecutorContext.current.register_observer_io(self, io, :r)
19
+ end
20
+
21
+ def monitor_remove(io)
22
+ monitor = @monitors.delete(io)
23
+ monitor.close unless monitor.nil?
24
+ end
25
+
26
+ def set_interests(io, interest)
27
+ monitor = @monitors[io]
28
+ monitor.interests = interest unless monitor.nil?
29
+ end
30
+
31
+ def set_timeout(timeout)
32
+ self.select_timeout = timeout
33
+ end
34
+
35
+ def monitor_ready(monitor, readiness)
36
+ @hydra.socket_is_ready(monitor.io, monitor.readable?, monitor.writable?)
37
+ end
38
+
39
+ def execute_continue
40
+ # mark all pending promises as executing since they could be started any time now.
41
+ # ideally we would do this on dequeue.
42
+ @pending.each do |promise|
43
+ begin_executing(promise) unless promise.started_executing?
44
+ end
45
+
46
+ @hydra.execute_continue
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,102 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ethon'
4
+
5
+ Ethon::Curl.ffi_lib 'curl'
6
+ Ethon::Curl.attach_function :multi_socket_action, :curl_multi_socket_action, [:pointer, :int, :int, :pointer], :multi_code
7
+
8
+ module IOPromise
9
+ module Faraday
10
+ class MultiSocketAction < Ethon::Multi
11
+ CURL_POLL_NONE = 0
12
+ CURL_POLL_IN = 1
13
+ CURL_POLL_OUT = 2
14
+ CURL_POLL_INOUT = 3
15
+ CURL_POLL_REMOVE = 4
16
+
17
+ CURL_SOCKET_BAD = -1
18
+ CURL_SOCKET_TIMEOUT = CURL_SOCKET_BAD
19
+
20
+ CURLM_OK = 0
21
+
22
+ CURL_CSELECT_IN = 0x01
23
+ CURL_CSELECT_OUT = 0x02
24
+ CURL_CSELECT_ERR = 0x04
25
+
26
+ attr_accessor :iop_handler
27
+
28
+ def initialize(options = {})
29
+ super(options)
30
+
31
+ @ios = {}
32
+ @iop_handler = nil
33
+ @notified_fds = 0
34
+
35
+ self.socketfunction = @keep_socketfunction = proc do |handle, sock, what, userp, socketp|
36
+ if what == CURL_POLL_REMOVE
37
+ io = @ios.delete(sock)
38
+ iop_handler.monitor_remove(io) unless io.nil?
39
+ else
40
+ # reuse existing if we have it anywhere
41
+ io = @ios[sock]
42
+ if io.nil?
43
+ io = @ios[sock] = IO.for_fd(sock).tap { |io| io.autoclose = false }
44
+ iop_handler.monitor_add(io)
45
+ end
46
+ if what == CURL_POLL_INOUT
47
+ iop_handler.set_interests(io, :rw)
48
+ elsif what == CURL_POLL_IN
49
+ iop_handler.set_interests(io, :r)
50
+ elsif what == CURL_POLL_OUT
51
+ iop_handler.set_interests(io, :w)
52
+ end
53
+ end
54
+ CURLM_OK
55
+ end
56
+
57
+ self.timerfunction = @keep_timerfunction = proc do |handle, timeout_ms, userp|
58
+ if timeout_ms > 0x7fffffffffffffff # FIXME: wrongly encoded
59
+ select_timeout = nil
60
+ else
61
+ select_timeout = timeout_ms.to_f / 1_000
62
+ end
63
+ iop_handler.set_timeout(select_timeout)
64
+ CURLM_OK
65
+ end
66
+ end
67
+
68
+ def perform
69
+ # stubbed out, we don't want any of the multi_perform logic
70
+ end
71
+
72
+ def run
73
+ # stubbed out, we don't want any of the multi_perform logic
74
+ end
75
+
76
+ def socket_is_ready(io, readable, writable)
77
+ running_handles = ::FFI::MemoryPointer.new(:int)
78
+
79
+ bitmask = 0
80
+ bitmask |= CURL_CSELECT_IN if readable
81
+ bitmask |= CURL_CSELECT_OUT if writable
82
+
83
+ Ethon::Curl.multi_socket_action(handle, io.fileno, bitmask, running_handles)
84
+ @notified_fds += 1
85
+ end
86
+
87
+ def execute_continue
88
+ running_handles = ::FFI::MemoryPointer.new(:int)
89
+
90
+ if @notified_fds == 0
91
+ # no FDs were readable/writable so we send the timeout fd, which lets
92
+ # curl perform housekeeping.
93
+ Ethon::Curl.multi_socket_action(handle, CURL_SOCKET_TIMEOUT, 0, running_handles)
94
+ else
95
+ @notified_fds = 0
96
+ end
97
+
98
+ check
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'continuable_hydra'
4
+ require_relative 'executor_pool'
5
+
6
+ module IOPromise
7
+ module Faraday
8
+ class FaradayPromise < ::IOPromise::Base
9
+ def self.parallel_manager
10
+ ContinuableHydra.for_current_thread
11
+ end
12
+
13
+ def initialize(response = nil)
14
+ super()
15
+
16
+ @response = response
17
+ @started = false
18
+
19
+ unless @response.nil?
20
+ @response.on_complete do |response_env|
21
+ fulfill(@response)
22
+ end
23
+ end
24
+
25
+ ::IOPromise::ExecutorContext.current.register(self) unless @response.nil?
26
+ end
27
+
28
+ def wait
29
+ if @response.nil?
30
+ super
31
+ else
32
+ ::IOPromise::ExecutorContext.current.wait_for_all_data(end_when_complete: self)
33
+ end
34
+ end
35
+
36
+ def execute_pool
37
+ FaradayExecutorPool.for(Thread.current)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IOPromise
4
+ module Faraday
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: iopromise-faraday
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Theo Julienne
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-06-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: iopromise
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 1.4.2
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 1.4.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: typhoeus
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 1.4.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 1.4.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: ethon
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 0.14.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.14.0
69
+ description: This gem provides a promise-based interface to Faraday using IOPromise
70
+ email:
71
+ - theo.julienne@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - CODE_OF_CONDUCT.md
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - LICENSE
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - iopromise-faraday.gemspec
87
+ - lib/iopromise/faraday.rb
88
+ - lib/iopromise/faraday/client.rb
89
+ - lib/iopromise/faraday/connection.rb
90
+ - lib/iopromise/faraday/continuable_hydra.rb
91
+ - lib/iopromise/faraday/executor_pool.rb
92
+ - lib/iopromise/faraday/multi_socket_action.rb
93
+ - lib/iopromise/faraday/promise.rb
94
+ - lib/iopromise/faraday/version.rb
95
+ homepage: https://github.com/iopromise-ruby/iopromise-faraday
96
+ licenses:
97
+ - MIT
98
+ metadata:
99
+ homepage_uri: https://github.com/iopromise-ruby/iopromise-faraday
100
+ source_code_uri: https://github.com/iopromise-ruby/iopromise-faraday
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: 2.4.0
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubygems_version: 3.2.9
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: Parallel Faraday support via IOPromise
120
+ test_files: []