AvaloniaUI/Avalonia.Labs

Bad usage of HttpClient

PhilWeisz opened this issue · 0 comments

AsyncImage uses HttpClient in several bad ways:
Currently inside AsyncImage.cs:
using var client = new HttpClient(); Link

I think it's a bad practice create a new HttpClient per control and force the auto-disposal of the HttpClient.

Why:
When opening many AsyncImages we create many clients in parallel to load the images which in turn:

  1. provoke socket-exhaustion, leading to a System.Net.Sockets.Exception (after disposing the HttpClient that socket is not instantly cleared, so it remains unusable for quite some time, in the TIME_WAIT state)
  2. provoke hitting rate-limits of the server (in case the images are all loaded from the same server)

What AsyncImage should do:
AsyncImage should enable supplying a HttpClient by the consumer in order to allow shared use of HttpClient(s) which can handle multiple requests in parallel and this would address both these problems.
If the developer wants to use that auto-disposing feature he should set some boolean flag - however, auto-disposing should ultimately be disabled by default (in order to prevent the Socket-Exhaustion issue). And in the Control's Help-Text the Socket-Exhaustion-Problem should maybe get referenced in the infotext of that AutoDisposeHttpClient -Property
[Which would only be required if the Consumer did not supply his own HttpClient instance, which can have rate-limiting integrated etc. and which should be disposed by himself and not the control then]