TwitchLib / TwitchLib.Api

API component of TwitchLib.
70 stars 84 forks source link

TwitchAPI should implement IDisposable / IAsyncDisposable to clean up HttpClient #368

Open danbopes opened 1 year ago

danbopes commented 1 year ago

As noted in the title. The TwitchAPI creates a handler http, which should be disposed of for short lived TwitchAPI uses.

Omsad commented 1 year ago

As far as I'm aware you should never dispose of an HttpClient explicitly as doing so will lead to a memory leak. See https://stackoverflow.com/questions/27732546/httpclienthandler-httpclient-memory-leak .

So if you were to make the TwitchAPI disposable, you would presumable want to clean up the HttpClient it uses internally which would then cause memory issues.

What should be changed instead is that an IHttpClientFactory should be used to create an IHttpClient instance, that way each TwitchAPI would get the same instance of the HttpClient and it would be re-used without issue.

This is what I do in my own program, e.g. I explicitly create a TwitchHttpClient for the TwitchAPI and the HttpClient the TwitchHttpClient receives is the same one every time.