Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func OpenConnection ¶
func OpenConnection(ctx context.Context, logContext, dsn string, maxConns, maxIdleConns int) (*sql.DB, error)
OpenConnection extracts the driver name from the DSN (expected as the URI scheme), adjusts it where necessary (e.g. some driver supported DSN formats don't include a scheme), opens a DB handle ensuring early termination if the context is closed (this is actually prevented by `database/sql` implementation), sets connection limits and returns the handle.
Below is the list of supported databases (with built in drivers) and their DSN formats. Unfortunately there is no dynamic way of loading a third party driver library (as e.g. with Java classpaths), so any driver additions require a binary rebuild.
MySQL ¶
Using the https://github.com/go-sql-driver/mysql driver, DSN format (passed to the driver stripped of the `mysql://` prefix):
mysql://username:password@protocol(host:port)/dbname?param=value
PostgreSQL ¶
Using the https://godoc.org/github.com/lib/pq driver, DSN format (passed through to the driver unchanged):
postgres://username:password@host:port/dbname?param=value
MS SQL Server ¶
Using the https://github.com/denisenkom/go-mssqldb driver, DSN format (passed through to the driver unchanged):
sqlserver://username:password@host:port/instance?param=value
Clickhouse ¶
Using the https://github.com/kshvakov/clickhouse driver, DSN format (passed to the driver with the`clickhouse://` prefix replaced with `tcp://`):
clickhouse://host:port?username=username&password=password&database=dbname¶m=value
func PingDB ¶
PingDB is a wrapper around sql.DB.PingContext() that terminates as soon as the context is closed.
sql.DB does not actually pass along the context to the driver when opening a connection (which always happens if the database is down) and the driver uses an arbitrary timeout which may well be longer than ours. So we run the ping call in a goroutine and terminate immediately if the context is closed.
Types ¶
This section is empty.