sqlconn

package
v1.89.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 3, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package sqlconn provides a simple way to connect to a SQL database and manage the connection. It also provides a way to perform a health check on the connection.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CheckConnectionFunc

type CheckConnectionFunc func(ctx context.Context, db *sql.DB) error

CheckConnectionFunc is the type of function called to perform a DB connection check.

type ConnectFunc

type ConnectFunc func(ctx context.Context, cfg *config) (*sql.DB, error)

ConnectFunc is the type of function called to perform the actual DB connection.

type Option

type Option func(*config)

Option is a type alias for a function that configures the DB connector.

func WithCheckConnectionFunc

func WithCheckConnectionFunc(fn CheckConnectionFunc) Option

WithCheckConnectionFunc replaces the default connection check function.

func WithConnMaxIdleCount

func WithConnMaxIdleCount(maxIdle int) Option

WithConnMaxIdleCount sets the maximum number of idle database connections.

func WithConnMaxIdleTime

func WithConnMaxIdleTime(t time.Duration) Option

WithConnMaxIdleTime sets the maximum idle time of a database connection.

func WithConnMaxLifetime

func WithConnMaxLifetime(t time.Duration) Option

WithConnMaxLifetime sets the maximum lifetime of a database connection.

func WithConnMaxOpen

func WithConnMaxOpen(maxOpen int) Option

WithConnMaxOpen sets the maximum number of open database connections.

func WithConnectFunc

func WithConnectFunc(fn ConnectFunc) Option

WithConnectFunc replaces the default connection function.

func WithDefaultDriver

func WithDefaultDriver(driver string) Option

WithDefaultDriver sets the default driver to use if not included in the DSN.

func WithPingTimeout

func WithPingTimeout(t time.Duration) Option

WithPingTimeout sets the healthcheck ping timeout.

func WithSQLOpenFunc

func WithSQLOpenFunc(fn SQLOpenFunc) Option

WithSQLOpenFunc replaces the default open database function.

func WithShutdownSignalChan added in v1.73.0

func WithShutdownSignalChan(ch chan struct{}) Option

WithShutdownSignalChan sets the shared channel uset to signal a shutdown. When the channel signal is received the database connection will be closed.

func WithShutdownWaitGroup added in v1.73.0

func WithShutdownWaitGroup(wg *sync.WaitGroup) Option

WithShutdownWaitGroup sets the shared waiting group to communicate externally when the databse connection is closed.

type SQLConn

type SQLConn struct {
	// contains filtered or unexported fields
}

SQLConn is the structure that helps to manage a SQL DB connection.

func Connect

func Connect(ctx context.Context, url string, opts ...Option) (*SQLConn, error)

Connect attempts to connect to a SQL database.

func (*SQLConn) DB

func (c *SQLConn) DB() *sql.DB

DB returns a database connection from the pool.

func (*SQLConn) HealthCheck

func (c *SQLConn) HealthCheck(ctx context.Context) error

HealthCheck performs a health check of the database connection.

func (*SQLConn) Shutdown added in v1.74.0

func (c *SQLConn) Shutdown(_ context.Context) error

Shutdown closes the database and prevents new queries from starting. It waits for all queries that have started processing on the server to finish.

type SQLOpenFunc

type SQLOpenFunc func(driverName, dataSourceName string) (*sql.DB, error)

SQLOpenFunc is the type of function called to open the DB. (Only for monkey patch testing).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL