Skip to content

Commit 49b74f6

Browse files
committed
Add LocalAddresser interface
GODRIVER-1209 Change-Id: I4d16f802f401ed4e0609888ac73cb66f393d3431
1 parent daeee5f commit 49b74f6

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

x/mongo/driver/driver.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ type Connection interface {
3030
Address() address.Address
3131
}
3232

33+
// LocalAddresser is a type that is able to supply its local address
34+
type LocalAddresser interface {
35+
LocalAddress() address.Address
36+
}
37+
3338
// Expirable represents an expirable object.
3439
type Expirable interface {
3540
Expire() error

x/mongo/driver/topology/connection.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,12 @@ func (c initConnection) Description() description.Server { return description.Se
272272
func (c initConnection) Close() error { return nil }
273273
func (c initConnection) ID() string { return c.id }
274274
func (c initConnection) Address() address.Address { return c.addr }
275+
func (c initConnection) LocalAddress() address.Address {
276+
if c.connection == nil || c.nc == nil {
277+
return address.Address("0.0.0.0")
278+
}
279+
return address.Address(c.nc.LocalAddr().String())
280+
}
275281
func (c initConnection) WriteWireMessage(ctx context.Context, wm []byte) error {
276282
return c.writeWireMessage(ctx, wm)
277283
}
@@ -430,6 +436,16 @@ func (c *Connection) Address() address.Address {
430436
return c.addr
431437
}
432438

439+
// LocalAddress returns the local address of the connection
440+
func (c *Connection) LocalAddress() address.Address {
441+
c.mu.RLock()
442+
defer c.mu.RUnlock()
443+
if c.connection == nil || c.nc == nil {
444+
return address.Address("0.0.0.0")
445+
}
446+
return address.Address(c.nc.LocalAddr().String())
447+
}
448+
433449
var notMasterCodes = []int32{10107, 13435}
434450
var recoveringCodes = []int32{11600, 11602, 13436, 189, 91}
435451

x/mongo/driver/topology/connection_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,12 @@ func TestConnection(t *testing.T) {
332332
if !cmp.Equal(got, want) {
333333
t.Errorf("Addresses do not match. got %v; want %v", got, want)
334334
}
335+
336+
want = address.Address("0.0.0.0")
337+
got = conn.LocalAddress()
338+
if !cmp.Equal(got, want) {
339+
t.Errorf("LocalAddresses do not match. got %v; want %v", got, want)
340+
}
335341
})
336342
})
337343
}

0 commit comments

Comments
 (0)