You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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();
}
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.
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):
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.
The text was updated successfully, but these errors were encountered: