Skip to content

GODRIVER-3373: Use non-default global DefaultTransport for DefaultClient #2110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: release/2.2
Choose a base branch
from

Conversation

zhouselena
Copy link
Contributor

GODRIVER-3373

Summary

NewHTTPClient returns a globally defined DefaultHTTPClient that returns DefaultTransport or updates the transport if it is replaced with a non-default transport.

Background & Motivation

Users can now use non-default transports without panic.

@mongodb-drivers-pr-bot mongodb-drivers-pr-bot bot added the priority-3-low Low Priority PR for Review label Jun 21, 2025
Copy link
Contributor

API Change Report

No changes found!

@zhouselena zhouselena marked this pull request as ready for review June 23, 2025 15:47

// NewHTTPClient will return the globally-defined DefaultHTTPClient, updating
// the transport if it differs from the http package DefaultTransport.
func NewHTTPClient() *http.Client {
Copy link
Member

@prestonvasquez prestonvasquez Jun 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The purpose of this change is to avoid cloning http.DefaultTransport.(*http.Transport) if a user changes the global http.DefaultTransport which would otherwise cause a panic when asserting to clone:

http.DefaultTransport.(*http.Transport).Clone()
panic: interface conversion: http.RoundTripper is *loghttp.Transport, not *http.Transport

goroutine 1 [running]:
go.mongodb.org/mongo-driver/v2/internal/httputil.init()
        /Users/preston.vasquez/Developer/mongo-go-driver/internal/httputil/httputil.go:15 +0x84
exit status 2

The current solution avoids this panic but doesn't set the default transport. You can create a small test in the options package to prove this:

func TestClientHTTPTransport(t *testing.T) {
	http.DefaultTransport = &nonDefaultTransport{}
	got := Client().HTTPClient.Transport 
	
	// Assert that got is a nonDefaultTransport
}

The fix is to use httputil. NewHTTPClient() as the HTTP Client when constructing ClientOptions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority-3-low Low Priority PR for Review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants