mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-07-08 12:24:48 +08:00
fix(go-models): harden ModelScope default transport handling (#15339)
## Summary - Harden `NewModelScopeModel` 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 `TestModelScopeNewModelWithCustomDefaultTransport` regression coverage. Co-authored-by: Cursor <cursoragent@cursor.com> Co-authored-by: Jin Hai <haijin.chn@gmail.com>
This commit is contained in:
@@ -72,7 +72,15 @@ type modelscopeModelListResponse struct {
|
||||
|
||||
// NewModelScopeModel creates a new ModelScope model instance.
|
||||
func NewModelScopeModel(baseURL map[string]string, urlSuffix URLSuffix) *ModelScopeModel {
|
||||
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
|
||||
|
||||
@@ -26,6 +26,12 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type roundTripperFunc func(*http.Request) (*http.Response, error)
|
||||
|
||||
func (f roundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) {
|
||||
return f(r)
|
||||
}
|
||||
|
||||
func newModelScopeForTest(baseURL string) *ModelScopeModel {
|
||||
return NewModelScopeModel(
|
||||
map[string]string{"default": baseURL},
|
||||
@@ -79,6 +85,20 @@ func TestModelScopeFactoryRoute(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestModelScopeNewModelWithCustomDefaultTransport(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 := NewModelScopeModel(map[string]string{"default": "http://unused"}, URLSuffix{}); model == nil {
|
||||
t.Fatal("NewModelScopeModel returned nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestModelScopeChatHappyPathNormalizesBaseURLAndOmitsEmptyAuth(t *testing.T) {
|
||||
var seen map[string]interface{}
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
Reference in New Issue
Block a user