Skip to content

Commit ea4b543

Browse files
author
iwysiu
committed
GODRIVER-1162 create driver.Expirable interface
Change-Id: I1fe083c9e66d34091677cad60370e70ef4718a7c
1 parent bab1629 commit ea4b543

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

x/mongo/driver/driver.go

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

33+
// Expirable represents an expirable object.
34+
type Expirable interface {
35+
Expire() error
36+
Alive() bool
37+
}
38+
3339
// Compressor is an interface used to compress wire messages. If a Connection supports compression
3440
// it should implement this interface as well. The CompressWireMessage method will be called during
3541
// the execution of an operation if the wire message is allowed to be compressed.

x/mongo/driver/topology/connection.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,8 @@ func (c initConnection) ReadWireMessage(ctx context.Context, dst []byte) ([]byte
281281
return c.readWireMessage(ctx, dst)
282282
}
283283

284-
// Connection implements the driver.Connection interface. It allows reading and writing wire
285-
// messages.
284+
// Connection implements the driver.Connection interface to allow reading and writing wire
285+
// messages and the driver.Expirable interface to allow expiring.
286286
type Connection struct {
287287
*connection
288288
s *Server
@@ -291,6 +291,7 @@ type Connection struct {
291291
}
292292

293293
var _ driver.Connection = (*Connection)(nil)
294+
var _ driver.Expirable = (*Connection)(nil)
294295

295296
// WriteWireMessage handles writing a wire message to the underlying connection.
296297
func (c *Connection) WriteWireMessage(ctx context.Context, wm []byte) error {
@@ -388,6 +389,29 @@ func (c *Connection) Close() error {
388389
return nil
389390
}
390391

392+
// Expire closes this connection and will close the underlying socket.
393+
func (c *Connection) Expire() error {
394+
c.mu.Lock()
395+
defer c.mu.Unlock()
396+
if c.connection == nil {
397+
return nil
398+
}
399+
if c.s != nil {
400+
c.s.sem.Release(1)
401+
}
402+
err := c.close()
403+
if err != nil {
404+
return err
405+
}
406+
c.connection = nil
407+
return nil
408+
}
409+
410+
// Alive returns if the connection is still alive.
411+
func (c *Connection) Alive() bool {
412+
return c.connection != nil
413+
}
414+
391415
// ID returns the ID of this connection.
392416
func (c *Connection) ID() string {
393417
c.mu.RLock()

x/mongo/driver/topology/connection_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,17 @@ func TestConnection(t *testing.T) {
310310
t.Errorf("errors do not match. got %v; want %v", got, want)
311311
}
312312

313+
got = conn.Expire()
314+
if !cmp.Equal(got, want, cmp.Comparer(compareErrors)) {
315+
t.Errorf("errors do not match. got %v; want %v", got, want)
316+
}
317+
318+
want = false
319+
got = conn.Alive()
320+
if !cmp.Equal(got, want) {
321+
t.Errorf("Alive does not match. got %v; want %v", got, want)
322+
}
323+
313324
want = "<closed>"
314325
got = conn.ID()
315326
if !cmp.Equal(got, want) {

0 commit comments

Comments
 (0)