-
Notifications
You must be signed in to change notification settings - Fork 893
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
GODRIVER-2620 Fix hostname parsing for SRV polling. #1112
Changes from 1 commit
cdaa715
7ad50fe
e675205
5b180ca
19a4154
eae97a7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -14,6 +14,8 @@ import ( | |||||
"context" | ||||||
"errors" | ||||||
"fmt" | ||||||
"net" | ||||||
"net/url" | ||||||
"strings" | ||||||
"sync" | ||||||
"sync/atomic" | ||||||
|
@@ -234,7 +236,22 @@ func (t *Topology) Connect() error { | |||||
|
||||||
t.serversLock.Unlock() | ||||||
if t.pollingRequired { | ||||||
go t.pollSRVRecords() | ||||||
uri, err := url.Parse(t.cfg.URI) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm concerned we don't have enough test coverage of MongoDB connection string formats in the SRV polling tests to assert that it's safe to use Is every There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The package doc says There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good. I also tested out using |
||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
// sanity check before passing the hostname to resolver | ||||||
parsedHosts := strings.Split(uri.Host, ",") | ||||||
if len(parsedHosts) != 1 { | ||||||
return fmt.Errorf("URI with SRV must include one and only one hostname") | ||||||
} | ||||||
_, _, err = net.SplitHostPort(parsedHosts[0]) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
[optional]: IMO this code reads better by using the uri to get the host, not the slice used in the sanity check. |
||||||
if err == nil { | ||||||
// we were able to successfully extract a port from the host, | ||||||
// but should not be able to when using SRV | ||||||
return fmt.Errorf("URI with srv must not include a port number") | ||||||
} | ||||||
go t.pollSRVRecords(parsedHosts[0]) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This refactor is a great idea! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
[optional]: IMO this code reads better by using the uri to get the host, not the slice used in the sanity check. |
||||||
t.pollingwg.Add(1) | ||||||
} | ||||||
|
||||||
|
@@ -556,7 +573,7 @@ func (t *Topology) selectServerFromDescription(desc description.Topology, | |||||
return suitable, nil | ||||||
} | ||||||
|
||||||
func (t *Topology) pollSRVRecords() { | ||||||
func (t *Topology) pollSRVRecords(hosts string) { | ||||||
defer t.pollingwg.Done() | ||||||
|
||||||
serverConfig := newServerConfig(t.cfg.ServerOpts...) | ||||||
|
@@ -573,13 +590,6 @@ func (t *Topology) pollSRVRecords() { | |||||
} | ||||||
}() | ||||||
|
||||||
// remove the scheme | ||||||
uri := t.cfg.URI[14:] | ||||||
hosts := uri | ||||||
if idx := strings.IndexAny(uri, "/?@"); idx != -1 { | ||||||
hosts = uri[:idx] | ||||||
} | ||||||
|
||||||
for { | ||||||
select { | ||||||
case <-pollTicker.C: | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only testing with a URI containing a username/password may cause the same lack of test coverage as there is currently, but for URIs with no username/password.
Is there a way we can test with multiple URI formats? Could we include the URI in the test struct and add test cases for both with and without username/password?