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:
glorydavid03023
2026-05-28 06:41:11 -05:00
committed by GitHub
parent 0a7662cf3e
commit 7fc909acc9
2 changed files with 29 additions and 1 deletions

View File

@@ -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

View File

@@ -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) {