spider-gazelle 3.0.9 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a1fb9d631f4b6388900685024833ffcc2ea4e27f
4
- data.tar.gz: e0a966bb202f0981be2fbeafc239165423562779
3
+ metadata.gz: d64004e93ed47b0a816510fb7e1c2ef3fd587e1e
4
+ data.tar.gz: f3e23bff3cb16a0442ad6037872674d2dd3c7793
5
5
  SHA512:
6
- metadata.gz: c6cb52ce7c34a05e486bba8f4a2cb9194fe65858c22eadf33f60bb8af64c9f5b0c091f73d098e5ff67050404cd2096cd073d34bab79693138e2487e7503dbd69
7
- data.tar.gz: e2dedb5e93f053a641270dccecb9ef9e32628653f0f89c74dc5be0cb4bee56d76a5825f07acb34b65f51df6e1bbc96c8be1c16a83fd8954b567f655440d1c473
6
+ metadata.gz: 1fa29f7d4dc35e3c870fe7fe4c0fa7755a09c6caa2296619d1c2b227430869914bcd030d77819f1ddd9dbbf1e28be6a15767661582b498c02cc34f315681064b
7
+ data.tar.gz: 0354d77ffd7971b1918eb5db4c7a6385b5b4708ff37eef32968e1bedd0cffa2060799041fcdedeb8eb265f6fc4fd3e599d2a80bd6e124391d31b6e6803318f3f
@@ -14,7 +14,6 @@ module SpiderGazelle
14
14
  @thread = ::Libuv::Reactor.default
15
15
  @logger = Logger.instance
16
16
  @running = false
17
- @shutdown = method(:shutdown)
18
17
  @shutdown_called = false
19
18
  end
20
19
 
@@ -23,8 +22,8 @@ module SpiderGazelle
23
22
  @thread.schedule { yield }
24
23
  else
25
24
  @running = true
26
- @thread.notifier method(:log)
27
- @thread.on_program_interrupt @shutdown
25
+ @thread.notifier { |*args| log(*args) }
26
+ @thread.on_program_interrupt { shutdown }
28
27
  @thread.run { yield }
29
28
  end
30
29
  end
@@ -89,8 +89,6 @@ module SpiderGazelle
89
89
 
90
90
  def connect_to_sg_master
91
91
  @pipe = @thread.pipe :ipc
92
-
93
- process = method(:process_response)
94
92
  @pipe.connect(SIGNAL_SERVER) do |client|
95
93
  @is_client = true
96
94
  @is_connected = true
@@ -103,7 +101,12 @@ module SpiderGazelle
103
101
  delimiter: "\x03"
104
102
  })
105
103
 
106
- client.progress process
104
+ # The client processes responses here
105
+ client.progress do |data, server|
106
+ @parser.extract(data).each do |msg|
107
+ Spider.instance.__send__(msg)
108
+ end
109
+ end
107
110
  client.start_read
108
111
  @client_check.resolve(true)
109
112
  end
@@ -138,7 +141,6 @@ module SpiderGazelle
138
141
  rescue
139
142
  end
140
143
 
141
- process = method(:process_request)
142
144
  @pipe.bind(SIGNAL_SERVER) do |client|
143
145
  @logger.verbose { "Client <0x#{client.object_id.to_s(16)}> connection made" }
144
146
  @validating[client.object_id] = SignalParser.new
@@ -159,12 +161,14 @@ module SpiderGazelle
159
161
  end
160
162
  end
161
163
 
162
- client.progress process
164
+ client.progress do |data, client|
165
+ process_request(data, client)
166
+ end
163
167
  client.start_read
164
168
  end
165
169
 
166
170
  # catch server errors
167
- @pipe.catch method(:panic!)
171
+ @pipe.catch { |error| panic! error }
168
172
  @pipe.finally { @is_connected = false }
169
173
 
170
174
  # start listening
@@ -208,12 +212,5 @@ module SpiderGazelle
208
212
  end
209
213
  end
210
214
  end
211
-
212
- # The client processes responses here
213
- def process_response(data, server)
214
- @parser.extract(data).each do |msg|
215
- Spider.instance.__send__(msg)
216
- end
217
- end
218
215
  end
219
216
  end
@@ -181,7 +181,7 @@ module SpiderGazelle
181
181
 
182
182
  def load_gazelle_thread(reactor, thread, mode, options, loading)
183
183
  # Log any unhandled errors
184
- thread.notifier reactor.method(:log)
184
+ thread.notifier { |*args| reactor.log(*args) }
185
185
  # Give current requests 5 seconds to complete
186
186
  thread.on_program_interrupt do
187
187
  timer = thread.timer {
@@ -19,12 +19,10 @@ module SpiderGazelle
19
19
  @port = @options[:port]
20
20
  @app, @app_port, @tls = AppStore.lookup(options[:rackup])
21
21
 
22
- @set_protocol = method(:set_protocol)
23
-
24
22
  @http1_cache = []
25
23
  @http2_cache = []
26
- @return_http1 = method(:return_http1)
27
- @return_http2 = method(:return_http2)
24
+ @return_http1 = proc { |parser| @http1_cache << parser }
25
+ @return_http2 = proc { |parser| @http2_cache << parser }
28
26
  @parser_count = 0
29
27
  end
30
28
 
@@ -78,7 +76,7 @@ module SpiderGazelle
78
76
  end
79
77
 
80
78
  def prepare_client_tls(client)
81
- client.on_handshake @set_protocol
79
+ client.on_handshake { |client, version| set_protocol(client, version) }
82
80
  client.start_tls(@tls)
83
81
 
84
82
  client.start_read
@@ -93,7 +91,7 @@ module SpiderGazelle
93
91
  end
94
92
 
95
93
  parser.load(client, @app_port, @app, @tls)
96
- client.progress do |data, _|
94
+ client.progress do |data|
97
95
  parser.parse(data)
98
96
  end
99
97
  end
@@ -109,19 +107,11 @@ module SpiderGazelle
109
107
  Http1.new(@return_http1, @h1_parser_obj, @thread, @logger, @iterator)
110
108
  end
111
109
 
112
- def return_http1(parser)
113
- @http1_cache.push parser
114
- end
115
-
116
110
  def new_http2_parser
117
111
  raise NotImplementedError.new 'TODO:: Create HTTP2 parser class'
118
112
  @parser_count += 1
119
113
  Http2.new(@return_http2, @thread, @logger, @iterator)
120
114
  end
121
-
122
- def return_http2(parser)
123
- @http2_cache << parser
124
- end
125
115
  end
126
116
  end
127
117
  end
@@ -74,8 +74,6 @@ module SpiderGazelle
74
74
  @thread = thread
75
75
  @logger = logger
76
76
 
77
- @queue_response = method :queue_response
78
- @write_chunk = method :write_chunk
79
77
  @gazelles = gazelles
80
78
 
81
79
  # The parser state for this instance
@@ -91,9 +89,7 @@ module SpiderGazelle
91
89
 
92
90
  attr_reader :parsing
93
91
 
94
-
95
- def self.on_progress(data, socket); end
96
- DUMMY_PROGRESS = self.method :on_progress
92
+ DUMMY_PROGRESS = proc {}
97
93
 
98
94
  def load(socket, port, app, tls)
99
95
  @socket = socket
@@ -113,7 +109,7 @@ module SpiderGazelle
113
109
 
114
110
  def on_close
115
111
  # Unlink the progress callback (prevent funny business)
116
- @socket.progress DUMMY_PROGRESS
112
+ @socket.progress &DUMMY_PROGRESS
117
113
  @socket.storage = nil
118
114
  reset
119
115
  @return_method.call(self)
@@ -189,7 +185,15 @@ module SpiderGazelle
189
185
  @processing = @requests.shift
190
186
  if @processing
191
187
  request = @processing
192
- request.then @queue_response
188
+
189
+ # queue response
190
+ request.then do |response|
191
+ @responses << response
192
+ send_next_response unless @transmitting
193
+
194
+ # Processing will be set to nil if the array is empty
195
+ process_next
196
+ end
193
197
 
194
198
  @gazelles.next.schedule do
195
199
  process_on_gazelle(request)
@@ -222,14 +226,6 @@ module SpiderGazelle
222
226
  # ----------------
223
227
  # Response Sending
224
228
  # ----------------
225
- def queue_response(response)
226
- @responses << response
227
- send_next_response unless @transmitting
228
-
229
- # Processing will be set to nil if the array is empty
230
- process_next
231
- end
232
-
233
229
  def send_next_response
234
230
  request, result = @responses.shift
235
231
  @transmitting = request
@@ -355,14 +351,17 @@ module SpiderGazelle
355
351
  write_headers keep_alive, status, headers
356
352
 
357
353
  # Stream the response (pass directly into @socket.write)
358
- body.each &@socket.method(:write)
354
+ body.each { |data| @socket.write(data) }
359
355
  @socket.shutdown if keep_alive == false
360
356
  else
361
357
  headers['Transfer-Encoding'] = 'chunked'
362
358
  write_headers keep_alive, status, headers
363
359
 
364
360
  # Stream the response
365
- body.each &@write_chunk
361
+ body.each do |part|
362
+ chunk = part.bytesize.to_s(16) << "\r\n" << part << "\r\n"
363
+ @socket.write chunk
364
+ end
366
365
 
367
366
  @socket.write "0\r\n\r\n"
368
367
  @socket.shutdown if keep_alive == false
@@ -390,11 +389,6 @@ module SpiderGazelle
390
389
  @socket.write header
391
390
  end
392
391
 
393
- def write_chunk(part)
394
- chunk = part.bytesize.to_s(16) << "\r\n" << part << "\r\n"
395
- @socket.write chunk
396
- end
397
-
398
392
  HTTP_STATUS_CODES = Rack::Utils::HTTP_STATUS_CODES
399
393
  HTTP_STATUS_DEFAULT = proc { 'CUSTOM' }
400
394
  def fetch_code(status)
@@ -21,15 +21,34 @@ class SpiderGazelle::Websocket < ::Libuv::Q::DeferredPromise
21
21
  @driver = ::WebSocket::Driver.rack self
22
22
 
23
23
  # Pass data from the socket to the driver
24
- @socket.progress &method(:socket_read)
25
- @socket.finally &method(:socket_close)
26
-
24
+ @socket.progress do |data|
25
+ begin
26
+ @driver.parse data
27
+ rescue Exception => e
28
+ # Prevent hanging sockets
29
+ @socket.close
30
+ raise e
31
+ end
32
+ end
33
+ @socket.finally do
34
+ if @shutdown_called.nil?
35
+ @defer.reject WebSocket::Driver::CloseEvent.new(1006, 'connection was closed unexpectedly')
36
+ end
37
+ end
27
38
 
28
39
  # Driver has indicated that it is closing
29
40
  # We'll close the socket after writing any remaining data
30
- @driver.on :close, &method(:on_close)
31
- @driver.on :message, &method(:on_message)
32
- @driver.on :error, &method(:on_error)
41
+ @driver.on :close do |event|
42
+ @shutdown_called = true
43
+ @socket.shutdown
44
+ @defer.resolve event
45
+ end
46
+ @driver.on :message do |event|
47
+ @progress.call(event.data, self) unless @progress.nil?
48
+ end
49
+ @driver.on :error do |event|
50
+ @defer.reject event
51
+ end
33
52
  end
34
53
 
35
54
  # Write some text to the websocket connection
@@ -61,37 +80,4 @@ class SpiderGazelle::Websocket < ::Libuv::Q::DeferredPromise
61
80
  callback ||= blk
62
81
  @driver.on :open, &callback
63
82
  end
64
-
65
- protected
66
-
67
- def socket_read(data, tcp)
68
- begin
69
- @driver.parse data
70
- rescue => e
71
- # Prevent hanging sockets
72
- @socket.close
73
- raise e
74
- end
75
- end
76
-
77
- def socket_close
78
- if @shutdown_called.nil?
79
- @defer.reject WebSocket::Driver::CloseEvent.new(1006, 'connection was closed unexpectedly')
80
- end
81
- end
82
-
83
-
84
- def on_message(event)
85
- @progress.call(event.data, self) unless @progress.nil?
86
- end
87
-
88
- def on_error(event)
89
- @defer.reject event
90
- end
91
-
92
- def on_close(event)
93
- @shutdown_called = true
94
- @socket.shutdown
95
- @defer.resolve event
96
- end
97
83
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SpiderGazelle
4
- VERSION = '3.0.9'
4
+ VERSION = '3.1.0'
5
5
  EXEC_NAME = 'sg'
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spider-gazelle
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.9
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephen von Takach
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-18 00:00:00.000000000 Z
11
+ date: 2017-11-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: http-parser