Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
26 changes: 26 additions & 0 deletions connect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"github.com/stretchr/testify/require"
"testing"
"time"
)

func TestConnectInvalidURLs(t *testing.T) {
Expand All @@ -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)
}
27 changes: 21 additions & 6 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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
}