Skip to content

Commit aa5b2d9

Browse files
authored
GODRIVER-3399: PoolClearedError should have TransientTransactionError label appended to it (#2114)
1 parent 6598290 commit aa5b2d9

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

x/mongo/driver/topology/pool.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,19 @@ func (p *pool) checkOut(ctx context.Context) (conn *connection, err error) {
503503
}
504504
return nil, ErrPoolClosed
505505
case poolPaused:
506-
err := poolClearedError{err: p.lastClearErr, address: p.address}
506+
// Wrap poolCleared in a driver.Error so we can add the
507+
// "TransientTransactionError" label. This will add
508+
// "TransientTransactionError" to all poolClearedError instances, not
509+
// just those that happened during transactions. While that behavior is
510+
// different than other places we add "TransientTransactionError", it is
511+
// consistent with the Transactions specification and simplifies the
512+
// code.
513+
pcErr := poolClearedError{err: p.lastClearErr, address: p.address}
514+
err := driver.Error{
515+
Message: pcErr.Error(),
516+
Labels: []string{driver.TransientTransactionError},
517+
Wrapped: pcErr,
518+
}
507519
p.stateMu.RUnlock()
508520

509521
duration := time.Since(start)

x/mongo/driver/topology/pool_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"go.mongodb.org/mongo-driver/v2/internal/eventtest"
2222
"go.mongodb.org/mongo-driver/v2/internal/require"
2323
"go.mongodb.org/mongo-driver/v2/mongo/address"
24+
"go.mongodb.org/mongo-driver/v2/x/mongo/driver"
2425
"go.mongodb.org/mongo-driver/v2/x/mongo/driver/operation"
2526
)
2627

@@ -1584,3 +1585,26 @@ func TestPool_PoolMonitor(t *testing.T) {
15841585
"expected ConnectionCheckOutFailed Duration to be set")
15851586
})
15861587
}
1588+
1589+
func TestPool_Error(t *testing.T) {
1590+
t.Parallel()
1591+
1592+
t.Run("should have TransientTransactionError", func(t *testing.T) {
1593+
t.Parallel()
1594+
1595+
p := newPool(poolConfig{})
1596+
assert.Equalf(t, poolPaused, p.getState(), "expected new pool to be paused")
1597+
1598+
// Since new pool is paused, checkout should throw PoolClearedError.
1599+
_, err := p.checkOut(context.Background())
1600+
var le driver.Error
1601+
if errors.As(err, &le) {
1602+
assert.ErrorIs(t, poolClearedError{}, le.Unwrap(), "expect error to be PoolClearedError")
1603+
assert.True(t, le.HasErrorLabel(driver.TransientTransactionError), `expected error to include the "TransientTransactionError" label`)
1604+
} else {
1605+
t.Errorf("expected labeled error, got %v", err)
1606+
}
1607+
1608+
p.close(context.Background())
1609+
})
1610+
}

0 commit comments

Comments
 (0)