Skip to content

Commit d4d3473

Browse files
authored
DRV-47: Update C# driver to speak HTTP/2 (#137)
1 parent 3565b33 commit d4d3473

File tree

4 files changed

+36
-10
lines changed

4 files changed

+36
-10
lines changed

FaunaDB.Client/Client/DefaultClientIO.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,31 @@ class DefaultClientIO : IClientIO
2727
readonly AuthenticationHeaderValue authHeader;
2828

2929
private LastSeen lastSeen;
30+
private Version httpVersion;
3031

3132
public const string StreamingPath = "stream";
3233
public const HttpMethodKind StreamingHttpMethod = HttpMethodKind.Post;
3334

34-
internal DefaultClientIO(HttpClient client, AuthenticationHeaderValue authHeader, LastSeen lastSeen, Uri endpoint, TimeSpan? timeout)
35+
internal DefaultClientIO(HttpClient client, AuthenticationHeaderValue authHeader, LastSeen lastSeen, Uri endpoint, TimeSpan? timeout, Version httpVersion)
3536
{
3637
this.client = client;
3738
this.authHeader = authHeader;
3839
this.lastSeen = lastSeen;
3940
this.endpoint = endpoint;
4041
this.clientTimeout = timeout;
42+
#if NETSTANDARD2_1
43+
this.httpVersion = httpVersion == null ? new Version(2, 0) : httpVersion;
44+
#else
45+
this.httpVersion = httpVersion == null ? new Version(1, 1) : httpVersion;
46+
#endif
4147
}
4248

43-
public DefaultClientIO(string secret, Uri endpoint, TimeSpan? timeout = null, HttpClient httpClient = null)
44-
: this(httpClient ?? CreateClient(), AuthHeader(secret), new LastSeen(), endpoint, timeout)
49+
public DefaultClientIO(string secret, Uri endpoint, TimeSpan? timeout = null, HttpClient httpClient = null, Version httpVersion = null)
50+
: this(httpClient ?? CreateClient(), AuthHeader(secret), new LastSeen(), endpoint, timeout, httpVersion)
4551
{ }
4652

4753
public IClientIO NewSessionClient(string secret) =>
48-
new DefaultClientIO(client, AuthHeader(secret), lastSeen, endpoint, clientTimeout);
54+
new DefaultClientIO(client, AuthHeader(secret), lastSeen, endpoint, clientTimeout, httpVersion);
4955

5056
public Task<RequestResult> DoRequest(HttpMethodKind method, string path, string data, IReadOnlyDictionary<string, string> query = null, TimeSpan? queryTimeout = null) =>
5157
DoRequestAsync(method, path, data, query, queryTimeout);
@@ -69,6 +75,7 @@ async Task<RequestResult> DoRequestAsync(HttpMethodKind method, string path, str
6975
message.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
7076
message.Headers.Add("X-FaunaDB-API-Version", "4");
7177
message.Headers.Add("X-Driver-Env", RuntimeEnvironmentHeader.Construct(EnvironmentEditor.Create()));
78+
message.Version = httpVersion;
7279

7380
var last = lastSeen.Txn;
7481
if (last.HasValue)
@@ -119,6 +126,8 @@ async Task<StreamingRequestResult> DoStreamingRequestAsync(string data, IReadOnl
119126
message.Headers.Authorization = authHeader;
120127
message.Headers.Add("X-FaunaDB-API-Version", "4");
121128
message.Headers.Add("X-Driver-Env", RuntimeEnvironmentHeader.Construct(EnvironmentEditor.Create()));
129+
message.Version = httpVersion;
130+
message.SetTimeout(Timeout.InfiniteTimeSpan);
122131

123132
var last = lastSeen.Txn;
124133
if (last.HasValue)
@@ -128,7 +137,7 @@ async Task<StreamingRequestResult> DoStreamingRequestAsync(string data, IReadOnl
128137

129138
var httpResponse = await client.SendAsync(message, HttpCompletionOption.ResponseHeadersRead, CancellationToken.None).ConfigureAwait(false);
130139

131-
Stream response = await httpResponse.Content.ReadAsStreamAsync();
140+
Stream response = await httpResponse.Content.ReadAsStreamAsync().ConfigureAwait(false);
132141

133142
var endTime = DateTime.UtcNow;
134143

FaunaDB.Client/Client/FaunaClient.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@ public class FaunaClient
3131
/// <param name="secret">Auth token for the FaunaDB server.</param>
3232
/// <param name="endpoint">URL for the FaunaDB server. Defaults to "https://db.fauna.com:443"</param>
3333
/// <param name="timeout">Timeout for I/O operations. Defaults to 1 minute.</param>
34+
/// <param name="httpVersion">Version of http. Default value is HttpVersion.Version11, is you use .net core 3.0 and above you can enable http/2 support by passing HttpVersion.Version20</param>
3435
public FaunaClient(
3536
string secret,
3637
string endpoint = "https://db.fauna.com:443",
3738
TimeSpan? timeout = null,
38-
HttpClient httpClient = null)
39-
: this(CreateClient(secret, endpoint, timeout, httpClient))
39+
HttpClient httpClient = null,
40+
Version httpVersion = null)
41+
: this(CreateClient(secret, endpoint, timeout, httpClient, httpVersion))
4042
{ }
4143

4244
/// <summary>
@@ -241,13 +243,15 @@ static IClientIO CreateClient(
241243
string secret,
242244
string endpoint,
243245
TimeSpan? timeout = null,
244-
HttpClient httpClient = null)
246+
HttpClient httpClient = null,
247+
Version httpVersion = null)
245248
{
246249
return new DefaultClientIO(
247250
secret: secret,
248251
endpoint: new Uri(endpoint),
249252
timeout: timeout,
250-
httpClient: httpClient
253+
httpClient: httpClient,
254+
httpVersion: httpVersion
251255
);
252256
}
253257
}

FaunaDB.Client/FaunaDB.Client.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ See https://fauna.com for more information.</Description>
3535
</Choose>
3636

3737
<ItemGroup>
38-
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
38+
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
3939
</ItemGroup>
4040
</Project>

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,19 @@ http.Timeout = TimeSpan.FromSeconds(15);
146146
var client = new FaunaClient("secret", "http://localhost:9090/", httpClient: http);
147147
```
148148

149+
#### HTTP 2.0 support
150+
Starting from version 4.0.0 of this driver (faunadb-csharp), HTTP/2 support is enabled by default for .NET standards 2.1 and above.
151+
This means that if you use .NET core 3.1 and above (which support that standard), you'll be sending requests to Fauna on HTTP/2.
152+
.NET standards lower than 2.1 and .NET frameworks 4.5-4.8 have HTTP/1.1 enabled as the default protocol version, since they lack of support for HTTP/2.
153+
We've also added an optional parameter if you want to specify the version of the protocol directly:
154+
```csharp
155+
var adminClient = new FaunaClient(
156+
endpoint: endpoint,
157+
secret: secret,
158+
httpVersion: HttpVersion.Version11
159+
);
160+
```
161+
149162
#### How to execute a query
150163

151164
```csharp

0 commit comments

Comments
 (0)