td-client 0.8.63 → 0.8.64

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MDBjNmRmZmY5MjZkMGIxY2ZmZjEzMzU3NjhlNDMxYTBhMDc0YzUxMw==
4
+ ZWZhMDhjOTJhMDg4YWViMWMxYjQ5ZTVlYmM1MTBmNGMxYTNlNzA5Nw==
5
5
  data.tar.gz: !binary |-
6
- MjE5ZTgwOTI1OGM4NjEyODdjMDNlYmM5NmQ4NGU5ZTVlYmNkMDMwYg==
6
+ NmIwNjQ1OWRmNDI1MGIyNmQ3NmIzZjc5MzEzNjg2MmRiM2M3MDVmMw==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZDZmNjY2OTkyODExOWUwYmMyNDE0ZGEwMmU5MzU4OTkzMmYzZTMwMzY3MzEw
10
- ZjIxZjMyYTkxZDlmYWFjODEwODA3ZmU0MDc0NjE1NzQzMDkxMWViYTQxZWE5
11
- YzRlYzRlOGYwYmYzZTlmNDgzYzQ4YmUzYzAxZDVjYjY4N2VjYzk=
9
+ ZDBjMjA0MzkyMzMxMzBlOWI4MGE4MTJhYTc3ZGIwZTUzYjdmYWIyOWE5Y2Y2
10
+ Mzk0MTA3MmFhZWFiZGE5N2E3ZDhjY2M2ZWJjYjY3ZDg0YjY1Yzc4MWNhYzEy
11
+ NTUyYzU0MzIzMTI3YWNhM2UwZmVmZjVkMTFlNWNjYmM4OGRiYzA=
12
12
  data.tar.gz: !binary |-
13
- Zjk1ZDViZGZkOGI2YmE4ZGM2N2ZjMzFkNmNhMDNmMzFkZDY1YzAwYTU1ZmU5
14
- MjUwYjRlNjhkYmM5ZmQwZWNmYjM4YmExZDllNzhlZWMwNjZjMjhiZTk5YjVl
15
- NzA1NTkyM2FkZjk3ZGQ4ZjRmOWYzNmFmZTMwMTgwM2NlN2YzNGQ=
13
+ ODJlY2Y0ODAzNjA4OGJkOWQxYTc0MmVlMWI1YWIxOGNkMGNjZjgyNzQ3ZGEw
14
+ ZGQ2NzE2ZDk3YTZjYTUyMGQ4MDRhYzNiNmI0NmIyMzQ4OWJjZTQ3MjQ0M2Zm
15
+ Y2QwNGZlNTMyYzEzNDM0NmVmMGRmZWIyMmFkNWM5MDY2ZjBiYjU=
@@ -53,6 +53,7 @@ class API
53
53
  @connect_timeout = opts[:connect_timeout] || 60
54
54
  @read_timeout = opts[:read_timeout] || 600
55
55
  @send_timeout = opts[:send_timeout] || 600
56
+ @retry_post_requests = opts[:retry_post_requests] || false
56
57
 
57
58
  case uri.scheme
58
59
  when 'http', 'https'
@@ -1235,17 +1236,57 @@ class API
1235
1236
  puts "DEBUG: params: " + params.to_s
1236
1237
  end
1237
1238
 
1238
- if block
1239
- response = http.request(request) {|res|
1240
- block.call(res)
1241
- }
1242
- else
1243
- response = http.request(request)
1244
- end
1239
+ # up to 7 retries with exponential (base 2) back-off starting at 'retry_delay'
1240
+ retry_delay = 5
1241
+ max_cumul_retry_delay = 600
1242
+ cumul_retry_delay = 0
1243
+
1244
+ # for both exceptions and 500+ errors retrying is enabled by default.
1245
+ # The total number of retries cumulatively should not exceed 10 minutes / 600 seconds
1246
+ response = nil
1247
+ begin # this block is to allow retry (redo) in the begin part of the begin-rescue block
1248
+ begin
1249
+ if block
1250
+ response = http.request(request) {|res|
1251
+ block.call(res)
1252
+ }
1253
+ else
1254
+ response = http.request(request)
1255
+ end
1256
+
1257
+ status = response.code.to_i
1258
+ # retry if the HTTP error code is 500 or higher and we did not run out of retrying attempts
1259
+ if !block_given? && status >= 500 && cumul_retry_delay <= max_cumul_retry_delay
1260
+ $stderr.puts "Error #{status}: #{get_error(response)}. Retrying after #{retry_delay} seconds..."
1261
+ sleep retry_delay
1262
+ cumul_retry_delay += retry_delay
1263
+ retry_delay *= 2
1264
+ redo # restart from beginning of do-while loop
1265
+ end
1266
+ rescue Errno::ECONNREFUSED, Errno::ECONNRESET, Timeout::Error, EOFError, OpenSSL::SSL::SSLError, SocketError => e
1267
+ if block_given?
1268
+ raise e
1269
+ end
1270
+ $stderr.print "#{e.class}: #{e.message}. "
1271
+ if cumul_retry_delay <= max_cumul_retry_delay
1272
+ $stderr.puts "Retrying after #{retry_delay} seconds..."
1273
+ sleep retry_delay
1274
+ cumul_retry_delay += retry_delay
1275
+ retry_delay *= 2
1276
+ retry
1277
+ else
1278
+ $stderr.puts "Retrying stopped after #{max_cumul_retry_delay} seconds."
1279
+ raise e
1280
+ end
1281
+ rescue => e
1282
+ raise e
1283
+ end
1284
+ end while false
1245
1285
 
1246
1286
  unless ENV['TD_CLIENT_DEBUG'].nil?
1247
1287
  puts "DEBUG: REST GET response:"
1248
1288
  puts "DEBUG: header: " + response.header.to_s
1289
+ puts "DEBUG: status: " + response.code.to_s
1249
1290
  puts "DEBUG: body: " + response.body.to_s
1250
1291
  end
1251
1292
 
@@ -1289,7 +1330,53 @@ class API
1289
1330
  request = Net::HTTP::Post.new(path, header)
1290
1331
  end
1291
1332
 
1292
- response = http.request(request)
1333
+ # up to 7 retries with exponential (base 2) back-off starting at 'retry_delay'
1334
+ retry_delay = 5
1335
+ max_cumul_retry_delay = 600
1336
+ cumul_retry_delay = 0
1337
+
1338
+ # for both exceptions and 500+ errors retrying can be enabled by initialization
1339
+ # parameter 'retry_post_requests'. The total number of retries cumulatively
1340
+ # should not exceed 10 minutes / 600 seconds
1341
+ response = nil
1342
+ begin # this block is to allow retry (redo) in the begin part of the begin-rescue block
1343
+ begin
1344
+ response = http.request(request)
1345
+
1346
+ # if the HTTP error code is 500 or higher and the user requested retrying
1347
+ # on post request, attempt a retry
1348
+ status = response.code.to_i
1349
+ if @retry_post_requests && status >= 500 && cumul_retry_delay <= max_cumul_retry_delay
1350
+ $stderr.puts "Error #{status}: #{get_error(response)}. Retrying after #{retry_delay} seconds..."
1351
+ sleep retry_delay
1352
+ cumul_retry_delay += retry_delay
1353
+ retry_delay *= 2
1354
+ redo # restart from beginning of do-while loop
1355
+ end
1356
+ rescue Errno::ECONNREFUSED, Errno::ECONNRESET, Timeout::Error, EOFError, OpenSSL::SSL::SSLError, SocketError => e
1357
+ $stderr.print "#{e.class}: #{e.message}. "
1358
+ if @retry_post_requests && cumul_retry_delay <= max_cumul_retry_delay
1359
+ $stderr.puts "Retrying after #{retry_delay} seconds..."
1360
+ sleep retry_delay
1361
+ cumul_retry_delay += retry_delay
1362
+ retry_delay *= 2
1363
+ retry
1364
+ else
1365
+ $stderr.puts "Retrying stopped after #{max_cumul_retry_delay} seconds."
1366
+ raise e
1367
+ end
1368
+ rescue => e
1369
+ raise e
1370
+ end
1371
+ end while false
1372
+
1373
+ unless ENV['TD_CLIENT_DEBUG'].nil?
1374
+ puts "DEBUG: REST POST response:"
1375
+ puts "DEBUG: header: " + response.header.to_s
1376
+ puts "DEBUG: status: " + response.code.to_s
1377
+ puts "DEBUG: body: <omitted>"
1378
+ end
1379
+
1293
1380
  return [response.code, response.body, response]
1294
1381
  end
1295
1382
 
@@ -1316,6 +1403,14 @@ class API
1316
1403
  end
1317
1404
 
1318
1405
  response = client.put(target, body, header)
1406
+
1407
+ unless ENV['TD_CLIENT_DEBUG'].nil?
1408
+ puts "DEBUG: REST PUT response:"
1409
+ puts "DEBUG: header: " + response.header.to_s
1410
+ puts "DEBUG: status: " + response.code.to_s
1411
+ puts "DEBUG: body: <omitted>"
1412
+ end
1413
+
1319
1414
  return [response.code.to_s, response.body, response]
1320
1415
  end
1321
1416
 
@@ -1372,48 +1467,39 @@ class API
1372
1467
  @ssl_ca_file ||= File.join(File.dirname(__FILE__), '..', '..', '..', 'data', 'ca-bundle.crt')
1373
1468
  end
1374
1469
 
1375
- def raise_error(msg, res, klass=nil)
1376
- status_code = res.code.to_s
1470
+ def get_error(res)
1377
1471
  begin
1378
1472
  js = JSON.load(res.body)
1379
1473
  if js.nil?
1380
1474
  error_msg = if res.respond_to?(:message)
1381
1475
  res.message # Net::HTTP
1382
1476
  else
1383
- res.reason # httpclient
1477
+ res.reason # HttpClient
1384
1478
  end
1385
1479
  else
1386
1480
  error_msg = js['message'] || js['error']
1387
1481
  end
1388
-
1389
- if klass
1390
- raise klass, "#{status_code}: #{msg}: #{error_msg}"
1391
- elsif status_code == "404"
1392
- raise NotFoundError, "#{msg}: #{error_msg}"
1393
- elsif status_code == "409"
1394
- raise AlreadyExistsError, "#{msg}: #{error_msg}"
1395
- elsif status_code == "401"
1396
- raise AuthError, "#{msg}: #{error_msg}"
1397
- elsif status_code == "403"
1398
- raise ForbiddenError, "#{msg}: #{error_msg}"
1399
- else
1400
- raise APIError, "#{status_code}: #{msg}: #{error_msg}"
1401
- end
1402
-
1403
1482
  rescue JSON::ParserError
1404
- if klass
1405
- raise klass, "#{status_code}: #{msg}: #{res.body}"
1406
- elsif status_code == "404"
1407
- raise NotFoundError, "#{msg}: #{res.body}"
1408
- elsif status_code == "409"
1409
- raise AlreadyExistsError, "#{msg}: #{res.body}"
1410
- elsif status_code == "401"
1411
- raise AuthError, "#{msg}: #{res.body}"
1412
- elsif status_code == "403"
1413
- raise ForbiddenError, "#{msg}: #{res.body}"
1414
- else
1415
- raise APIError, "#{status_code}: #{msg}: #{res.body}"
1416
- end
1483
+ error_msg = res.body
1484
+ end
1485
+ error_msg
1486
+ end
1487
+
1488
+ def raise_error(msg, res, klass=nil)
1489
+ status_code = res.code.to_s
1490
+ error_msg = get_error(res)
1491
+ if klass
1492
+ raise klass, "#{status_code}: #{msg}: #{res.body}"
1493
+ elsif status_code == "404"
1494
+ raise NotFoundError, "#{msg}: #{res.body}"
1495
+ elsif status_code == "409"
1496
+ raise AlreadyExistsError, "#{msg}: #{res.body}"
1497
+ elsif status_code == "401"
1498
+ raise AuthError, "#{msg}: #{res.body}"
1499
+ elsif status_code == "403"
1500
+ raise ForbiddenError, "#{msg}: #{res.body}"
1501
+ else
1502
+ raise APIError, "#{status_code}: #{msg}: #{res.body}"
1417
1503
  end
1418
1504
  # TODO error
1419
1505
  end
@@ -1,5 +1,5 @@
1
1
  module TreasureData
2
2
  class Client
3
- VERSION = '0.8.63'
3
+ VERSION = '0.8.64'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: td-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.63
4
+ version: 0.8.64
5
5
  platform: ruby
6
6
  authors:
7
7
  - Treasure Data, Inc.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-01 00:00:00.000000000 Z
11
+ date: 2014-09-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: msgpack