-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Russia is a shit and blocks a lot of western services. We want to be able to switch to an off-the-shelf CDN for engine downloads, but Russia gets in the way of that. The fallback system allows 99% of users to use the main CDN, while the unfortunate ones that have Russia problems just go directly to our servers.
- Loading branch information
Showing
5 changed files
with
178 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using System; | ||
using System.Collections.Immutable; | ||
using System.Net.Http; | ||
using System.Net.Http.Json; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Serilog; | ||
|
||
namespace SS14.Launcher.Utility; | ||
|
||
public sealed class UrlFallbackSet(ImmutableArray<string> urls) | ||
{ | ||
public static readonly TimeSpan AttemptDelay = TimeSpan.FromSeconds(3); | ||
|
||
public readonly ImmutableArray<string> Urls = urls; | ||
|
||
public async Task<T?> GetFromJsonAsync<T>(HttpClient client, CancellationToken cancel = default) where T : notnull | ||
{ | ||
var msg = await GetAsync(client, cancel).ConfigureAwait(false); | ||
msg.EnsureSuccessStatusCode(); | ||
|
||
return await msg.Content.ReadFromJsonAsync<T>(cancel).ConfigureAwait(false); | ||
} | ||
|
||
public async Task<HttpResponseMessage> GetAsync(HttpClient httpClient, CancellationToken cancel = default) | ||
{ | ||
return await SendAsync(httpClient, url => new HttpRequestMessage(HttpMethod.Get, url), cancel) | ||
.ConfigureAwait(false); | ||
} | ||
|
||
public async Task<HttpResponseMessage> SendAsync( | ||
HttpClient httpClient, | ||
Func<string, HttpRequestMessage> builder, | ||
CancellationToken cancel = default) | ||
{ | ||
var (response, index) = await HappyEyeballsHttp.ParallelTask( | ||
Urls.Length, | ||
(i, token) => AttemptConnection(httpClient, builder(Urls[i]), token), | ||
AttemptDelay, | ||
cancel).ConfigureAwait(false); | ||
|
||
Log.Verbose("Successfully connected to {Url}", Urls[index]); | ||
|
||
return response; | ||
} | ||
|
||
private static async Task<HttpResponseMessage> AttemptConnection( | ||
HttpClient httpClient, | ||
HttpRequestMessage message, | ||
CancellationToken cancel) | ||
{ | ||
if (new Random().Next(2) == 0) | ||
{ | ||
Log.Error("Dropped the URL: {Message}", message); | ||
throw new InvalidOperationException("OOPS"); | ||
} | ||
|
||
var response = await httpClient.SendAsync( | ||
message, | ||
HttpCompletionOption.ResponseHeadersRead, | ||
cancel | ||
).ConfigureAwait(false); | ||
|
||
return response; | ||
} | ||
} |