Merge pull request #249 from terinjokes/terin/mutex-open-from-conn-pool
fix(stdlib): lock openFromConnPoolCount while using
This commit is contained in:
+9
-1
@@ -49,11 +49,15 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"sync"
|
||||
|
||||
"github.com/jackc/pgx"
|
||||
)
|
||||
|
||||
var openFromConnPoolCount int
|
||||
var (
|
||||
openFromConnPoolCountMu sync.Mutex
|
||||
openFromConnPoolCount int
|
||||
)
|
||||
|
||||
// oids that map to intrinsic database/sql types. These will be allowed to be
|
||||
// binary, anything else will be forced to text format
|
||||
@@ -115,8 +119,12 @@ func (d *Driver) Open(name string) (driver.Conn, error) {
|
||||
// pool connection size must be at least 2.
|
||||
func OpenFromConnPool(pool *pgx.ConnPool) (*sql.DB, error) {
|
||||
d := &Driver{Pool: pool}
|
||||
|
||||
openFromConnPoolCountMu.Lock()
|
||||
name := fmt.Sprintf("pgx-%d", openFromConnPoolCount)
|
||||
openFromConnPoolCount++
|
||||
openFromConnPoolCountMu.Unlock()
|
||||
|
||||
sql.Register(name, d)
|
||||
db, err := sql.Open(name, "")
|
||||
if err != nil {
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"database/sql"
|
||||
"github.com/jackc/pgx"
|
||||
"github.com/jackc/pgx/stdlib"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -163,6 +164,43 @@ func TestOpenFromConnPool(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpenFromConnPoolRace(t *testing.T) {
|
||||
wg := &sync.WaitGroup{}
|
||||
connConfig := pgx.ConnConfig{
|
||||
Host: "127.0.0.1",
|
||||
User: "pgx_md5",
|
||||
Password: "secret",
|
||||
Database: "pgx_test",
|
||||
}
|
||||
|
||||
config := pgx.ConnPoolConfig{ConnConfig: connConfig}
|
||||
pool, err := pgx.NewConnPool(config)
|
||||
if err != nil {
|
||||
t.Fatalf("Unable to create connection pool: %v", err)
|
||||
}
|
||||
defer pool.Close()
|
||||
|
||||
wg.Add(10)
|
||||
for i := 0; i < 10; i++ {
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
db, err := stdlib.OpenFromConnPool(pool)
|
||||
if err != nil {
|
||||
t.Fatalf("Unable to create connection pool: %v", err)
|
||||
}
|
||||
defer closeDB(t, db)
|
||||
|
||||
// Can get pgx.ConnPool from driver
|
||||
driver := db.Driver().(*stdlib.Driver)
|
||||
if driver.Pool == nil {
|
||||
t.Fatal("Expected driver opened through OpenFromConnPool to have Pool, but it did not")
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func TestStmtExec(t *testing.T) {
|
||||
db := openDB(t)
|
||||
defer closeDB(t, db)
|
||||
|
||||
Reference in New Issue
Block a user