Documentation
¶
Index ¶
- func DefaultConnectDBFunc(ctx context.Context, id interface{}, driverName, dataSourceName string, ...) (db *sqlx.DB, err error)
- type Group
- type Request
- type Store
- func New(ctx context.Context, driverName string, dataSourceName string, debug bool) (s *Store, err error)
- func NewWithConnectDBFuncAndTimeouts(ctx context.Context, ...) (s *Store, err error)
- func NewWithUnlockAndStatementTimeouts(ctx context.Context, driverName string, dataSourceName string, ...) (s *Store, err error)
- func (s *Store) RWGetDB(id interface{}, ctx context.Context, tag string) (cancel context.CancelFunc, db *sql.DB, err error)
- func (s *Store) RWGetDBWithTimeout(id interface{}, ctx context.Context, tag string, ...) (cancel context.CancelFunc, db *sql.DB, err error)
- func (s *Store) RWGetDBx(id interface{}, ctx context.Context, tag string) (cancel context.CancelFunc, db *sqlx.DB, err error)
- func (s *Store) RWGetDBxWithTimeout(id interface{}, ctx context.Context, tag string, ...) (cancel context.CancelFunc, db *sqlx.DB, err error)
- func (s *Store) ReadGetDB(id interface{}, ctx context.Context, tag string) (cancel context.CancelFunc, db *sql.DB, err error)
- func (s *Store) ReadGetDBx(id interface{}, ctx context.Context, tag string) (cancel context.CancelFunc, db *sqlx.DB, err error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultConnectDBFunc ¶
func DefaultConnectDBFunc(ctx context.Context, id interface{}, driverName, dataSourceName string, statementTimeout *time.Duration) (db *sqlx.DB, err error)
DefaultConnectDBFunc is the default function used to connecct to the database This default function has an unused id variable. This function could be customised, for example, to send requests to different database shards based on the provided id.
Types ¶
type Request ¶
type Request struct {
// contains filtered or unexported fields
}
Request is a database access request
type Store ¶
type Store struct {
sync.Mutex
Ctx context.Context
DriverName string
DataSourceName string
UnlockTimeout *time.Duration
StatementTimeout *time.Duration
// contains filtered or unexported fields
}
Store is the dblocker store
func New ¶
func New( ctx context.Context, driverName string, dataSourceName string, debug bool, ) (s *Store, err error)
New creates a new dblocker Store using the default connectDBFunc; with a default unlockTimeout for waiting for access to the database of 2 minutes, and with a default statemenTimeout for database sessions of 4 minutes (where the database supports statement timeouts)
func NewWithConnectDBFuncAndTimeouts ¶
func NewWithConnectDBFuncAndTimeouts( ctx context.Context, connectDBFunc func(ctx context.Context, id interface{}, driverName, dataSourceName string, statementTimeout *time.Duration) (db *sqlx.DB, err error), driverName string, dataSourceName string, unlockTimeout *time.Duration, statementTimeout *time.Duration, debug bool, ) (s *Store, err error)
NewWithConnectDBFuncAndTimeouts creates a new dblocker Store with a custom connectDBFunc (which can be used for database types not in the DefaultConnectDBFunc (i.e. sqlite, postgres, and mysql) and/or to shard requests by id for example); with an unlockTimeout for waiting for access to the database; and with a statemenTimeout for database sessions (returns an error if not nil and the database does not support statement timeouts).
func NewWithUnlockAndStatementTimeouts ¶
func NewWithUnlockAndStatementTimeouts( ctx context.Context, driverName string, dataSourceName string, unlockTimeout *time.Duration, statementTimeout *time.Duration, debug bool, ) (s *Store, err error)
NewWithUnlockAndStatementTimeouts creates a new dblocker Store using the default connectDBFunc; with an unlockTimeout for waiting for access to the database; and with a statemenTimeout for database sessions (returns an error if not nil and the database does not support statement timeouts).
func (*Store) RWGetDB ¶
func (s *Store) RWGetDB(id interface{}, ctx context.Context, tag string) (cancel context.CancelFunc, db *sql.DB, err error)
RWGetDB returns a shared copy of a database session (*sql.DB) for the specified id. RWGetDB acts like Lock() for a RWMutex for the specified id. All other RWGetDB, RWGetDBWithTimeout, and ReadDB function calls will wait for access to the database for the specified id until the returned cancel() function is called.
func (*Store) RWGetDBWithTimeout ¶
func (s *Store) RWGetDBWithTimeout(id interface{}, ctx context.Context, tag string, statementTimeout *time.Duration) (cancel context.CancelFunc, db *sql.DB, err error)
RWGetDBWithTimeout returns a new database session (*sql.DB) for the specified id with a custom session timeout. RWGetDBWithTimeout acts like Lock() for a RWMutex for the specified id. All other RWGetDB, RWGetDBWithTimeout, and ReadDB function calls will wait for access to the database for the specified id until the returned cancel() function is called.
func (*Store) RWGetDBx ¶
func (s *Store) RWGetDBx(id interface{}, ctx context.Context, tag string) (cancel context.CancelFunc, db *sqlx.DB, err error)
RWGetDB returns a shared copy of a database session (*sqlx.DB) for the specified id. github.com/jmoiron/sqlx is a library which provides a set of extensions on go's standard database/sql library. RWGetDB acts like Lock() for a RWMutex for the specified id. All other RWGetDB, RWGetDBWithTimeout, and ReadDB function calls will wait for access to the database for the specified id until the returned cancel() function is called.
func (*Store) RWGetDBxWithTimeout ¶
func (s *Store) RWGetDBxWithTimeout(id interface{}, ctx context.Context, tag string, statementTimeout *time.Duration) (cancel context.CancelFunc, db *sqlx.DB, err error)
RWGetDBWithTimeout returns a new database session (*sqlx.DB) for the specified id with a custom session timeout. github.com/jmoiron/sqlx is a library which provides a set of extensions on go's standard database/sql library. RWGetDBWithTimeout acts like Lock() for a RWMutex for the specified id. All other RWGetDB, RWGetDBWithTimeout, and ReadDB function calls will wait for access to the database for the specified id until the returned cancel() function is called.
func (*Store) ReadGetDB ¶
func (s *Store) ReadGetDB(id interface{}, ctx context.Context, tag string) (cancel context.CancelFunc, db *sql.DB, err error)
ReadDB returns a shared copy of a database session (*sql.DB) for the specified id. ReadDB acts like RLock() for a RWMutex for the specified id. Multiple ReadDB function calls can access the shared database at the same time. All RWGetDB and RWGetDBWithTimeout function calls will wait for access to the database for the specified id until the returned cancel() function is called.
func (*Store) ReadGetDBx ¶
func (s *Store) ReadGetDBx(id interface{}, ctx context.Context, tag string) (cancel context.CancelFunc, db *sqlx.DB, err error)
ReadDB returns a shared copy of a database session (*sqlx.DB) for the specified id. github.com/jmoiron/sqlx is a library which provides a set of extensions on go's standard database/sql library. ReadDB acts like RLock() for a RWMutex for the specified id. Multiple ReadDB function calls can access the shared database at the same time. All RWGetDB and RWGetDBWithTimeout function calls will wait for access to the database for the specified id until the returned cancel() function is called.