gsilvamartin / dotnet-gemini-sdk

.NET SDK for seamless and efficient integration with the Google Gemini
MIT License
43 stars 11 forks source link

Prevent connection pool starvation #21

Open theadibest2 opened 4 months ago

theadibest2 commented 4 months ago

I was looking for a valid C# SDK to use in our integration with Gemini and I found your solution. Right now it can be used for dev/testing but not for production from my point of view. One of the important problems that I found by looking into the code is the way the requests are performed. You are using HttpClient and you are doing a new instance every time ApiRequester is created.

 public class ApiRequester : IApiRequester
   {
       private readonly HttpClient _httpClient;

       public ApiRequester()
       {
          _httpClient = new HttpClient();
       }

This approach will lead to connection pool starvation. You should use IHttpClientFactory instead and maybe allow dependency injection for it . Or make HttpClient static inisde the class and configure PooledConnectionLifetime and in this way you are avoiding the problems on how many instances of ApiRequester are created.

You can read more about this problem here: https://learn.microsoft.com/en-us/dotnet/fundamentals/networking/http/httpclient-guidelines#recommended-use or https://devblogs.microsoft.com/azure-sdk/net-framework-connection-pool-limits/

I saw that you are making it AddSingleton in the services but is not enough, knowing that someone can invoke GeminiClient(GoogleGeminiConfig config) multiple times and here is build a new ApiRequester every time (is not making use of the singleton):

    public GeminiClient(GoogleGeminiConfig config)
    {
        _config = config;
        _apiRequester = new ApiRequester();
    }

Also in this context I suggest you increase the resilience of the connection by using Polly: https://learn.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/implement-http-call-retries-exponential-backoff-polly

I hope this SKD will improve in the future. U are doing a great job.

gsilvamartin commented 2 months ago

Hi @theadibest2 thanks for suggestion, i'll make a improvement to prevent it