Skip to content

Commit b3dc788

Browse files
committed
Simplify interface of ClientOptions.
1 parent 689f931 commit b3dc788

File tree

4 files changed

+17
-31
lines changed

4 files changed

+17
-31
lines changed

mongo/options/clientoptions.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -328,22 +328,6 @@ func (c *ClientOptions) validate() error {
328328
return nil
329329
}
330330

331-
// GetRawHosts returns the raw hosts. If ApplyURI was not called during construction, this returns a nil.
332-
func (c *ClientOptions) GetRawHosts() []string {
333-
if c.cs == nil {
334-
return nil
335-
}
336-
return c.cs.RawHosts
337-
}
338-
339-
// GetScheme returns the scheme. If ApplyURI was not called during construction, this returns "".
340-
func (c *ClientOptions) GetScheme() string {
341-
if c.cs == nil {
342-
return ""
343-
}
344-
return c.cs.Scheme
345-
}
346-
347331
// GetURI returns the original URI used to configure the ClientOptions instance. If ApplyURI was not called during
348332
// construction, this returns "".
349333
func (c *ClientOptions) GetURI() string {

x/mongo/driver/topology/topology.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ type Topology struct {
8787
rescanSRVInterval time.Duration
8888
pollHeartbeatTime atomic.Value // holds a bool
8989

90+
hosts []string
91+
9092
updateCallback updateTopologyCallback
9193
fsm *fsm
9294

@@ -152,7 +154,14 @@ func New(cfg *Config) (*Topology, error) {
152154
return t.apply(context.Background(), desc)
153155
}
154156

155-
t.pollingRequired = (t.cfg.Scheme == connstring.SchemeMongoDBSRV) && !t.cfg.LoadBalanced
157+
if t.cfg.URI != "" {
158+
connStr, err := connstring.Parse(t.cfg.URI)
159+
if err != nil {
160+
return nil, err
161+
}
162+
t.pollingRequired = (connStr.Scheme == connstring.SchemeMongoDBSRV) && !t.cfg.LoadBalanced
163+
t.hosts = connStr.RawHosts
164+
}
156165

157166
t.publishTopologyOpeningEvent()
158167

@@ -346,20 +355,20 @@ func (t *Topology) Connect() error {
346355

347356
t.serversLock.Unlock()
348357
if mustLogTopologyMessage(t, logger.LevelInfo) {
349-
logTopologyThirdPartyUsage(t, t.cfg.Hosts)
358+
logTopologyThirdPartyUsage(t, t.hosts)
350359
}
351360
if t.pollingRequired {
352361
// sanity check before passing the hostname to resolver
353-
if len(t.cfg.Hosts) != 1 {
362+
if len(t.hosts) != 1 {
354363
return fmt.Errorf("URI with SRV must include one and only one hostname")
355364
}
356-
_, _, err = net.SplitHostPort(t.cfg.Hosts[0])
365+
_, _, err = net.SplitHostPort(t.hosts[0])
357366
if err == nil {
358367
// we were able to successfully extract a port from the host,
359368
// but should not be able to when using SRV
360369
return fmt.Errorf("URI with srv must not include a port number")
361370
}
362-
go t.pollSRVRecords(t.cfg.Hosts[0])
371+
go t.pollSRVRecords(t.hosts[0])
363372
t.pollingwg.Add(1)
364373
}
365374

x/mongo/driver/topology/topology_options.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ type Config struct {
3131
ReplicaSetName string
3232
SeedList []string
3333
ServerOpts []ServerOption
34-
Scheme string
35-
Hosts []string
34+
URI string
3635
ServerSelectionTimeout time.Duration
3736
ServerMonitor *event.ServerMonitor
3837
SRVMaxHosts int
@@ -102,8 +101,7 @@ func NewConfig(co *options.ClientOptions, clock *session.ClusterClock) (*Config,
102101
}))
103102
}
104103

105-
cfgp.Hosts = co.GetRawHosts()
106-
cfgp.Scheme = co.GetScheme()
104+
cfgp.URI = co.GetURI()
107105

108106
if co.SRVServiceName != nil {
109107
cfgp.SRVServiceName = *co.SRVServiceName

x/mongo/driver/topology/topology_test.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -656,15 +656,11 @@ func TestTopologyConstruction(t *testing.T) {
656656
testCases := []struct {
657657
name string
658658
uri string
659-
scheme string
660-
hosts []string
661659
pollingRequired bool
662660
}{
663661
{
664662
name: "normal",
665663
uri: "mongodb://localhost:27017",
666-
scheme: "mongodb",
667-
hosts: []string{"localhost:27017"},
668664
pollingRequired: false,
669665
},
670666
}
@@ -676,8 +672,7 @@ func TestTopologyConstruction(t *testing.T) {
676672
topo, err := New(cfg)
677673
assert.Nil(t, err, "topology.New error: %v", err)
678674

679-
assert.Equal(t, tc.scheme, topo.cfg.Scheme, "expected topology scheme to be %v, got %v", tc.scheme, topo.cfg.Scheme)
680-
assert.Equal(t, tc.hosts, topo.cfg.Hosts, "expected topology hosts to be %v, got %v", tc.hosts, topo.cfg.Hosts)
675+
assert.Equal(t, tc.uri, topo.cfg.URI, "expected topology URI to be %v, got %v", tc.uri, topo.cfg.URI)
681676
assert.Equal(t, tc.pollingRequired, topo.pollingRequired,
682677
"expected topo.pollingRequired to be %v, got %v", tc.pollingRequired, topo.pollingRequired)
683678
})

0 commit comments

Comments
 (0)