waithook 0.4.1 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/example/example.rb +1 -1
- data/example/real_server_test.rb +1 -1
- data/example/waithook_example.rb +2 -2
- data/lib/waithook/version.rb +1 -1
- data/lib/waithook/websocket_client.rb +33 -10
- data/lib/waithook.rb +5 -1
- data/tests/waithook_test.rb +16 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c3df6454a557d19f38729b89f1874b43111ab2d367c9f6f018064ad012d3ff2a
|
4
|
+
data.tar.gz: f933b2e75524e3301afbe50c8e56bbb525cea7a5503a15071cfad2ab4bd7ff59
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8f6403f65b5bf735376deea1d9eb4157b66d87428e1c13717c71b80045bfa898487863e15d9a206c59575652cfaa6003dc14885746f1dcd22a2f6ebec03eac1a
|
7
|
+
data.tar.gz: b7f95a852f39039a4832f5a0f5889163e92fb855ade1b7e33a9c5b3ad5dbd0d587f393f8925156d5e8102f055eef0f7843e93be2f5122c1d9934bdbfca0ba70d
|
data/Gemfile.lock
CHANGED
data/example/example.rb
CHANGED
data/example/real_server_test.rb
CHANGED
data/example/waithook_example.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
require_relative '
|
1
|
+
require_relative '../lib/waithook'
|
2
2
|
require 'excon'
|
3
3
|
|
4
|
-
waithook = Waithook.subscribe('my-super-test', host: 'localhost', port: 3012)
|
4
|
+
waithook = Waithook.subscribe(path: 'my-super-test', host: 'localhost', port: 3012)
|
5
5
|
|
6
6
|
Excon.post('http://localhost:3012/my-super-test', body: Time.now.to_s)
|
7
7
|
|
data/lib/waithook/version.rb
CHANGED
@@ -79,7 +79,7 @@ class Waithook
|
|
79
79
|
end
|
80
80
|
end
|
81
81
|
|
82
|
-
#
|
82
|
+
# Establish connection to server and send initial handshake
|
83
83
|
def connect!
|
84
84
|
logger.info "Connecting to #{@host} #{@port}"
|
85
85
|
|
@@ -118,7 +118,7 @@ class Waithook
|
|
118
118
|
def _start_parser!
|
119
119
|
@reader, @writter = IO.pipe
|
120
120
|
@processing_thread = Thread.new do
|
121
|
-
Thread.current.abort_on_exception = true
|
121
|
+
# Thread.current.abort_on_exception = true
|
122
122
|
begin
|
123
123
|
logger.debug "Start reading in thread"
|
124
124
|
handshake_response = _wait_handshake_response
|
@@ -129,8 +129,14 @@ class Waithook
|
|
129
129
|
_handshake_recieved!
|
130
130
|
_wait_frames!
|
131
131
|
rescue Object => error
|
132
|
+
# if got error - close socket and end thread
|
133
|
+
# @last_connection_error will be detected in #wait_message and raised to caller thread,
|
134
|
+
# so caller can decide to reconnect or do something else
|
135
|
+
@last_connection_error = error
|
136
|
+
close!(send_close: false, kill_processing_thread: false) rescue nil
|
137
|
+
@messages.close
|
138
|
+
|
132
139
|
logger.error "#{error.class}: #{error.message}\n#{error.backtrace.join("\n")}"
|
133
|
-
raise error
|
134
140
|
end
|
135
141
|
end
|
136
142
|
end
|
@@ -150,7 +156,7 @@ class Waithook
|
|
150
156
|
_send_frame(:text, payload)
|
151
157
|
end
|
152
158
|
|
153
|
-
# Wait for new message (thread safe, all waiting threads will
|
159
|
+
# Wait for new message (thread safe, all waiting threads will receive a message)
|
154
160
|
# Message format [type, data], e.g. <tt>[:text, "hello world"]</tt>
|
155
161
|
def wait_new_message
|
156
162
|
waiter = Waiter.new
|
@@ -158,14 +164,27 @@ class Waithook
|
|
158
164
|
waiter.wait
|
159
165
|
end
|
160
166
|
|
161
|
-
#
|
167
|
+
# Synchronously waiting for new message. Not thread safe, only one thread will receive message
|
162
168
|
# Message format [type, data], e.g. <tt>[:text, "hello world"]</tt>
|
163
169
|
def wait_message
|
164
|
-
|
170
|
+
begin
|
171
|
+
message = @messages.pop
|
172
|
+
if (!message || message.first == nil) && @messages.closed? && @last_connection_error
|
173
|
+
raise @last_connection_error
|
174
|
+
end
|
175
|
+
|
176
|
+
return message
|
177
|
+
rescue => error
|
178
|
+
if @last_connection_error
|
179
|
+
raise @last_connection_error
|
180
|
+
else
|
181
|
+
raise error
|
182
|
+
end
|
183
|
+
end
|
165
184
|
end
|
166
185
|
|
167
186
|
# Wait until server handshake recieved.
|
168
|
-
# Once it's
|
187
|
+
# Once it's received - we can are listening for new messages
|
169
188
|
# and connection is ready for sending data
|
170
189
|
def wait_handshake!
|
171
190
|
return true if @handshake_received
|
@@ -240,16 +259,20 @@ class Waithook
|
|
240
259
|
data.join("")
|
241
260
|
end
|
242
261
|
|
262
|
+
def socket_open?
|
263
|
+
@socket && !@socket.closed?
|
264
|
+
end
|
265
|
+
|
243
266
|
# Send <tt>:close</tt> message to socket and close socket connection
|
244
|
-
def close!(
|
267
|
+
def close!(send_close: true, kill_processing_thread: true)
|
245
268
|
unless @is_open
|
246
269
|
logger.info "Already closed"
|
247
270
|
return false
|
248
271
|
end
|
249
272
|
|
250
273
|
logger.info "Disconnecting from #{@host} #{@port}"
|
251
|
-
@processing_thread.kill
|
252
|
-
_send_frame(:close) if
|
274
|
+
@processing_thread.kill if kill_processing_thread
|
275
|
+
_send_frame(:close) if send_close
|
253
276
|
@socket.close
|
254
277
|
@is_open = false
|
255
278
|
|
data/lib/waithook.rb
CHANGED
@@ -118,7 +118,11 @@ class Waithook
|
|
118
118
|
client.ping_sender
|
119
119
|
logger.debug("Exit ping sender thread")
|
120
120
|
rescue => error
|
121
|
-
|
121
|
+
if error.message == "closed stream" && !@client.socket_open?
|
122
|
+
logger.debug("Connection closed, stopping ping sender thread")
|
123
|
+
else
|
124
|
+
logger.warn("Error in ping sender thread: #{error.message} (#{error.class})\n#{error.backtrace.join("\n")}")
|
125
|
+
end
|
122
126
|
end
|
123
127
|
end
|
124
128
|
end
|
data/tests/waithook_test.rb
CHANGED
@@ -96,4 +96,20 @@ describe "Waithook" do
|
|
96
96
|
|
97
97
|
assert_includes(out, "Timeout::Error: execution expired")
|
98
98
|
end
|
99
|
+
|
100
|
+
it "should throw exception when got socket error" do
|
101
|
+
waithook = default_client(logger: true)
|
102
|
+
socket = waithook.client.instance_variable_get(:@socket)
|
103
|
+
socket.close
|
104
|
+
|
105
|
+
captured_error = nil
|
106
|
+
begin
|
107
|
+
waithook.wait_message(raise_on_timeout: false)
|
108
|
+
rescue => error
|
109
|
+
captured_error = error
|
110
|
+
end
|
111
|
+
|
112
|
+
assert_instance_of(IOError, captured_error)
|
113
|
+
assert_equal("stream closed in another thread", captured_error.message)
|
114
|
+
end
|
99
115
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: waithook
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pavel Evstigneev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-10-
|
11
|
+
date: 2024-10-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: websocket
|