Skip to content

Commit cb0e5d1

Browse files
authored
Merge pull request #15 from CodyBatt/master
A bunch of random hacking
2 parents f767e4a + c35209d commit cb0e5d1

File tree

7 files changed

+50
-15
lines changed

7 files changed

+50
-15
lines changed

ReleaseNotes.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
### new in 1.0.7 (Release 2018/08/09)
2+
* New: EndpointPool.Build()..WithHttpReadTimeout() - Set timeout for HTTP GET requests
3+
* Fixed: NullReferenceException thrown in certain HTTP timeout conditions is now EtcdTimeoutException
4+
15
### new in 1.0.6.1 (Release 2018/08/02)
26
* Change: Updated `build.cake` to support deploying symbol package
37

source/Draft/Endpoints/EndpointPool.Builder.cs

+30-14
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,13 @@ public sealed class Builder
2727

2828
private EndpointVerificationStrategy _verificationStrategy;
2929

30-
internal EndpointRoutingStrategy RoutingStrategy
31-
{
32-
get { return _routingStrategy ?? EndpointRoutingStrategy.Default; }
33-
}
30+
private TimeSpan? _httpGetTimeout;
3431

35-
internal EndpointVerificationStrategy VerificationStrategy
36-
{
37-
get { return _verificationStrategy ?? EndpointVerificationStrategy.Default; }
38-
}
32+
private EndpointRoutingStrategy RoutingStrategy => _routingStrategy ?? EndpointRoutingStrategy.Default;
33+
34+
private EndpointVerificationStrategy VerificationStrategy => _verificationStrategy ?? EndpointVerificationStrategy.Default;
35+
36+
internal TimeSpan? HttpGetTimeout => _httpGetTimeout;
3937

4038
/// <summary>
4139
/// Verifies the passed <paramref name="uris" /> first to ensure that they are <see cref="Uri.IsAbsoluteUri" />. Then
@@ -54,19 +52,22 @@ public async Task<EndpointPool> VerifyAndBuild(params Uri[] uris)
5452
{
5553
if (uris == null || !uris.Any())
5654
{
57-
throw new ArgumentNullException("uris", "You must supply at least 1 Uri");
55+
throw new ArgumentNullException(nameof(uris), "You must supply at least 1 Uri");
5856
}
5957
var invalidUris = uris.Where(x => !x.IsAbsoluteUri).ToList();
6058
if (invalidUris.Any())
6159
{
6260
throw new ArgumentException(
63-
string.Format("The following Uri(s) are not valid absolute Uri(s): '{0}'", string.Join(", ", invalidUris)),
64-
"uris"
61+
$"The following Uri(s) are not valid absolute Uri(s): '{string.Join(", ", invalidUris)}'",
62+
nameof(uris)
6563
);
6664
}
6765
var endpoints = await VerificationStrategy.Verify(uris);
6866

69-
return new EndpointPool(endpoints, RoutingStrategy);
67+
return new EndpointPool(endpoints, RoutingStrategy)
68+
{
69+
HttpGetTimeout = _httpGetTimeout
70+
};
7071
}
7172

7273
/// <summary>
@@ -77,7 +78,7 @@ public Builder WithRoutingStrategy(EndpointRoutingStrategy routingStrategy)
7778
{
7879
if (routingStrategy == null)
7980
{
80-
throw new ArgumentNullException("routingStrategy");
81+
throw new ArgumentNullException(nameof(routingStrategy));
8182
}
8283
_routingStrategy = routingStrategy;
8384
return this;
@@ -91,12 +92,27 @@ public Builder WithVerificationStrategy(EndpointVerificationStrategy verificatio
9192
{
9293
if (verificationStrategy == null)
9394
{
94-
throw new ArgumentNullException("verificationStrategy");
95+
throw new ArgumentNullException(nameof(verificationStrategy));
9596
}
9697
_verificationStrategy = verificationStrategy;
9798
return this;
9899
}
99100

101+
/// <summary>
102+
/// Sets the default timeout for HTTP GET requests
103+
/// </summary>
104+
/// <param name="httpGetTimeout"></param>
105+
/// <returns></returns>
106+
public Builder WithHttpReadTimeout(TimeSpan httpGetTimeout)
107+
{
108+
if (httpGetTimeout == null)
109+
{
110+
throw new ArgumentNullException(nameof(httpGetTimeout));
111+
}
112+
_httpGetTimeout = httpGetTimeout;
113+
return this;
114+
}
115+
100116
}
101117

102118
}

source/Draft/Endpoints/EndpointPool.cs

+3
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,8 @@ internal Url GetEndpointUrl(params string[] pathParts)
3838
return pathSegment.ToUrl(RoutingStrategy.Select(pathSegment.Value, OnlineEndpoints).Uri);
3939
}
4040

41+
[IgnoreDataMember]
42+
internal TimeSpan? HttpGetTimeout { get; set; } = null;
43+
4144
}
4245
}

source/Draft/Exceptions/ExceptionHandling.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public static EtcdException ProcessException(this FlurlHttpException This)
3636

3737
var etcdError = This.GetResponseJson<EtcdError>();
3838

39+
if (etcdError == null) { return new UnknownErrorException(This.Message); }
40+
3941
var message = etcdError.Message;
4042

4143
etcdError.ErrorCode = etcdError.ErrorCode
@@ -254,7 +256,9 @@ private static EtcdException AsTimeoutException(this FlurlHttpException This)
254256

255257
private static bool IsTimeoutException(this FlurlHttpException This)
256258
{
257-
return This is FlurlHttpTimeoutException;
259+
if (This is FlurlHttpTimeoutException) return true;
260+
261+
return This.InnerException is OperationCanceledException;
258262
}
259263

260264
#endregion

source/Draft/Extensions/Flurl.Extensions.cs

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Threading.Tasks;
55

66
using Flurl;
7+
using Flurl.Http;
78

89
namespace Draft
910
{
@@ -20,6 +21,11 @@ public static Url Conditionally(this Url This, bool predicate, Func<Url, Url> ac
2021
return predicate ? action(This) : This;
2122
}
2223

24+
public static IFlurlClient Conditionally(this Url This, bool predicate, Func<Url, IFlurlClient> action)
25+
{
26+
return predicate ? action(This) : new FlurlClient(This, true);
27+
}
28+
2329
public static Task<HttpResponseMessage> Conditionally(this Url This, bool predicate, object data, Func<Url, object, Task<HttpResponseMessage>> ifTrue, Func<Url, object, Task<HttpResponseMessage>> ifFalse)
2430
{
2531
return predicate ? ifTrue(This, data) : ifFalse(This, data);

source/Draft/Requests/BaseRequest.cs

+1
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@ public Url TargetUrl
3131
}
3232
}
3333

34+
protected TimeSpan? HttpGetTimeout => _endpointPool.HttpGetTimeout;
3435
}
3536
}

source/Draft/Requests/GetRequest.cs

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public async Task<IKeyEvent> Execute()
2828
return await TargetUrl
2929
.Conditionally(Quorum.HasValue && Quorum.Value, x => x.SetQueryParam(Constants.Etcd.Parameter_Quorum, Constants.Etcd.Parameter_True))
3030
.Conditionally(Recursive.HasValue && Recursive.Value, x => x.SetQueryParam(Constants.Etcd.Parameter_Recursive, Constants.Etcd.Parameter_True))
31+
.Conditionally(HttpGetTimeout != null, x=>x.WithTimeout(HttpGetTimeout.GetValueOrDefault()))
3132
.GetAsync()
3233
.ReceiveEtcdResponse<KeyEvent>(EtcdClient);
3334
}

0 commit comments

Comments
 (0)