Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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 Loading... | |
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"/>.· |
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> |
class
2013/09/16 23:47:29
Missing <param>
peleyal
2013/09/17 15:53:40
Done.
| |
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 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
156 if (ts > MaxTimeSpan || ts < TimeSpan.Zero) | 157 if (ts > MaxTimeSpan || ts < TimeSpan.Zero) |
157 { | 158 { |
158 return false; | 159 return false; |
159 } | 160 } |
160 | 161 |
161 await Wait(ts, cancellationToken); | 162 await Wait(ts, cancellationToken); |
162 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); |
163 return true; | 164 return true; |
164 } | 165 } |
165 | 166 |
166 /// <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> |
167 /// <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> |
168 /// <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· |
169 /// the middle</param> | 170 /// the middle</param> |
170 protected virtual async Task Wait(TimeSpan ts, CancellationToken cancell ationToken) | 171 protected virtual async Task Wait(TimeSpan ts, CancellationToken cancell ationToken) |
171 { | 172 { |
172 await TaskEx.Delay(ts, cancellationToken); | 173 await TaskEx.Delay(ts, cancellationToken); |
173 } | 174 } |
174 } | 175 } |
175 } | 176 } |
LEFT | RIGHT |