@@ -57,6 +57,9 @@ def initialize(tag_prefix = nil, *args)
57
57
@host = options [ :host ]
58
58
@port = options [ :port ]
59
59
60
+ @require_ack_response = options [ :require_ack_response ]
61
+ @ack_response_timeout = options [ :ack_response_timeout ] || 190
62
+
60
63
@mon = Monitor . new
61
64
@pending = nil
62
65
@connect_error_history = [ ]
@@ -108,7 +111,7 @@ def close
108
111
if @pending
109
112
begin
110
113
@pending . each do |tag , record |
111
- send_data ( [ tag , record ] . to_msgpack )
114
+ send_data ( tag , record )
112
115
end
113
116
rescue => e
114
117
set_last_error ( e )
@@ -171,7 +174,7 @@ def write(tag, time, map)
171
174
172
175
begin
173
176
@pending . each do |tag , record |
174
- send_data ( [ tag , record ] . to_msgpack )
177
+ send_data ( tag , record )
175
178
end
176
179
@pending = nil
177
180
true
@@ -189,11 +192,17 @@ def write(tag, time, map)
189
192
}
190
193
end
191
194
192
- def send_data ( data )
195
+ def send_data ( tag , record )
193
196
unless connect?
194
197
connect!
195
198
end
196
- @con . write data
199
+ if @require_ack_response
200
+ option = { }
201
+ option [ 'chunk' ] = generate_chunk
202
+ @con . write [ tag , record , option ] . to_msgpack
203
+ else
204
+ @con . write [ tag , record ] . to_msgpack
205
+ end
197
206
#while true
198
207
# puts "sending #{data.length} bytes"
199
208
# if data.length > 32*1024
@@ -208,6 +217,21 @@ def send_data(data)
208
217
# data = data[n..-1]
209
218
#end
210
219
220
+ if @require_ack_response && @ack_response_timeout > 0
221
+ if IO . select ( [ @con ] , nil , nil , @ack_response_timeout )
222
+ raw_data = @con . recv ( 1024 )
223
+
224
+ if raw_data . empty?
225
+ raise "Closed connection"
226
+ else
227
+ response = MessagePack . unpack ( raw_data )
228
+ if response [ 'ack' ] != option [ 'chunk' ]
229
+ raise "ack in response and chunk id in sent data are different"
230
+ end
231
+ end
232
+ end
233
+ end
234
+
211
235
true
212
236
end
213
237
@@ -246,6 +270,10 @@ def set_last_error(e)
246
270
# TODO: Check non GVL env
247
271
@last_error [ Thread . current . object_id ] = e
248
272
end
273
+
274
+ def generate_chunk
275
+ Base64 . encode64 ( ( [ SecureRandom . random_number ( 1 << 32 ) ] * 4 ) . pack ( 'NNNN' ) ) . chomp
276
+ end
249
277
end
250
278
end
251
279
end
0 commit comments