diff --git a/README.md b/README.md index 4a5e3b0..5a2eddf 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,10 @@ func instanceCount(ctx context.Context, databaseURL, projectID string) (int, err } ``` +`Open` returns the same pool without the ping, for callers that must start +before the database is reachable; the pool connects on first use. It does not +fail fast during an outage, so give each query a context deadline. + Existing `*sql.DB` and `*sql.Tx` values can also be passed to service constructors. Generated query sets support `WithTx`. Callers own pools, deadlines, region/cell selection, credentials, and authorization. Use database accounts with SELECT access. diff --git a/connect_test.go b/connect_test.go index fd293f1..d8b240c 100644 --- a/connect_test.go +++ b/connect_test.go @@ -4,6 +4,7 @@ import ( "context" "github.com/stretchr/testify/require" "testing" + "time" ) func TestConnectInvalidURLs(t *testing.T) { @@ -22,3 +23,28 @@ func TestConnectInvalidOptions(t *testing.T) { _, err := ConnectContext(context.Background(), "mysql://user:pass@localhost/db", ConnectionOptions{MaxOpenConns: -1}) require.Error(t, err) } +func TestOpenInvalidURLs(t *testing.T) { + for _, u := range []string{"", "postgresql://user:pass@localhost/db"} { + _, err := Open(u, ConnectionOptions{}) + require.Error(t, err) + } +} +func TestOpenInvalidOptions(t *testing.T) { + _, err := Open("mysql://user:pass@localhost/db", ConnectionOptions{MaxOpenConns: -1}) + require.Error(t, err) +} +func TestOpenUnreachableServer(t *testing.T) { + db, err := Open("mysql://user:pass@127.0.0.1:1/db", ConnectionOptions{}) + require.NoError(t, err) + defer db.Close() + require.Equal(t, 5, db.Stats().MaxOpenConnections) + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + require.Error(t, db.PingContext(ctx)) +} +func TestOpenOptions(t *testing.T) { + db, err := Open("mysql://user:pass@localhost/db", ConnectionOptions{MaxOpenConns: 7}) + require.NoError(t, err) + defer db.Close() + require.Equal(t, 7, db.Stats().MaxOpenConnections) +} diff --git a/db.go b/db.go index 30ef2c3..3731330 100644 --- a/db.go +++ b/db.go @@ -24,9 +24,28 @@ func Connect(connectionString string) (*sql.DB, error) { return ConnectContext(ctx, connectionString, ConnectionOptions{}) } -// ConnectContext opens a pool from an oslo.db-style MySQL URL and verifies it -// within ctx's deadline. It does not grant or enforce database permissions. +// ConnectContext opens a pool with Open and verifies it within ctx's deadline. +// It does not grant or enforce database permissions. func ConnectContext(ctx context.Context, connectionString string, opts ConnectionOptions) (*sql.DB, error) { + db, err := Open(connectionString, opts) + if err != nil { + return nil, err + } + if err := db.PingContext(ctx); err != nil { + _ = db.Close() + return nil, fmt.Errorf("failed to ping database: %w", err) + } + return db, nil +} + +// Open creates a pool from an oslo.db-style MySQL URL without contacting the +// server. The pool connects on first use and again after a lost connection, so +// a caller can hold a pool for a database that is not reachable yet. +// The pool does not fail fast while the database is unreachable: Open adds +// no dial timeout of its own, so bound each call with a context deadline, for +// example with QueryContext or PingContext. +// The caller owns the returned pool and must close it. +func Open(connectionString string, opts ConnectionOptions) (*sql.DB, error) { if opts.MaxOpenConns < 0 || opts.MaxIdleConns < 0 || opts.ConnMaxLifetime < 0 { return nil, fmt.Errorf("connection options must not be negative") } @@ -47,9 +66,5 @@ func ConnectContext(ctx context.Context, connectionString string, opts Connectio db.SetMaxOpenConns(opts.MaxOpenConns) db.SetMaxIdleConns(opts.MaxIdleConns) db.SetConnMaxLifetime(opts.ConnMaxLifetime) - if err := db.PingContext(ctx); err != nil { - _ = db.Close() - return nil, fmt.Errorf("failed to ping database: %w", err) - } return db, nil }