taba-client 0.3.20.rc1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,226 @@
1
+ require 'taba/error'
2
+ require 'taba/json_fields'
3
+
4
+ module Taba
5
+ # The models of the client API and the reading of its JSON responses.
6
+
7
+ # What +create_or_update_topic+ did and the topic as it is now.
8
+ class TopicPut
9
+ # It did not exist (+201+).
10
+ CREATED = 'created'.freeze
11
+ # It existed: the mutable fields are now the declared ones, or already were (+200+).
12
+ UPDATED = 'updated'.freeze
13
+ # It was removed and created again with a new +id+ (+201+ with +Taba-Topic-Recreated: true+).
14
+ RECREATED = 'recreated'.freeze
15
+
16
+ # +topic+ (a Taba::Topic) and +outcome+ (+created+, +updated+ or +recreated+).
17
+ attr_reader :topic, :outcome
18
+
19
+ def initialize(topic, outcome)
20
+ @topic, @outcome = topic, outcome
21
+ end
22
+ end
23
+
24
+ class Topic
25
+ attr_reader :id, :name, :type, :created_at, :fifo, :volatile, :raw
26
+ # id, name, type (+queue+ or +distributor+), created_at (RFC 3339 text), fifo, volatile, and raw (the topic
27
+ # JSON, as the server returned it).
28
+ def initialize(id, name, type, created_at, fifo, volatile, raw)
29
+ @id, @name, @type, @created_at, @fifo, @volatile, @raw = id, name, type, created_at, fifo, volatile, raw
30
+ end
31
+ end
32
+
33
+ class Page
34
+ attr_reader :items, :next_cursor, :raw
35
+ # The topics of the page; next_cursor is the cursor of the next page, or nil on the last one.
36
+ def initialize(items, next_cursor, raw)
37
+ @items, @next_cursor, @raw = items, next_cursor, raw
38
+ end
39
+ end
40
+
41
+ # A delivered message. The +body+ always comes out as bytes (doc 21, "The delivered message"): for JSON, the JSON
42
+ # text as it came; for +text/*+, the UTF-8 text; for base64, the bytes already decoded (a binary String).
43
+ class Message
44
+ attr_reader :id, :body, :content_type, :receipt_token, :delivery_count, :published_at, :invisible_until,
45
+ :group_id, :deduplication_id, :dead_lettered_at, :dead_letter_reason, :raw
46
+
47
+ def initialize(attributes)
48
+ attributes.each { |name, value| instance_variable_set("@#{name}", value) }
49
+ end
50
+
51
+ # The +body+ as UTF-8 text.
52
+ def body_text
53
+ @body.dup.force_encoding(Encoding::UTF_8)
54
+ end
55
+
56
+ # The +body+ read as JSON.
57
+ def json
58
+ ::JSON.parse(body_text, quirks_mode: true)
59
+ rescue ::JSON::ParserError => e
60
+ raise Error.client('client.invalid-response', "body of message #{@id} is not valid JSON: #{e.message}")
61
+ end
62
+ end
63
+
64
+ # The copy of a publication made by a distributor to a queue.
65
+ class DistributorDelivery
66
+ attr_reader :topic, :id, :error_code, :error_message
67
+
68
+ def initialize(topic, id, error_code, error_message)
69
+ @topic, @id, @error_code, @error_message = topic, id, error_code, error_message
70
+ end
71
+
72
+ def succeeded?
73
+ @error_code.nil?
74
+ end
75
+ end
76
+
77
+ class PublishResponse
78
+ # id (queue; nil in a distributor's response); duplicate (FIFO queue only: true when it repeated a publication
79
+ # within the deduplication window); matched (distributor only: how many queues matched); deliveries.
80
+ attr_reader :id, :duplicate, :matched, :deliveries, :raw
81
+
82
+ def initialize(id, duplicate, matched, deliveries, raw)
83
+ @id, @duplicate, @matched, @deliveries, @raw = id, duplicate, matched, deliveries, raw
84
+ end
85
+ end
86
+
87
+ class VisibilityResponse
88
+ attr_reader :id, :invisible_until, :raw
89
+
90
+ def initialize(id, invisible_until, raw)
91
+ @id, @invisible_until, @raw = id, invisible_until, raw
92
+ end
93
+ end
94
+
95
+ # The result of one item of a batch, in the order of the request: the +status+ the single call would have
96
+ # answered and what it would have returned. An item that failed does not raise: +ok?+ is false.
97
+ class BatchResult
98
+ # status: 201 published, 200 a FIFO duplicate, a distributor or a new invisibility, 204 acked, or the error's.
99
+ attr_reader :status, :id, :duplicate, :matched, :invisible_until, :error_code, :error_message, :raw
100
+
101
+ def initialize(status, id, duplicate, matched, invisible_until, error_code, error_message, raw)
102
+ @status, @id, @duplicate, @matched, @invisible_until = status, id, duplicate, matched, invisible_until
103
+ @error_code, @error_message, @raw = error_code, error_message, raw
104
+ end
105
+
106
+ def ok?
107
+ @error_code.nil? && @status < 300
108
+ end
109
+ end
110
+
111
+ # Reading the responses.
112
+ module Parser
113
+ module_function
114
+
115
+ def invalid(what, error)
116
+ Error.client('client.invalid-response', "#{what}: #{error.message}")
117
+ end
118
+
119
+ def fields(raw, what)
120
+ JsonFields.parse(raw)
121
+ rescue JsonError => e
122
+ raise invalid(what, e)
123
+ end
124
+
125
+ def json_type?(content_type)
126
+ media = content_type.split(';').first.to_s.strip.downcase
127
+ media == 'application/json' || media.end_with?('+json')
128
+ end
129
+
130
+ def topic(raw)
131
+ f = fields(raw, 'topic')
132
+ Topic.new(f.string('id') || '', f.string('name') || '', f.string('type') || '', f.string('createdAt') || '',
133
+ f.boolean('fifo'), f.boolean('volatile'), raw)
134
+ rescue JsonError => e
135
+ raise invalid('topic', e)
136
+ end
137
+
138
+ def message(raw)
139
+ f = fields(raw, 'message')
140
+ content_type = f.string('contentType') || 'application/json'
141
+ body = f.raw('body')
142
+ if body.nil?
143
+ data = ''.force_encoding(Encoding::BINARY)
144
+ elsif f.string('bodyEncoding') == 'base64'
145
+ text = JsonText.decode_string(body)
146
+ begin
147
+ data = text.unpack('m0').first
148
+ rescue ArgumentError
149
+ raise Error.client('client.invalid-response', 'invalid base64 body')
150
+ end
151
+ elsif json_type?(content_type)
152
+ data = body.dup
153
+ else
154
+ data = JsonText.decode_string(body)
155
+ end
156
+ Message.new(
157
+ id: f.string('id') || '', body: data, content_type: content_type, receipt_token: f.string('receiptToken') || '',
158
+ delivery_count: f.integer('deliveryCount') || 0, published_at: f.string('publishedAt') || '',
159
+ invisible_until: f.string('invisibleUntil') || '', group_id: f.string('groupId'),
160
+ deduplication_id: f.string('deduplicationId'), dead_lettered_at: f.string('deadLetteredAt'),
161
+ dead_letter_reason: f.string('deadLetterReason'), raw: raw
162
+ )
163
+ rescue JsonError => e
164
+ raise invalid('message', e)
165
+ end
166
+
167
+ def messages(raw)
168
+ f = fields(raw, 'consume')
169
+ listing = f.raw('messages')
170
+ (listing.nil? ? [] : JsonText.items(listing)).map { |item| message(item) }
171
+ rescue JsonError => e
172
+ raise invalid('messages', e)
173
+ end
174
+
175
+ def page(raw)
176
+ f = fields(raw, 'page')
177
+ listing = f.raw('items')
178
+ items = (listing.nil? ? [] : JsonText.items(listing)).map { |item| topic(item) }
179
+ Page.new(items, f.string('nextCursor'), raw)
180
+ rescue JsonError => e
181
+ raise invalid('page items', e)
182
+ end
183
+
184
+ # The +error+ object of a delivery or a batch item: [code, message], or nil when there is none.
185
+ def error_of(f)
186
+ error = f.raw('error')
187
+ return nil if error.nil? || !error.start_with?('{')
188
+ e = JsonFields.parse(error)
189
+ [e.string('code') || '', e.string('message')]
190
+ end
191
+
192
+ def publish_response(raw)
193
+ f = fields(raw, 'publication')
194
+ listing = f.raw('deliveries')
195
+ deliveries = (listing.nil? ? [] : JsonText.items(listing)).map do |item|
196
+ d = JsonFields.parse(item)
197
+ error = error_of(d)
198
+ DistributorDelivery.new(d.string('topic') || '', d.string('id'), error && error[0], error && error[1])
199
+ end
200
+ PublishResponse.new(f.string('id'), f.boolean('duplicate'), f.integer('matched'), deliveries, raw)
201
+ rescue JsonError => e
202
+ raise invalid('publication', e)
203
+ end
204
+
205
+ # The +results+ of a batch, one per item in the order of the request.
206
+ def batch_results(raw)
207
+ f = fields(raw, 'batch')
208
+ listing = f.raw('results')
209
+ (listing.nil? ? [] : JsonText.items(listing)).map do |item|
210
+ r = JsonFields.parse(item)
211
+ error = error_of(r)
212
+ BatchResult.new(r.integer('status') || 0, r.string('id'), r.boolean('duplicate'), r.integer('matched'),
213
+ r.string('invisibleUntil'), error && error[0], error && error[1], item)
214
+ end
215
+ rescue JsonError => e
216
+ raise invalid('batch results', e)
217
+ end
218
+
219
+ def visibility(raw)
220
+ f = fields(raw, 'visibility')
221
+ VisibilityResponse.new(f.string('id') || '', f.string('invisibleUntil') || '', raw)
222
+ rescue JsonError => e
223
+ raise invalid('visibility', e)
224
+ end
225
+ end
226
+ end
@@ -0,0 +1,103 @@
1
+ require 'taba/error'
2
+
3
+ module Taba
4
+ # Base of the request objects: +new(field: value, ...)+. An unknown field is a +client.invalid-request+ error, so a
5
+ # typo does not go unnoticed.
6
+ class Fields
7
+ def self.fields(*names)
8
+ @names = names
9
+ attr_accessor(*names)
10
+ end
11
+
12
+ def self.names
13
+ @names || []
14
+ end
15
+
16
+ def self.default(_name)
17
+ nil
18
+ end
19
+
20
+ def initialize(values = {})
21
+ self.class.names.each { |name| instance_variable_set("@#{name}", self.class.default(name)) }
22
+ values.each do |key, value|
23
+ unless self.class.names.include?(key.to_sym)
24
+ raise Error.client('client.invalid-request', "unknown field of #{self.class.name}: #{key}")
25
+ end
26
+ send("#{key}=", value)
27
+ end
28
+ end
29
+
30
+ # From an instance of the class or from a hash of fields (a copy: the caller's object is never changed).
31
+ def self.from(value)
32
+ return value.dup if value.is_a?(self)
33
+ new(value || {})
34
+ end
35
+ end
36
+
37
+ # +consume+ options. The connector sends only what was set: whatever is nil takes the server default.
38
+ #
39
+ # max_messages; visibility_timeout (seconds of invisibility); wait_time (seconds of long polling); auto_create;
40
+ # volatile, idle_timeout, fifo and tags (only with auto_create); request_id; traceparent.
41
+ class ConsumeOptions < Fields
42
+ fields :max_messages, :visibility_timeout, :wait_time, :auto_create, :volatile, :idle_timeout, :fifo, :tags,
43
+ :request_id, :traceparent
44
+
45
+ def self.default(name)
46
+ name == :auto_create ? false : nil
47
+ end
48
+ end
49
+
50
+ # The options of +publish+: content_type (default application/json; +publish_text+ and +publish_json+ set their
51
+ # own); group_id; deduplication_id; request_id; traceparent; fifo (true when the topic is a FIFO queue: only there
52
+ # can a 502/504 be repeated).
53
+ class PublishOptions < Fields
54
+ fields :content_type, :group_id, :deduplication_id, :request_id, :traceparent, :fifo
55
+
56
+ def self.default(name)
57
+ name == :fifo ? false : nil
58
+ end
59
+ end
60
+
61
+ # +POST /topics+. Only what was filled in is sent. +type+ is +queue+ or +distributor+; +prefixes+ (distributor
62
+ # only) are the name prefixes of the queues each publication is copied to.
63
+ class TopicRequest < Fields
64
+ fields :name, :type, :tags, :prefixes, :volatile, :idle_timeout, :fifo, :max_message_bytes, :ttl, :max_deliveries
65
+ end
66
+
67
+ # +PATCH /topics/{topic}+: only what was given is sent. For +max_message_bytes+, +ttl+ and +max_deliveries+ a key
68
+ # present with +nil+ sends +null+ (back to the server default); a key left out is not sent.
69
+ #
70
+ # Taba::TopicUpdate.new(ttl: nil, max_deliveries: 5)
71
+ class TopicUpdate
72
+ KNOWN = [:tags, :prefixes, :idle_timeout, :max_message_bytes, :ttl, :max_deliveries].freeze
73
+ NULLABLE = [:max_message_bytes, :ttl, :max_deliveries].freeze
74
+
75
+ def initialize(values = {})
76
+ @given = {}
77
+ values.each do |key, value|
78
+ unless KNOWN.include?(key.to_sym)
79
+ raise Error.client('client.invalid-request', "unknown field of Taba::TopicUpdate: #{key}")
80
+ end
81
+ @given[key.to_sym] = value
82
+ end
83
+ end
84
+
85
+ def self.from(value)
86
+ return value if value.is_a?(TopicUpdate)
87
+ new(value || {})
88
+ end
89
+
90
+ # The fields to send, name => value.
91
+ attr_reader :given
92
+ end
93
+
94
+ # One message of +publish_batch+: the same fields as +publish+. The bytes travel in base64.
95
+ class BatchMessage < Fields
96
+ fields :body, :content_type, :group_id, :deduplication_id
97
+
98
+ def initialize(body = '', values = {})
99
+ super(values)
100
+ @body = body
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,347 @@
1
+ require 'net/http'
2
+ require 'openssl'
3
+ require 'uri'
4
+ require 'taba/error'
5
+ require 'taba/cancel_token'
6
+ require 'taba/config'
7
+ require 'taba/json_fields'
8
+
9
+ module Taba
10
+ # The description of a request to the API: method, path, query, body, headers and the rules of the call (timeout,
11
+ # retry of 502/504, responses that count as success after a 503).
12
+ class ApiCall
13
+ attr_accessor :method, :path, :query, :body, :content_type, :request_id, :traceparent
14
+ # Overrides the total time of the call in seconds (+consume+ uses +wait_time + 10+).
15
+ attr_accessor :timeout
16
+ # Retries 502 and 504: only publishing to a FIFO queue, where the primary deduplicates.
17
+ attr_accessor :retry_gateway
18
+ # After a 503, these [status, code] count as success (the operation had already been applied).
19
+ attr_accessor :recover
20
+
21
+ # +path+ has the segments already encoded (+/topics/orders+).
22
+ def initialize(method, path)
23
+ @method = method
24
+ @path = path
25
+ @query = []
26
+ @body = nil
27
+ @recover = []
28
+ @retry_gateway = false
29
+ end
30
+
31
+ def with_query(key, value)
32
+ @query << [key, value.to_s]
33
+ self
34
+ end
35
+
36
+ def with_json(text)
37
+ @body = text
38
+ self
39
+ end
40
+ end
41
+
42
+ # +recreated+: the response carries +Taba-Topic-Recreated: true+ (+PUT /topics/{topic}+).
43
+ ApiReply = Struct.new(:status, :body, :recovered, :recreated)
44
+
45
+ # The client's HTTP transport: builds the request, sends it, reads the body, turns the API error into a
46
+ # Taba::Error and does the call retries of the common contract (doc 21).
47
+ class Transport
48
+ # Net::HTTP repeats an idempotent request (GET, PUT, DELETE) once by itself after a timeout or a reset. The
49
+ # contract says when to repeat (a timeout in +consume+ is never safe to repeat), so the requests report a method
50
+ # Net::HTTP does not know as idempotent; the line sent on the wire is still the real verb.
51
+ module NoAutomaticRetry
52
+ def method
53
+ "TABA-#{@method}"
54
+ end
55
+ end
56
+
57
+ REQUESTS = {
58
+ 'GET' => Net::HTTP::Get, 'POST' => Net::HTTP::Post, 'PUT' => Net::HTTP::Put,
59
+ 'DELETE' => Net::HTTP::Delete, 'PATCH' => Net::HTTP::Patch
60
+ }.each_with_object({}) do |(verb, base), out|
61
+ out[verb] = Class.new(base) { include NoAutomaticRetry }
62
+ end.freeze
63
+
64
+ CONNECT_ERRORS = [Errno::ECONNREFUSED, SocketError, Errno::ENETUNREACH, Errno::EHOSTUNREACH].freeze
65
+ STALE_ERRORS = [EOFError, Errno::ECONNRESET, Errno::EPIPE].freeze
66
+
67
+ # A failure before the body was sent (refused, DNS): the call can be repeated like a 503.
68
+ class Retry < StandardError
69
+ attr_reader :error
70
+
71
+ def initialize(error)
72
+ super(error.message)
73
+ @error = error
74
+ end
75
+ end
76
+
77
+ attr_reader :config
78
+ # The wait between attempts: +sleep(seconds, token)+ raises Cancelled. Tests replace it.
79
+ attr_accessor :sleeper
80
+
81
+ def self.segment(value)
82
+ URI.encode_www_form_component(value.to_s).gsub('+', '%20')
83
+ end
84
+
85
+ def initialize(config)
86
+ @config = config
87
+ @scheme, @host, @port, @prefix = validate(config.base_url)
88
+ @key = "taba-connection-#{object_id}"
89
+ @sleeper = lambda do |seconds, token|
90
+ raise Cancelled if token.wait(seconds)
91
+ end
92
+ end
93
+
94
+ # Closes the calling thread's connection.
95
+ def close
96
+ drop
97
+ end
98
+
99
+ # Makes the call, retrying only what the contract says to retry: 429 and 503 (waiting for the +Retry-After+, or
100
+ # 1 s, 2 s, 4 s), 502 and 504 only with +retry_gateway+, and a failure to connect (refused, DNS). Never a 4xx
101
+ # (except 429), nor a failure after the body was sent, nor a timeout.
102
+ def send_call(call, cancel = nil)
103
+ cancellable = !cancel.nil?
104
+ cancel ||= CancelToken.new
105
+ timeout = call.timeout || @config.request_timeout
106
+ maximum = [1, @config.max_attempts].max
107
+ after_retryable = false
108
+ attempt = 0
109
+ loop do
110
+ attempt += 1
111
+ backoff = (1 << [attempt - 1, 6].min).to_f
112
+ begin
113
+ status, headers, body = once(call, timeout, cancel, cancellable)
114
+ if status < 400
115
+ recreated = headers['taba-topic-recreated'].to_s.strip.downcase == 'true'
116
+ return ApiReply.new(status, body, false, recreated)
117
+ end
118
+ if after_retryable && call.recover.any? { |s, c| s == status && c == error_code_of(body) }
119
+ return ApiReply.new(status, body, true)
120
+ end
121
+ error = error_from(status, headers, body)
122
+ retry_it = status == 429 || status == 503 || ([502, 504].include?(status) && call.retry_gateway)
123
+ raise error if !retry_it || attempt >= maximum
124
+ after_retryable ||= status == 503
125
+ wait = error.retry_after.nil? ? backoff : error.retry_after.to_f
126
+ rescue Retry => r
127
+ raise r.error if attempt >= maximum
128
+ after_retryable = true
129
+ wait = backoff
130
+ end
131
+ @sleeper.call(wait, cancel)
132
+ end
133
+ end
134
+
135
+ private
136
+
137
+ def validate(base_url)
138
+ uri = nil
139
+ if base_url.is_a?(String) && !base_url.strip.empty?
140
+ begin
141
+ uri = URI.parse(base_url.strip)
142
+ rescue URI::InvalidURIError
143
+ uri = nil
144
+ end
145
+ end
146
+ unless uri && %w(http https).include?(uri.scheme) && uri.host && !uri.host.empty?
147
+ shown = base_url.is_a?(String) && !base_url.strip.empty? ? base_url : 'empty'
148
+ raise Error.client('client.invalid-config', "base_url must be an http:// or https:// address (received: #{shown})")
149
+ end
150
+ [uri.scheme, uri.host.sub(/\A\[(.*)\]\z/, '\1'), uri.port, uri.path.sub(%r{/+\z}, '')]
151
+ end
152
+
153
+ def new_connection
154
+ http = Net::HTTP.new(@host, @port)
155
+ http.open_timeout = @config.connect_timeout
156
+ http.keep_alive_timeout = 30 if http.respond_to?(:keep_alive_timeout=)
157
+ http.max_retries = 0 if http.respond_to?(:max_retries=)
158
+ if @scheme == 'https'
159
+ http.use_ssl = true
160
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
161
+ http.ca_file = @config.ca_file if @config.ca_file
162
+ http.ca_path = @config.ca_path if @config.ca_path
163
+ if http.respond_to?(:min_version=)
164
+ http.min_version = :TLS1_2
165
+ else
166
+ http.ssl_version = :TLSv1_2
167
+ end
168
+ end
169
+ http
170
+ end
171
+
172
+ def connection
173
+ Thread.current.thread_variable_get(@key)
174
+ end
175
+
176
+ def drop
177
+ http = connection
178
+ Thread.current.thread_variable_set(@key, nil)
179
+ return if http.nil?
180
+ begin
181
+ http.finish if http.started?
182
+ rescue StandardError, IOError
183
+ nil
184
+ end
185
+ end
186
+
187
+ # The socket of the connection, to close it from another thread when the call is cancelled.
188
+ def abort_connection(http)
189
+ socket = http.instance_variable_get(:@socket)
190
+ socket.io.close if socket
191
+ rescue StandardError, IOError
192
+ nil
193
+ end
194
+
195
+ def url_path(call)
196
+ path = @prefix + call.path
197
+ unless call.query.empty?
198
+ path += '?' + call.query.map { |k, v| "#{Transport.segment(k)}=#{Transport.segment(v)}" }.join('&')
199
+ end
200
+ path
201
+ end
202
+
203
+ def build_request(call)
204
+ request = REQUESTS.fetch(call.method).new(url_path(call))
205
+ request['Accept'] = 'application/json'
206
+ request['Accept-Encoding'] = 'identity'
207
+ request['User-Agent'] = @config.user_agent
208
+ request['Authorization'] = "Bearer #{@config.token}" if @config.token && !@config.token.empty?
209
+ request['X-Request-Id'] = call.request_id if call.request_id
210
+ request['traceparent'] = call.traceparent if call.traceparent
211
+ unless call.body.nil?
212
+ request['Content-Type'] = call.content_type || 'application/json'
213
+ request.body = call.body
214
+ end
215
+ request
216
+ end
217
+
218
+ def now
219
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
220
+ end
221
+
222
+ def describe(error)
223
+ error.message.to_s.empty? ? error.class.name : "#{error.class.name}: #{error.message}"
224
+ end
225
+
226
+ # The exchange itself: connects if needed, sends the request and reads the whole response. +state+ tells the
227
+ # caller whether the request had started to go out when it failed.
228
+ def exchange(http, call, deadline, state)
229
+ unless http.started?
230
+ begin
231
+ http.start
232
+ rescue Net::OpenTimeout
233
+ raise Error.client('client.network', "timed out connecting to #{@host}")
234
+ end
235
+ end
236
+ remaining = deadline - now
237
+ raise Net::ReadTimeout if remaining <= 0
238
+ http.read_timeout = remaining
239
+ http.write_timeout = remaining if http.respond_to?(:write_timeout=)
240
+ state[:sent] = true
241
+ response = http.request(build_request(call))
242
+ headers = {}
243
+ response.each_header { |name, value| headers[name.downcase] = value }
244
+ [response.code.to_i, headers, response.body.to_s.dup.force_encoding(Encoding::BINARY)]
245
+ end
246
+
247
+ # Runs the exchange in a helper thread, so a cancellation returns at once: closing the socket of the call in
248
+ # flight does not wake a thread blocked in a read on every Ruby version. The helper ends by itself when the
249
+ # socket closes or the read times out.
250
+ def exchange_cancellable(cancel, http, call, deadline, state)
251
+ outcome = Queue.new
252
+ worker = Thread.new do
253
+ begin
254
+ outcome << [:ok, exchange(http, call, deadline, state)]
255
+ rescue Exception => e # rubocop:disable Lint/RescueException - handed to the caller, which raises it
256
+ outcome << [:error, e]
257
+ end
258
+ end
259
+ worker.abort_on_exception = false
260
+ wake = lambda { outcome << [:cancelled] }
261
+ cancel.attach(wake)
262
+ kind, value = outcome.pop
263
+ cancel.detach(wake)
264
+ case kind
265
+ when :ok then value
266
+ when :error then raise value
267
+ else
268
+ abort_connection(http)
269
+ raise Cancelled
270
+ end
271
+ end
272
+
273
+ # One attempt. A stale pooled connection (the server closed it while idle) is tried again once on a new one.
274
+ def once(call, timeout, cancel, cancellable)
275
+ deadline = now + timeout
276
+ [true, false].each do |stale_retry|
277
+ cancel.raise_if_cancelled
278
+ http = connection
279
+ reused = !http.nil? && http.started?
280
+ if http.nil?
281
+ http = new_connection
282
+ Thread.current.thread_variable_set(@key, http)
283
+ end
284
+ state = { sent: false }
285
+ begin
286
+ result = if cancellable
287
+ exchange_cancellable(cancel, http, call, deadline, state)
288
+ else
289
+ exchange(http, call, deadline, state)
290
+ end
291
+ drop if result[1]['connection'].to_s.downcase == 'close'
292
+ return result
293
+ rescue Cancelled
294
+ Thread.current.thread_variable_set(@key, nil) # the helper may still hold this connection
295
+ raise
296
+ rescue Error
297
+ drop
298
+ raise
299
+ rescue StandardError, IOError => e
300
+ drop
301
+ raise Cancelled if cancel.cancelled?
302
+ raise Error.client('client.timeout', 'timed out waiting for the response') if e.is_a?(Timeout::Error)
303
+ error = Error.client('client.network', describe(e))
304
+ raise Retry.new(error) if !state[:sent] && !reused && CONNECT_ERRORS.any? { |k| e.is_a?(k) }
305
+ next if reused && stale_retry && STALE_ERRORS.any? { |k| e.is_a?(k) }
306
+ raise error
307
+ end
308
+ end
309
+ raise 'unreachable'
310
+ end
311
+
312
+ def error_code_of(body)
313
+ JsonFields.parse(utf8(body)).string('code')
314
+ rescue JsonError
315
+ nil
316
+ end
317
+
318
+ def utf8(body)
319
+ body.dup.force_encoding(Encoding::UTF_8)
320
+ end
321
+
322
+ def error_from(status, headers, body)
323
+ text = utf8(body)
324
+ code = message = request_id = details = nil
325
+ begin
326
+ fields = JsonFields.parse(text)
327
+ code = fields.string('code')
328
+ unless code.nil?
329
+ message = fields.string('message') || ''
330
+ request_id = fields.string('requestId')
331
+ details = fields.raw('details')
332
+ end
333
+ rescue JsonError
334
+ nil # Not an ErrorResponse: falls to the generic code below.
335
+ end
336
+ if code.nil?
337
+ code = "http.#{status}"
338
+ trimmed = text.strip
339
+ message = trimmed.empty? ? "response #{status} with no body" : trimmed[0, 300]
340
+ end
341
+ header = headers['retry-after']
342
+ retry_after = header && header.strip =~ /\A[0-9]+\z/ ? header.strip.to_i : nil
343
+ Error.new(status, code, message, request_id: request_id || headers['x-request-id'], retry_after: retry_after,
344
+ details: details, body: body)
345
+ end
346
+ end
347
+ end
@@ -0,0 +1,4 @@
1
+ module Taba
2
+ VERSION = '0.3.20.rc1'.freeze
3
+ CONTRACT_VERSION = '1'.freeze
4
+ end
data/lib/taba.rb ADDED
@@ -0,0 +1,18 @@
1
+ # The Ruby client of Taba (https://github.com/rafaelpontani/taba): the nine operations of the client API,
2
+ # +subscribe+, consuming with a handler and the call retries of the common contract. No dependencies.
3
+ #
4
+ # client = Taba::Client.new('http://localhost:8080', token: token)
5
+ # client.publish_json('orders', order: 9001)
6
+ # client.consume_with('orders', wait_time: 20) { |context| puts context.message.body_text }
7
+ $LOAD_PATH.unshift(File.expand_path('..', __FILE__)) unless $LOAD_PATH.include?(File.expand_path('..', __FILE__))
8
+
9
+ require 'taba/version'
10
+ require 'taba/error'
11
+ require 'taba/cancel_token'
12
+ require 'taba/config'
13
+ require 'taba/json_fields'
14
+ require 'taba/options'
15
+ require 'taba/models'
16
+ require 'taba/transport'
17
+ require 'taba/calls'
18
+ require 'taba/client'