iopromise 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,17 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'faraday/connection'
4
-
5
- module IOPromise
6
- module Faraday
7
- class << self
8
- def new(url = nil, options = {}, &block)
9
- options = ::Faraday.default_connection_options.merge(options)
10
- ::IOPromise::Faraday::Connection.new(url, options) do |faraday|
11
- faraday.adapter :typhoeus
12
- block.call unless block.nil?
13
- end
14
- end
15
- end
16
- end
17
- end
@@ -1,25 +0,0 @@
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 with_deferred_parallel
11
- @parallel_manager = FaradayPromise.parallel_manager
12
- yield
13
- ensure
14
- @parallel_manager = nil
15
- end
16
-
17
- def get_as_promise(*args, **kwargs)
18
- @parallel_manager = FaradayPromise.parallel_manager
19
- FaradayPromise.new(get(*args, **kwargs))
20
- ensure
21
- @parallel_manager = nil
22
- end
23
- end
24
- end
25
- end
@@ -1,29 +0,0 @@
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 execute_continue(ready_readers, ready_writers, ready_exceptions)
22
- # fill up the curl easy handle as much as possible
23
- dequeue_many
24
-
25
- @multi.execute_continue(ready_readers, ready_writers, ready_exceptions)
26
- end
27
- end
28
- end
29
- end
@@ -1,19 +0,0 @@
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 execute_continue(ready_readers, ready_writers, ready_exceptions)
9
- # mark all pending promises as executing since they could be started any time now.
10
- # ideally we would do this on dequeue.
11
- @pending.each do |promise|
12
- begin_executing(promise) unless promise.started_executing?
13
- end
14
-
15
- ContinuableHydra.for_current_thread.execute_continue(ready_readers, ready_writers, ready_exceptions)
16
- end
17
- end
18
- end
19
- end
@@ -1,107 +0,0 @@
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
- def initialize(options = {})
27
- super(options)
28
-
29
- @read_fds = {}
30
- @write_fds = {}
31
- @select_timeout = nil
32
-
33
- self.socketfunction = @keep_socketfunction = proc do |handle, sock, what, userp, socketp|
34
- if what == CURL_POLL_REMOVE
35
- @read_fds.delete(sock)
36
- @write_fds.delete(sock)
37
- else
38
- # reuse existing if we have it anywhere
39
- io = @read_fds[sock] || @write_fds[sock] || IO.for_fd(sock).tap { |io| io.autoclose = false }
40
- if what == CURL_POLL_INOUT
41
- @read_fds[sock] = io
42
- @write_fds[sock] = io
43
- elsif what == CURL_POLL_IN
44
- @read_fds[sock] = io
45
- @write_fds.delete(sock)
46
- elsif what == CURL_POLL_OUT
47
- @read_fds.delete(sock)
48
- @write_fds[sock] = io
49
- end
50
- end
51
- CURLM_OK
52
- end
53
-
54
- self.timerfunction = @keep_timerfunction = proc do |handle, timeout_ms, userp|
55
- if timeout_ms > 0x7fffffffffffffff # FIXME: wrongly encoded
56
- @select_timeout = nil
57
- else
58
- @select_timeout = timeout_ms.to_f / 1_000
59
- end
60
- CURLM_OK
61
- end
62
- end
63
-
64
- def perform
65
- # stubbed out, we don't want any of the multi_perform logic
66
- end
67
-
68
- def run
69
- # stubbed out, we don't want any of the multi_perform logic
70
- end
71
-
72
- def execute_continue(ready_readers, ready_writers, ready_exceptions)
73
- running_handles = ::FFI::MemoryPointer.new(:int)
74
-
75
- flags = Hash.new(0)
76
-
77
- unless ready_readers.nil?
78
- ready_readers.each do |s|
79
- flags[s.fileno] |= CURL_CSELECT_IN
80
- end
81
- end
82
- unless ready_writers.nil?
83
- ready_writers.each do |s|
84
- flags[s.fileno] |= CURL_CSELECT_OUT
85
- end
86
- end
87
- unless ready_exceptions.nil?
88
- ready_exceptions.each do |s|
89
- flags[s.fileno] |= CURL_CSELECT_ERR
90
- end
91
- end
92
-
93
- flags.each do |fd, bitmask|
94
- Ethon::Curl.multi_socket_action(handle, fd, bitmask, running_handles)
95
- end
96
-
97
- if flags.empty?
98
- Ethon::Curl.multi_socket_action(handle, CURL_SOCKET_TIMEOUT, 0, running_handles)
99
- end
100
-
101
- check
102
-
103
- [@read_fds.values, @write_fds.values, [], @select_timeout]
104
- end
105
- end
106
- end
107
- end
@@ -1,42 +0,0 @@
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
- execute_pool.complete(self)
23
- end
24
- end
25
-
26
- ::IOPromise::ExecutorContext.current.register(self) unless @response.nil?
27
- end
28
-
29
- def wait
30
- if @response.nil?
31
- super
32
- else
33
- ::IOPromise::ExecutorContext.current.wait_for_all_data(end_when_complete: self)
34
- end
35
- end
36
-
37
- def execute_pool
38
- FaradayExecutorPool.for(Thread.current)
39
- end
40
- end
41
- end
42
- end
@@ -1,20 +0,0 @@
1
-
2
- module IOPromise
3
- module Rack
4
- class ContextMiddleware
5
- def initialize(app)
6
- @app = app
7
- end
8
-
9
- def call(env)
10
- ::IOPromise::ExecutorContext.push
11
- begin
12
- status, headers, body = @app.call(env)
13
- ensure
14
- ::IOPromise::ExecutorContext.pop
15
- end
16
- [status, headers, body]
17
- end
18
- end
19
- end
20
- end