Skip to content

Commit

Permalink
Concurrency
Browse files Browse the repository at this point in the history
  • Loading branch information
m-sadegh-sh committed Mar 10, 2024
1 parent 52a3905 commit b28ca1b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
23 changes: 12 additions & 11 deletions SharedApp/Network.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Zebble.Device
{
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand All @@ -15,13 +16,13 @@ public static partial class Network
const int DEFAULT_TIME_OUT = 5000;
public static readonly AsyncEvent<bool> ConnectivityChanged = new(ConcurrentEventRaisePolicy.Queue);
static readonly List<Task> AllDownloads = new();
static Dictionary<string, HttpClient> HttpClients = new();
static ConcurrentDictionary<string, HttpClient> HttpClients = new();

static Network()
{
#if ANDROID || IOS
ServicePointManager.ServerCertificateValidationCallback += (_, _, _, _) => true;
ServicePointManager.DefaultConnectionLimit = 256;
ServicePointManager.DefaultConnectionLimit = 256;
#endif
Init();
}
Expand All @@ -43,20 +44,20 @@ static HttpClient CreateHttpClient(string hostName, TimeSpan timeout, string sch
if (hostName.ContainsAny(["/", ":"])) throw new HttpRequestException($"Invalid host name format: {hostName}");
var baseAddress = $"{scheme}://{hostName}:{port}";

return HttpClients.GetOrAdd(baseAddress, CreateClient);

if (HttpClients.TryGetValue(baseAddress, out var result))
return result;

HttpClient CreateClient()
{
#if IOS
var handler = new NSUrlSessionHandler();
var handler = new NSUrlSessionHandler();
#elif ANDROID
var handler = new Xamarin.Android.Net.AndroidClientHandler();
var handler = new Xamarin.Android.Net.AndroidClientHandler();
#else
var handler = new HttpClientHandler();
var handler = new HttpClientHandler();
#endif

result = new HttpClient(handler) { BaseAddress = baseAddress.AsUri(), Timeout = timeout };
return HttpClients[baseAddress] = result;
return new HttpClient(handler) { BaseAddress = baseAddress.AsUri(), Timeout = timeout };
}
}

/// <summary>
Expand Down Expand Up @@ -138,7 +139,7 @@ static async void TryDownload(Uri url, int retries, int timeoutPerAttempt, TaskC
if (completedFirst != fetchTask)
{
// Mark the exception as observed so it does not crash the whole thing.
fetchTask.ContinueWith(x => { }).GetAwaiter();
fetchTask.ContinueWith(x => { }).GetAwaiter();
Log.For(typeof(Network)).Warning("Attempt #" + retry + " timed out for downloading " + url);
}
else if (fetchTask.Status == TaskStatus.Faulted || fetchTask.Status == TaskStatus.Canceled)
Expand Down
2 changes: 1 addition & 1 deletion Zebble/Zebble.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<RootNamespace>Zebble</RootNamespace>
<PackageId>Zebble</PackageId>
<Product>$(AssemblyName) ($(TargetFramework))</Product>
<Version>5.0.5.0</Version>
<Version>5.0.6.0</Version>
<PackOnBuild>true</PackOnBuild>
<UseMaui>true</UseMaui>
<SingleProject>true</SingleProject>
Expand Down

0 comments on commit b28ca1b

Please sign in to comment.