Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(214)

Delta Between Two Patch Sets: Src/GoogleApis/Apis/Http/BackOffHandler.cs

Issue 13412046: Reimplement OAuth2 library - Step 1 (Closed) Base URL: https://google-api-dotnet-client.googlecode.com/hg/
Left Patch Set: all tests are running Created 10 years, 6 months ago
Right Patch Set: minor Created 10 years, 6 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
1 /* 1 /*
2 Copyright 2013 Google Inc 2 Copyright 2013 Google Inc
3 3
4 Licensed under the Apache License, Version 2.0 (the "License"); 4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License. 5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at 6 You may obtain a copy of the License at
7 7
8 http://www.apache.org/licenses/LICENSE-2.0 8 http://www.apache.org/licenses/LICENSE-2.0
9 9
10 Unless required by applicable law or agreed to in writing, software 10 Unless required by applicable law or agreed to in writing, software
(...skipping 14 matching lines...) Expand all
25 namespace Google.Apis.Http 25 namespace Google.Apis.Http
26 { 26 {
27 /// <summary> 27 /// <summary>
28 /// A thread-safe back-off handler which handles an abnormal HTTP response o r an exception with· 28 /// A thread-safe back-off handler which handles an abnormal HTTP response o r an exception with·
29 /// <see cref="IBackOff"/>. 29 /// <see cref="IBackOff"/>.
30 /// </summary> 30 /// </summary>
31 public class BackOffHandler : IHttpUnsuccessfulResponseHandler, IHttpExcepti onHandler 31 public class BackOffHandler : IHttpUnsuccessfulResponseHandler, IHttpExcepti onHandler
32 { 32 {
33 private static readonly ILogger Logger = ApplicationContext.Logger.ForTy pe<BackOffHandler>(); 33 private static readonly ILogger Logger = ApplicationContext.Logger.ForTy pe<BackOffHandler>();
34 34
35 /// <summary> An initializer class to initialize a back-off handler. </s ummary> 35 /// <summary>An initializer class to initialize a back-off handler.</sum mary>
36 public class Initializer 36 public class Initializer
37 { 37 {
38 /// <summary> Gets the back-off policy used by this back-off handler . </summary> 38 /// <summary>Gets the back-off policy used by this back-off handler. </summary>
39 public IBackOff BackOff { get; private set; } 39 public IBackOff BackOff { get; private set; }
40 40
41 /// <summary> 41 /// <summary>
42 /// Gets or sets the maximum time span to wait. If the back-off inst ance returns a greater time span then· 42 /// Gets or sets the maximum time span to wait. If the back-off inst ance returns a greater time span then·
43 /// this value, this handler returns <c>false</c> to both <see cref= "HandleExceptionAsync"/> and· 43 /// this value, this handler returns <c>false</c> to both <see cref= "HandleExceptionAsync"/> and·
44 /// <see cref="HandleResponseAsync"/>. Default value is 5 seconds pe r a retry request. 44 /// <see cref="HandleResponseAsync"/>. Default value is 5 seconds pe r a retry request.
45 /// </summary> 45 /// </summary>
46 public TimeSpan MaxTimeSpan { get; set; } 46 public TimeSpan MaxTimeSpan { get; set; }
47 47
48 /// <summary> 48 /// <summary>
49 /// Gets or sets a delegate function which indicates if this back-of f handler should handle an abnormal· 49 /// Gets or sets a delegate function which indicates if this back-of f handler should handle an abnormal·
50 /// HTTP response. The default is <see cref="DefaultHandleUnsuccessf ulResponseFunc"/>.· 50 /// HTTP response. The default is <see cref="DefaultHandleUnsuccessf ulResponseFunc"/>.·
51 /// </summary> 51 /// </summary>
52 public Func<HttpResponseMessage, bool> HandleUnsuccessfulResponseFun c { get; set; } 52 public Func<HttpResponseMessage, bool> HandleUnsuccessfulResponseFun c { get; set; }
53 53
54 /// <summary> 54 /// <summary>
55 /// Gets or sets a delegate function which indicates if this back-of f handler should handle an exception.· 55 /// Gets or sets a delegate function which indicates if this back-of f handler should handle an exception.·
56 /// The default is <see cref="DefaultHandleExceptionFunc"/>.· 56 /// The default is <see cref="DefaultHandleExceptionFunc"/>.·
peleyal 2013/09/10 01:14:44 remove extra space The default (single space)
peleyal 2013/09/10 12:32:22 Done.
57 /// </summary> 57 /// </summary>
58 public Func<Exception, bool> HandleExceptionFunc { get; set; } 58 public Func<Exception, bool> HandleExceptionFunc { get; set; }
59 59
60 /// <summary> Default function which handles server errors (503). </ summary> 60 /// <summary>Default function which handles server errors (503).</su mmary>
61 public static readonly Func<HttpResponseMessage, bool> DefaultHandle UnsuccessfulResponseFunc = 61 public static readonly Func<HttpResponseMessage, bool> DefaultHandle UnsuccessfulResponseFunc =
62 (r) => (int)r.StatusCode == 503; 62 (r) => (int)r.StatusCode == 503;
63 63
64 /// <summary> 64 /// <summary>
65 /// Default function which handles exception which aren't· 65 /// Default function which handles exception which aren't·
66 /// <seealso cref="System.Threading.Tasks.TaskCanceledException"/> o r· 66 /// <seealso cref="System.Threading.Tasks.TaskCanceledException"/> o r·
67 /// <seealso cref="System.OperationCanceledException"/>. Those excep tions represent a task or an operation 67 /// <seealso cref="System.OperationCanceledException"/>. Those excep tions represent a task or an operation
68 /// which was canceled and we shouldn't retry. 68 /// which was canceled and we shouldn't retry.
69 /// </summary> 69 /// </summary>
70 public static readonly Func<Exception, bool> DefaultHandleExceptionF unc = 70 public static readonly Func<Exception, bool> DefaultHandleExceptionF unc =
71 (ex) => !(ex is TaskCanceledException || ex is OperationCanceled Exception); 71 (ex) => !(ex is TaskCanceledException || ex is OperationCanceled Exception);
72 72
73 /// <summary> Constructs a new initializer by the given back-off. </ summary> 73 /// <summary>Constructs a new initializer by the given back-off.</su mmary>
74 public Initializer(IBackOff backOff) 74 public Initializer(IBackOff backOff)
75 { 75 {
76 BackOff = backOff; 76 BackOff = backOff;
77 HandleExceptionFunc = DefaultHandleExceptionFunc; 77 HandleExceptionFunc = DefaultHandleExceptionFunc;
78 HandleUnsuccessfulResponseFunc = DefaultHandleUnsuccessfulRespon seFunc; 78 HandleUnsuccessfulResponseFunc = DefaultHandleUnsuccessfulRespon seFunc;
79 MaxTimeSpan = TimeSpan.FromSeconds(5); 79 MaxTimeSpan = TimeSpan.FromSeconds(5);
80 } 80 }
81 } 81 }
82 82
83 /// <summary> Gets the back-off policy used by this back-off handler. </ summary> 83 /// <summary>Gets the back-off policy used by this back-off handler.</su mmary>
84 public IBackOff BackOff { get; private set; } 84 public IBackOff BackOff { get; private set; }
85 85
86 /// <summary> 86 /// <summary>
87 /// Gets the maximum time span to wait. If the back-off instance returns a greater time span, the handle method 87 /// Gets the maximum time span to wait. If the back-off instance returns a greater time span, the handle method
88 /// returns <c>false</c>. Default value is 5 seconds per a retry request . 88 /// returns <c>false</c>. Default value is 5 seconds per a retry request .
89 /// </summary> 89 /// </summary>
90 public TimeSpan MaxTimeSpan { get; private set; } 90 public TimeSpan MaxTimeSpan { get; private set; }
91 91
92 /// <summary> 92 /// <summary>
93 /// Gets a delegate function which indicates if this back-off handler sh ould handle an abnormal HTTP response.· 93 /// Gets a delegate function which indicates if this back-off handler sh ould handle an abnormal HTTP response.·
94 /// The default is <see cref="DefaultHandleUnsuccessfulResponseFunc"/>.· 94 /// The default is <see cref="DefaultHandleUnsuccessfulResponseFunc"/>.·
95 /// </summary> 95 /// </summary>
96 public Func<HttpResponseMessage, bool> HandleUnsuccessfulResponseFunc { get; private set; } 96 public Func<HttpResponseMessage, bool> HandleUnsuccessfulResponseFunc { get; private set; }
97 97
98 /// <summary> 98 /// <summary>
99 /// Gets a delegate function which indicates if this back-off handler sh ould handle an exception. The· 99 /// Gets a delegate function which indicates if this back-off handler sh ould handle an exception. The·
100 /// default is <see cref="DefaultHandleExceptionFunc"/>.· 100 /// default is <see cref="DefaultHandleExceptionFunc"/>.·
101 /// </summary> 101 /// </summary>
102 public Func<Exception, bool> HandleExceptionFunc { get; private set; } 102 public Func<Exception, bool> HandleExceptionFunc { get; private set; }
103 103
104 /// <summary> Constructs a new back-off handler with the given back-off. </summary> 104 /// <summary>Constructs a new back-off handler with the given back-off.< /summary>
105 /// <param name="backOff">The back-off policy</param>
105 public BackOffHandler(IBackOff backOff) 106 public BackOffHandler(IBackOff backOff)
106 : this(new Initializer(backOff)) 107 : this(new Initializer(backOff))
107 { 108 {
108 } 109 }
109 110
110 /// <summary> Constructs a new back-off handler with the given initializ er. </summary> 111 /// <summary>Constructs a new back-off handler with the given initialize r.</summary>
111 public BackOffHandler(Initializer initializer) 112 public BackOffHandler(Initializer initializer)
112 { 113 {
113 BackOff = initializer.BackOff; 114 BackOff = initializer.BackOff;
114 MaxTimeSpan = initializer.MaxTimeSpan; 115 MaxTimeSpan = initializer.MaxTimeSpan;
115 HandleExceptionFunc = initializer.HandleExceptionFunc; 116 HandleExceptionFunc = initializer.HandleExceptionFunc;
116 HandleUnsuccessfulResponseFunc = initializer.HandleUnsuccessfulRespo nseFunc; 117 HandleUnsuccessfulResponseFunc = initializer.HandleUnsuccessfulRespo nseFunc;
117 } 118 }
118 119
119 #region IHttpUnsuccessfulResponseHandler 120 #region IHttpUnsuccessfulResponseHandler
120 121
121 public virtual async Task<bool> HandleResponseAsync(HandleUnsuccessfulRe sponseArgs args) 122 public virtual async Task<bool> HandleResponseAsync(HandleUnsuccessfulRe sponseArgs args)
122 { 123 {
123 // if the func returns true try to handle this current failed try 124 // if the func returns true try to handle this current failed try
124 return HandleUnsuccessfulResponseFunc != null && HandleUnsuccessfulR esponseFunc(args.Response) && 125 return HandleUnsuccessfulResponseFunc != null && HandleUnsuccessfulR esponseFunc(args.Response) &&
125 await Handle(args.SupportsRetry, args.CurrentFailedTry, args.Can cellationToken); 126 await HandleAsync(args.SupportsRetry, args.CurrentFailedTry, arg s.CancellationToken);
126 } 127 }
127 128
128 #endregion 129 #endregion
129 130
130 #region IHttpExceptionHandler 131 #region IHttpExceptionHandler
131 132
132 public virtual async Task<bool> HandleExceptionAsync(HandleExceptionArgs args) 133 public virtual async Task<bool> HandleExceptionAsync(HandleExceptionArgs args)
133 { 134 {
134 // if the func returns true try to handle this current failed try 135 // if the func returns true try to handle this current failed try
135 return HandleExceptionFunc != null && HandleExceptionFunc(args.Excep tion) && 136 return HandleExceptionFunc != null && HandleExceptionFunc(args.Excep tion) &&
136 await Handle(args.SupportsRetry, args.CurrentFailedTry, args.Can cellationToken); 137 await HandleAsync(args.SupportsRetry, args.CurrentFailedTry, arg s.CancellationToken);
peleyal 2013/09/10 01:14:44 rename HandleAsync
peleyal 2013/09/10 12:32:22 Done.
137 } 138 }
138 139
139 #endregion 140 #endregion
140 141
141 /// <summary> 142 /// <summary>
142 /// Handles back-off. In case the request doesn't support retry or the b ack-off time span is greater than the 143 /// Handles back-off. In case the request doesn't support retry or the b ack-off time span is greater than the
143 /// maximum time span allowed for a request, the handler returns <c>fals e</c>. Otherwise, current thread will 144 /// maximum time span allowed for a request, the handler returns <c>fals e</c>. Otherwise, current thread will
144 /// block for x milliseconds (x is defined by the <see cref="BackOff"/> instance), and this handler returns· 145 /// block for x milliseconds (x is defined by the <see cref="BackOff"/> instance), and this handler returns·
145 /// <c>true</c>. 146 /// <c>true</c>.
146 /// </summary> 147 /// </summary>
147 private async Task<bool> Handle(bool supportsRetry, int currentFailedTry , CancellationToken cancellationToken) 148 private async Task<bool> HandleAsync(bool supportsRetry, int currentFail edTry,
149 CancellationToken cancellationToken)
148 { 150 {
149 if (!supportsRetry || BackOff.MaxNumOfRetries < currentFailedTry) 151 if (!supportsRetry || BackOff.MaxNumOfRetries < currentFailedTry)
150 { 152 {
151 return false; 153 return false;
152 } 154 }
153 155
154 TimeSpan ts = BackOff.GetNextBackOff(currentFailedTry); 156 TimeSpan ts = BackOff.GetNextBackOff(currentFailedTry);
155 if (ts > MaxTimeSpan || ts < TimeSpan.Zero) 157 if (ts > MaxTimeSpan || ts < TimeSpan.Zero)
156 { 158 {
157 return false; 159 return false;
158 } 160 }
159 161
160 await Wait(ts, cancellationToken); 162 await Wait(ts, cancellationToken);
161 Logger.Debug("Back-Off handled the error. Waited {0}ms before next r etry...", ts.TotalMilliseconds); 163 Logger.Debug("Back-Off handled the error. Waited {0}ms before next r etry...", ts.TotalMilliseconds);
162 return true; 164 return true;
163 } 165 }
164 166
165 /// <summary> Waits the given time span. Override this method is recomme nded for mocking purposes.</summary> 167 /// <summary>Waits the given time span. Override this method is recommen ded for mocking purposes.</summary>
166 /// <param name="ts">TimeSpan to wait (and block the current thread)</pa ram> 168 /// <param name="ts">TimeSpan to wait (and block the current thread)</pa ram>
167 /// <param name="cancellationToken">The cancellation token in case the u ser wants to cancel the operation in· 169 /// <param name="cancellationToken">The cancellation token in case the u ser wants to cancel the operation in·
168 /// the middle</param> 170 /// the middle</param>
169 protected virtual async Task Wait(TimeSpan ts, CancellationToken cancell ationToken) 171 protected virtual async Task Wait(TimeSpan ts, CancellationToken cancell ationToken)
170 { 172 {
171 await TaskEx.Delay(ts, cancellationToken); 173 await TaskEx.Delay(ts, cancellationToken);
172 } 174 }
173 } 175 }
174 } 176 }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b