mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-07-12 06:35:44 +08:00
fix(go-models): harden N1N default transport handling (#15351)
## Summary - Harden `NewN1NModel` to avoid panics when `http.DefaultTransport` is a custom non-`*http.Transport` RoundTripper. - Fallback to a safe transport (`ProxyFromEnvironment`) while preserving existing pooling/timeout settings. - Add `n1n_test.go` with coverage for name/factory plus `TestN1NNewModelWithCustomDefaultTransport`. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -54,7 +54,15 @@ type N1NModel struct {
|
||||
// long-lived SSE streams in ChatStreamlyWithSender. Non-streaming
|
||||
// callers wrap each request with context.WithTimeout instead.
|
||||
func NewN1NModel(baseURL map[string]string, urlSuffix URLSuffix) *N1NModel {
|
||||
transport := http.DefaultTransport.(*http.Transport).Clone()
|
||||
defaultTransport, ok := http.DefaultTransport.(*http.Transport)
|
||||
var transport *http.Transport
|
||||
if ok {
|
||||
transport = defaultTransport.Clone()
|
||||
} else {
|
||||
transport = &http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
}
|
||||
}
|
||||
transport.MaxIdleConns = 100
|
||||
transport.MaxIdleConnsPerHost = 10
|
||||
transport.IdleConnTimeout = 90 * time.Second
|
||||
|
||||
49
internal/entity/models/n1n_test.go
Normal file
49
internal/entity/models/n1n_test.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type roundTripperFunc func(*http.Request) (*http.Response, error)
|
||||
|
||||
func (f roundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) {
|
||||
return f(r)
|
||||
}
|
||||
|
||||
func newN1NForTest(baseURL string) *N1NModel {
|
||||
return NewN1NModel(
|
||||
map[string]string{"default": baseURL},
|
||||
URLSuffix{Chat: "chat/completions", Models: "models"},
|
||||
)
|
||||
}
|
||||
|
||||
func TestN1NName(t *testing.T) {
|
||||
if got := newN1NForTest("http://unused").Name(); got != "n1n" {
|
||||
t.Errorf("Name()=%q, want %q", got, "n1n")
|
||||
}
|
||||
}
|
||||
|
||||
func TestN1NFactory(t *testing.T) {
|
||||
driver, err := NewModelFactory().CreateModelDriver("n1n", map[string]string{"default": "http://unused"}, URLSuffix{})
|
||||
if err != nil {
|
||||
t.Fatalf("CreateModelDriver: %v", err)
|
||||
}
|
||||
if _, ok := driver.(*N1NModel); !ok {
|
||||
t.Fatalf("driver type=%T, want *N1NModel", driver)
|
||||
}
|
||||
}
|
||||
|
||||
func TestN1NNewModelWithCustomDefaultTransport(t *testing.T) {
|
||||
original := http.DefaultTransport
|
||||
http.DefaultTransport = roundTripperFunc(func(*http.Request) (*http.Response, error) {
|
||||
return nil, nil
|
||||
})
|
||||
t.Cleanup(func() {
|
||||
http.DefaultTransport = original
|
||||
})
|
||||
|
||||
if model := NewN1NModel(map[string]string{"default": "http://unused"}, URLSuffix{}); model == nil {
|
||||
t.Fatal("NewN1NModel returned nil")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user