Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent connection pool starvation #21

Open
theadibest2 opened this issue May 28, 2024 · 1 comment
Open

Prevent connection pool starvation #21

theadibest2 opened this issue May 28, 2024 · 1 comment
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@theadibest2
Copy link

theadibest2 commented May 28, 2024

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
Copy link
Owner

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

@gsilvamartin gsilvamartin self-assigned this Jul 29, 2024
@gsilvamartin gsilvamartin added enhancement New feature or request good first issue Good for newcomers labels Jul 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

When branches are created from issues, their pull requests are automatically linked.

2 participants