Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/azure/core/pipeline/policies/_retry_async.py: 25%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

71 statements  

1# -------------------------------------------------------------------------- 

2# 

3# Copyright (c) Microsoft Corporation. All rights reserved. 

4# 

5# The MIT License (MIT) 

6# 

7# Permission is hereby granted, free of charge, to any person obtaining a copy 

8# of this software and associated documentation files (the ""Software""), to 

9# deal in the Software without restriction, including without limitation the 

10# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 

11# sell copies of the Software, and to permit persons to whom the Software is 

12# furnished to do so, subject to the following conditions: 

13# 

14# The above copyright notice and this permission notice shall be included in 

15# all copies or substantial portions of the Software. 

16# 

17# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 

18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 

19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 

20# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 

21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 

22# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 

23# IN THE SOFTWARE. 

24# 

25# -------------------------------------------------------------------------- 

26""" 

27This module is the requests implementation of Pipeline ABC 

28""" 

29from typing import TypeVar, Dict, Any, Optional, cast 

30import logging 

31import time 

32from azure.core.pipeline import PipelineRequest, PipelineResponse 

33from azure.core.pipeline.transport import ( 

34 AsyncHttpResponse as LegacyAsyncHttpResponse, 

35 HttpRequest as LegacyHttpRequest, 

36 AsyncHttpTransport, 

37) 

38from azure.core.rest import AsyncHttpResponse, HttpRequest 

39from azure.core.exceptions import ( 

40 AzureError, 

41 ClientAuthenticationError, 

42 ServiceRequestError, 

43) 

44from ._base_async import AsyncHTTPPolicy 

45from ._retry import RetryPolicyBase 

46 

47AsyncHTTPResponseType = TypeVar("AsyncHTTPResponseType", AsyncHttpResponse, LegacyAsyncHttpResponse) 

48HTTPRequestType = TypeVar("HTTPRequestType", HttpRequest, LegacyHttpRequest) 

49 

50_LOGGER = logging.getLogger(__name__) 

51 

52 

53class AsyncRetryPolicy(RetryPolicyBase, AsyncHTTPPolicy[HTTPRequestType, AsyncHTTPResponseType]): 

54 """Async flavor of the retry policy. 

55 

56 The async retry policy in the pipeline can be configured directly, or tweaked on a per-call basis. 

57 

58 :keyword int retry_total: Total number of retries to allow. Takes precedence over other counts. 

59 Default value is 10. 

60 

61 :keyword int retry_connect: How many connection-related errors to retry on. 

62 These are errors raised before the request is sent to the remote server, 

63 which we assume has not triggered the server to process the request. Default value is 3. 

64 

65 :keyword int retry_read: How many times to retry on read errors. 

66 These errors are raised after the request was sent to the server, so the 

67 request may have side-effects. Default value is 3. 

68 

69 :keyword int retry_status: How many times to retry on bad status codes. Default value is 3. 

70 

71 :keyword float retry_backoff_factor: A backoff factor to apply between attempts after the second try 

72 (most errors are resolved immediately by a second try without a delay). 

73 Retry policy will sleep for: `{backoff factor} * (2 ** ({number of total retries} - 1))` 

74 seconds. If the backoff_factor is 0.1, then the retry will sleep 

75 for [0.0s, 0.2s, 0.4s, ...] between retries. The default value is 0.8. 

76 

77 :keyword int retry_backoff_max: The maximum back off time. Default value is 120 seconds (2 minutes). 

78 

79 .. admonition:: Example: 

80 

81 .. literalinclude:: ../samples/test_example_async.py 

82 :start-after: [START async_retry_policy] 

83 :end-before: [END async_retry_policy] 

84 :language: python 

85 :dedent: 4 

86 :caption: Configuring an async retry policy. 

87 """ 

88 

89 async def _sleep_for_retry( 

90 self, 

91 response: PipelineResponse[HTTPRequestType, AsyncHTTPResponseType], 

92 transport: AsyncHttpTransport[HTTPRequestType, AsyncHTTPResponseType], 

93 ) -> bool: 

94 """Sleep based on the Retry-After response header value. 

95 

96 :param response: The PipelineResponse object. 

97 :type response: ~azure.core.pipeline.PipelineResponse 

98 :param transport: The HTTP transport type. 

99 :type transport: ~azure.core.pipeline.transport.AsyncHttpTransport 

100 :return: Whether the retry-after value was found. 

101 :rtype: bool 

102 """ 

103 retry_after = self.get_retry_after(response) 

104 if retry_after: 

105 await transport.sleep(retry_after) 

106 return True 

107 return False 

108 

109 async def _sleep_backoff( 

110 self, settings: Dict[str, Any], transport: AsyncHttpTransport[HTTPRequestType, AsyncHTTPResponseType] 

111 ) -> None: 

112 """Sleep using exponential backoff. Immediately returns if backoff is 0. 

113 

114 :param dict settings: The retry settings. 

115 :param transport: The HTTP transport type. 

116 :type transport: ~azure.core.pipeline.transport.AsyncHttpTransport 

117 """ 

118 backoff = self.get_backoff_time(settings) 

119 if backoff <= 0: 

120 return 

121 await transport.sleep(backoff) 

122 

123 async def sleep( 

124 self, 

125 settings: Dict[str, Any], 

126 transport: AsyncHttpTransport[HTTPRequestType, AsyncHTTPResponseType], 

127 response: Optional[PipelineResponse[HTTPRequestType, AsyncHTTPResponseType]] = None, 

128 ) -> None: 

129 """Sleep between retry attempts. 

130 

131 This method will respect a server's ``Retry-After`` response header 

132 and sleep the duration of the time requested. If that is not present, it 

133 will use an exponential backoff. By default, the backoff factor is 0 and 

134 this method will return immediately. 

135 

136 :param dict settings: The retry settings. 

137 :param transport: The HTTP transport type. 

138 :type transport: ~azure.core.pipeline.transport.AsyncHttpTransport 

139 :param response: The PipelineResponse object. 

140 :type response: ~azure.core.pipeline.PipelineResponse 

141 """ 

142 if response: 

143 slept = await self._sleep_for_retry(response, transport) 

144 if slept: 

145 return 

146 await self._sleep_backoff(settings, transport) 

147 

148 async def send( 

149 self, request: PipelineRequest[HTTPRequestType] 

150 ) -> PipelineResponse[HTTPRequestType, AsyncHTTPResponseType]: 

151 """Uses the configured retry policy to send the request to the next policy in the pipeline. 

152 

153 :param request: The PipelineRequest object 

154 :type request: ~azure.core.pipeline.PipelineRequest 

155 :return: Returns the PipelineResponse or raises error if maximum retries exceeded. 

156 :rtype: ~azure.core.pipeline.PipelineResponse 

157 :raise: ~azure.core.exceptions.AzureError if maximum retries exceeded. 

158 :raise: ~azure.core.exceptions.ClientAuthenticationError if authentication fails 

159 """ 

160 retry_active = True 

161 response = None 

162 retry_settings = self.configure_retries(request.context.options) 

163 self._configure_positions(request, retry_settings) 

164 

165 absolute_timeout = retry_settings["timeout"] 

166 is_response_error = True 

167 

168 while retry_active: 

169 start_time = time.time() 

170 # PipelineContext types transport as a Union of HttpTransport and AsyncHttpTransport, but 

171 # here we know that this is an AsyncHttpTransport. 

172 # The correct fix is to make PipelineContext generic, but that's a breaking change and a lot of 

173 # generic to update in Pipeline, PipelineClient, PipelineRequest, PipelineResponse, etc. 

174 transport: AsyncHttpTransport[HTTPRequestType, AsyncHTTPResponseType] = cast( 

175 AsyncHttpTransport[HTTPRequestType, AsyncHTTPResponseType], request.context.transport 

176 ) 

177 try: 

178 self._configure_timeout(request, absolute_timeout, is_response_error) 

179 request.context["retry_count"] = len(retry_settings["history"]) 

180 response = await self.next.send(request) 

181 if self.is_retry(retry_settings, response): 

182 retry_active = self.increment(retry_settings, response=response) 

183 if retry_active: 

184 await self.sleep( 

185 retry_settings, 

186 transport, 

187 response=response, 

188 ) 

189 is_response_error = True 

190 continue 

191 break 

192 except ClientAuthenticationError: # pylint:disable=try-except-raise 

193 # the authentication policy failed such that the client's request can't 

194 # succeed--we'll never have a response to it, so propagate the exception 

195 raise 

196 except AzureError as err: 

197 if absolute_timeout > 0 and self._is_method_retryable(retry_settings, request.http_request): 

198 retry_active = self.increment(retry_settings, response=request, error=err) 

199 if retry_active: 

200 await self.sleep(retry_settings, transport) 

201 if isinstance(err, ServiceRequestError): 

202 is_response_error = False 

203 else: 

204 is_response_error = True 

205 continue 

206 raise err 

207 finally: 

208 end_time = time.time() 

209 if absolute_timeout: 

210 absolute_timeout -= end_time - start_time 

211 if not response: 

212 raise AzureError("Maximum retries exceeded.") 

213 

214 self.update_context(response.context, retry_settings) 

215 return response