llhttpretrypolicy.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /**
  2. * @file llhttpretrypolicy.cpp
  3. * @brief Implementation of the http retry policy class.
  4. *
  5. * $LicenseInfo:firstyear=2013&license=viewergpl$
  6. *
  7. * Copyright (c) 2013, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "linden_common.h"
  33. // for curl_getdate() (apparently parsing RFC 1123 dates is hard)
  34. #include "curl/curl.h"
  35. #include "llhttpretrypolicy.h"
  36. #include "llhttpconstants.h"
  37. LLAdaptiveRetryPolicy::LLAdaptiveRetryPolicy(F32 min_delay,
  38. F32 max_delay,
  39. F32 backoff_factor,
  40. U32 max_retries,
  41. bool retry_on_4xx)
  42. : mMinDelay(min_delay),
  43. mMaxDelay(max_delay),
  44. mBackoffFactor(backoff_factor),
  45. mMaxRetries(max_retries),
  46. mRetryOn4xx(retry_on_4xx)
  47. {
  48. init();
  49. }
  50. void LLAdaptiveRetryPolicy::init()
  51. {
  52. mDelay = mMinDelay;
  53. mRetryCount = 0;
  54. mShouldRetry = true;
  55. }
  56. void LLAdaptiveRetryPolicy::reset()
  57. {
  58. init();
  59. }
  60. bool LLAdaptiveRetryPolicy::getRetryAfter(const LLSD& headers,
  61. F32& retry_header_time)
  62. {
  63. return headers.has(HTTP_IN_HEADER_RETRY_AFTER) &&
  64. getSecondsUntilRetryAfter(headers[HTTP_IN_HEADER_RETRY_AFTER].asStringRef(),
  65. retry_header_time);
  66. }
  67. bool LLAdaptiveRetryPolicy::getRetryAfter(const LLCore::HttpHeaders::ptr_t& headers,
  68. F32& retry_header_time)
  69. {
  70. if (headers)
  71. {
  72. const std::string* retry_value =
  73. headers->find(HTTP_IN_HEADER_RETRY_AFTER.c_str());
  74. if (retry_value &&
  75. getSecondsUntilRetryAfter(*retry_value, retry_header_time))
  76. {
  77. return true;
  78. }
  79. }
  80. return false;
  81. }
  82. void LLAdaptiveRetryPolicy::onSuccess()
  83. {
  84. init();
  85. }
  86. void LLAdaptiveRetryPolicy::onFailure(S32 status, const LLSD& headers)
  87. {
  88. F32 retry_header_time = 0.f;
  89. bool has_retry_header_time = getRetryAfter(headers, retry_header_time);
  90. onFailureCommon(status, has_retry_header_time, retry_header_time);
  91. }
  92. void LLAdaptiveRetryPolicy::onFailure(const LLCore::HttpResponse* response)
  93. {
  94. if (!response) return;
  95. F32 retry_header_time = 0.f;
  96. const LLCore::HttpHeaders::ptr_t headers = response->getHeaders();
  97. bool has_retry_header_time = getRetryAfter(headers, retry_header_time);
  98. onFailureCommon(response->getStatus().getType(), has_retry_header_time,
  99. retry_header_time);
  100. }
  101. void LLAdaptiveRetryPolicy::onFailureCommon(S32 status,
  102. bool has_retry_header_time,
  103. F32 retry_header_time)
  104. {
  105. if (!mShouldRetry)
  106. {
  107. llinfos << "Keep on failing..." << llendl;
  108. return;
  109. }
  110. if (mRetryCount > 0)
  111. {
  112. mDelay = llclamp(mDelay * mBackoffFactor, mMinDelay, mMaxDelay);
  113. }
  114. // Honor server Retry-After header. Status 503 may ask us to wait for a
  115. // certain amount of time before retrying.
  116. F32 wait_time = mDelay;
  117. if (has_retry_header_time)
  118. {
  119. wait_time = retry_header_time;
  120. }
  121. if (mRetryCount >= mMaxRetries)
  122. {
  123. llwarns << "Too many retries " << mRetryCount << ", aborting."
  124. << llendl;
  125. mShouldRetry = false;
  126. }
  127. if (!mRetryOn4xx && !isHttpServerErrorStatus(status))
  128. {
  129. llwarns << "Non-server error " << status << ", aborting." << llendl;
  130. mShouldRetry = false;
  131. }
  132. if (mShouldRetry)
  133. {
  134. llinfos << "Retry count: " << mRetryCount << ". Will retry after "
  135. << wait_time << "s." << llendl;
  136. mRetryTimer.reset();
  137. mRetryTimer.setTimerExpirySec(wait_time);
  138. }
  139. ++mRetryCount;
  140. }
  141. bool LLAdaptiveRetryPolicy::shouldRetry(F32& seconds_to_wait) const
  142. {
  143. if (mRetryCount == 0)
  144. {
  145. // Called shouldRetry before any failure.
  146. seconds_to_wait = F32_MAX;
  147. return false;
  148. }
  149. seconds_to_wait = mShouldRetry ? mRetryTimer.getRemainingTimeF32()
  150. : F32_MAX;
  151. return mShouldRetry;
  152. }
  153. // Parses 'Retry-After' header contents and returns seconds until retry should
  154. // occur.
  155. //static
  156. bool LLAdaptiveRetryPolicy::getSecondsUntilRetryAfter(const std::string& retry_after,
  157. F32& seconds_to_wait)
  158. {
  159. // *TODO: This needs testing ! Not in use yet. Examples of Retry-After
  160. // headers: Retry-After: Fri, 31 Dec 1999 23:59:59 GMT
  161. // Retry-After: 120
  162. // Check for number of seconds version, first:
  163. char* end = 0;
  164. // Parse as double
  165. double seconds = std::strtod(retry_after.c_str(), &end);
  166. if (end != 0 && *end == 0)
  167. {
  168. // Successful parse
  169. seconds_to_wait = (F32)seconds;
  170. return true;
  171. }
  172. // Parse rfc1123 date.
  173. time_t date = curl_getdate(retry_after.c_str(), NULL);
  174. if (date == -1)
  175. {
  176. return false;
  177. }
  178. seconds_to_wait = (F64)date - LLTimer::getTotalSeconds();
  179. return true;
  180. }