Skip to content

Commit d8d1cef

Browse files
committed
fix(chatbot): add LLM HTTP timeout, shared transport, retry backoff
- Add 120s timeout to OpenAI HTTP client (was unbounded) - Share global http.Transport with connection pooling (was per-adapter) - Add idle connection timeout (90s) to transport - Add retry backoff (1s × attempt) to AutoCorrectGenerate loop
1 parent 04860d1 commit d8d1cef

2 files changed

Lines changed: 25 additions & 6 deletions

File tree

internal/admin/chatbot/client.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"net/http"
1414
"os"
1515
"strings"
16+
"time"
1617
)
1718

1819
// Message represents a single turn in a conversation.
@@ -39,7 +40,16 @@ type openAIAdapter struct {
3940
client *http.Client
4041
}
4142

42-
const openAIChatCompletionsPath = "/v1/chat/completions"
43+
const (
44+
openAIChatCompletionsPath = "/v1/chat/completions"
45+
defaultLLMTimeout = 120 * time.Second
46+
defaultLLMIdleTimeout = 90 * time.Second
47+
)
48+
49+
var defaultLLMTransport = &http.Transport{
50+
MaxIdleConns: 10,
51+
IdleConnTimeout: defaultLLMIdleTimeout,
52+
}
4353

4454
// openAIStreamResponse represents a single SSE chunk returned by the server.
4555
type openAIStreamResponse struct {
@@ -64,18 +74,24 @@ func NewOpenAIAdapter(endpoint, apiKey, model string, temperature float64) LLMCl
6474
slog.Warn("CHATBOT_INSECURE_TLS is enabled — TLS certificate verification is disabled for LLM API calls")
6575
}
6676

67-
transport := &http.Transport{
68-
TLSClientConfig: &tls.Config{
69-
InsecureSkipVerify: insecureSkipVerify,
70-
},
77+
transport := defaultLLMTransport
78+
if insecureSkipVerify {
79+
transport = &http.Transport{
80+
TLSClientConfig: &tls.Config{
81+
InsecureSkipVerify: true,
82+
},
83+
}
7184
}
7285

7386
return &openAIAdapter{
7487
endpoint: endpoint,
7588
apiKey: apiKey,
7689
model: model,
7790
temp: temperature,
78-
client: &http.Client{Transport: transport},
91+
client: &http.Client{
92+
Transport: transport,
93+
Timeout: defaultLLMTimeout,
94+
},
7995
}
8096
}
8197

internal/admin/chatbot/validator.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"log/slog"
88
"strings"
9+
"time"
910

1011
corev1 "k8s.io/api/core/v1"
1112
discoveryv1 "k8s.io/api/discovery/v1"
@@ -173,6 +174,8 @@ func AutoCorrectGenerate(
173174
}
174175

175176
for attempt := 1; attempt <= maxRetries; attempt++ {
177+
time.Sleep(time.Duration(attempt) * time.Second)
178+
176179
feedback := fmt.Sprintf(correctionPrompt, err.Error())
177180

178181
correctedResponse, retryErr := collectFullStreamingResponse(ctx, llm, feedback, history)

0 commit comments

Comments
 (0)