fix(ai): reuse a persistent http.Client in HttpApiClient - #18603
fix(ai): reuse a persistent http.Client in HttpApiClient#18603SelaseKay wants to merge 1 commit into
Conversation
Default generateContent calls used package:http helpers that create and close a Client per request, forcing a fresh TCP/TLS handshake every time. Fixes #18602
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. |
Description
HttpApiClientfell back tohttp.post/Request.send()when no client was injected. Those helpers create and close anhttp.Clientper call, which tears down the TCP/TLS connection and forces a fresh handshake on everygenerateContent.This keeps a long-lived
http.Clienton eachHttpApiClient(httpClient ?? http.Client()) so keep-alive reuse can work for the lifetime of the model. Injected clients are unchanged.idleTimeoutis still thedart:iodefault (15s); apps that need a longer pool can already pass a tunedIOClientviaFirebaseAI.generativeModel(httpClient: …).Related Issues
Checklist
///).melos run analyze) does not report any problems on my PR.Breaking Change
Test plan
flutter test test/client_test.dartinpackages/firebase_ai/firebase_ai(3 tests passed)generateContentstill works against a real backend