From 9530d7fa4c34de05b123b65069bd6c8b195180e1 Mon Sep 17 00:00:00 2001 From: Gaspard Douady Date: Mon, 20 Nov 2017 14:00:27 +0100 Subject: [PATCH] ConnPool begin should not retry if ctx is done --- conn_pool.go | 2 +- conn_pool_test.go | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/conn_pool.go b/conn_pool.go index a09fcde9..6ca0ee01 100644 --- a/conn_pool.go +++ b/conn_pool.go @@ -519,7 +519,7 @@ func (p *ConnPool) BeginEx(ctx context.Context, txOptions *TxOptions) (*Tx, erro // again on a new connection would fix, so just return the error. But // if the connection is dead try to acquire a new connection and try // again. - if alive { + if alive || ctx.Err() != nil { return nil, err } continue diff --git a/conn_pool_test.go b/conn_pool_test.go index 174b8379..84a74aed 100644 --- a/conn_pool_test.go +++ b/conn_pool_test.go @@ -1066,3 +1066,18 @@ func TestConnPoolBeginBatch(t *testing.T) { t.Fatal(err) } } + +func TestConnPoolBeginEx(t *testing.T) { + t.Parallel() + + pool := createConnPool(t, 2) + defer pool.Close() + + ctx, cancel := context.WithCancel(context.Background()) + cancel() + + tx, err := pool.BeginEx(ctx, nil) + if err == nil || tx != nil { + t.Fatal("Should not be able to create a tx") + } +}