Skip to content

GODRIVER-3092 Fix UNIX socket in URL parsing. #1521

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

Merged
merged 7 commits into from
Feb 7, 2024

Conversation

qingyang-hu
Copy link
Collaborator

@qingyang-hu qingyang-hu commented Jan 17, 2024

GODRIVER-3092

Summary

  • Fix bug in parsing "mongodb://%2Ftmp%2Fmongodb-27017.sock/" by discarding the url.Parse() call.
  • Add test case
  • Refactor ConnString

Background & Motivation

Add RawHosts to ConnString, so we can initial Topology with scheme and hosts parsed by ConnString from the URI.
The RawHosts is directly parsed from the URI string before SRV lookup.

@qingyang-hu qingyang-hu requested a review from a team as a code owner January 17, 2024 20:31
@qingyang-hu qingyang-hu requested review from blink1073, matthewdale and prestonvasquez and removed request for a team and blink1073 January 17, 2024 20:31
Copy link
Contributor

mongodb-drivers-pr-bot bot commented Jan 17, 2024

API Change Report

./x/mongo/driver/connstring

incompatible changes

Parse: changed from func(string) (ConnString, error) to func(string) (*ConnString, error)
ParseAndValidate: changed from func(string) (ConnString, error) to func(string) (*ConnString, error)

compatible changes

ConnString.RawHosts: added

@qingyang-hu qingyang-hu marked this pull request as draft January 17, 2024 20:57
@@ -248,7 +248,7 @@ func ExampleEncoder_IntMinSize() {
panic(err)
}

enc, err := bson.NewEncoder(vw)
enc := bson.NewEncoder(vw)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

To fix the build.

@@ -237,7 +237,6 @@ type ClientOptions struct {
ZstdLevel *int

err error
uri string
cs *connstring.ConnString
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Remove the uri so the ConnString.Original is the single point of truth.

@qingyang-hu qingyang-hu marked this pull request as ready for review January 18, 2024 04:15
@qingyang-hu qingyang-hu changed the title Godriver3092 GODRIVER-3092 Fix UNIX socket in URL parsing. Jan 18, 2024
Comment on lines 191 to 193
func (u ConnString) String() string {
return u.Original
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

The ConnString struct consumes almost 1kb per copy. We should always be passing it by pointer for that reason (including the return value of Parse and ParseAndValidate).

}
host, err := url.QueryUnescape(host)
if err != nil {
return fmt.Errorf("invalid host %q: %w", host, err)
return host, fmt.Errorf("invalid host %q: %w", host, err)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Go functions conventionally return the zero value if returning an error. Is there a reason to return host instead of ""?

Comment on lines 817 to 819
host, err := url.QueryUnescape(host)
if err != nil {
return fmt.Errorf("invalid host %q: %w", host, err)
return host, fmt.Errorf("invalid host %q: %w", host, err)
Copy link
Collaborator

Choose a reason for hiding this comment

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

url.QueryUnescape returns empty string on error. As a result, the error returned here will always have an empty host string. What we want instead is the original, invalid host value.

E.g.

unescaped, err := url.QueryUnescape(host)
if err != nil {
	return "", fmt.Errorf("error query unescaping host %q: %w", host, err)

Comment on lines 332 to 340
func (c *ClientOptions) GetRawHosts() []string {
if c.cs == nil {
return nil
}
return c.cs.RawHosts
}

// GetScheme returns the scheme. If ApplyURI was not called during construction, this returns "".
func (c *ClientOptions) GetScheme() string {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm concerned about exposing these values piecemeal in the ClientOptions API. The pattern isn't sustainable if we need to continue to expose more connection string or other config data to users.

Is there another way to pass the data to topology.New without having to add methods to ClientOptions?

Copy link
Collaborator

@matthewdale matthewdale left a comment

Choose a reason for hiding this comment

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

Looks good! 👍

@qingyang-hu qingyang-hu merged commit af11d4f into mongodb:master Feb 7, 2024
qingyang-hu added a commit to qingyang-hu/mongo-go-driver that referenced this pull request Feb 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants