Documentation
¶
Index ¶
- func ContextWithTx(ctx context.Context, tx TxInterface) context.Context
- func Offset(pageParam int64, pageSize uint64) uint64
- func PageSize(sizeParam int64) uint64
- func StreamReplicaRoutingInterceptor() grpc.StreamServerInterceptor
- func TransactionMiddleware(db DBClientInterface, logger logging.LoggerInterface) func(http.Handler) http.Handler
- func UnaryReplicaRoutingInterceptor() grpc.UnaryServerInterceptor
- func WithReadOnly(ctx context.Context) context.Context
- type Config
- type DBClient
- func (d *DBClient) BeginTx(ctx context.Context) (context.Context, TxInterface, error)
- func (d *DBClient) Close()
- func (d *DBClient) Statement(ctx context.Context) sq.StatementBuilderType
- func (d *DBClient) TxStatement(ctx context.Context) (TxInterface, sq.StatementBuilderType, error)
- func (d *DBClient) WithTx(ctx context.Context, fn func(context.Context) error) error
- type DBClientInterface
- type LazyTxContextKey
- type ReadOnlyContextKey
- type TxContextKey
- type TxInterface
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ContextWithTx ¶
func ContextWithTx(ctx context.Context, tx TxInterface) context.Context
ContextWithTx returns a new context with the transaction attached.
func Offset ¶
Offset calculates the offset for pagination based on the provided page parameter and page size.
func PageSize ¶
PageSize calculates the page size for pagination based on the provided size parameter.
func StreamReplicaRoutingInterceptor ¶ added in v1.4.0
func StreamReplicaRoutingInterceptor() grpc.StreamServerInterceptor
StreamReplicaRoutingInterceptor routes read-only streaming RPCs to the read-only replica database pool, provided that no active transaction exists in the context.
func TransactionMiddleware ¶
func TransactionMiddleware(db DBClientInterface, logger logging.LoggerInterface) func(http.Handler) http.Handler
TransactionMiddleware creates a middleware that wraps each request in a database transaction. The transaction is committed if the handler completes successfully (status < 400). The transaction is rolled back if the handler returns an error or status >= 400.
func UnaryReplicaRoutingInterceptor ¶ added in v1.4.0
func UnaryReplicaRoutingInterceptor() grpc.UnaryServerInterceptor
UnaryReplicaRoutingInterceptor routes read-only unary RPCs to the read-only replica database pool, provided that no active transaction exists in the context.
Types ¶
type Config ¶
type Config struct {
DSN string
MaxConns int32
MinConns int32
MaxConnLifetime time.Duration
MaxConnIdleTime time.Duration
TracingEnabled bool
ReplicaDSN string
ReplicaMaxConns int32
ReplicaMinConns int32
ReplicaMaxConnLifetime time.Duration
ReplicaMaxConnIdleTime time.Duration
MaxReplicaLagMs int64
ReplicaPoolSizeMultiplier float64
}
type DBClient ¶
type DBClient struct {
// contains filtered or unexported fields
}
func NewDBClient ¶
func NewDBClient(cfg Config, tracer tracing.TracingInterface, monitor monitoring.MonitorInterface, logger logging.LoggerInterface) (*DBClient, error)
NewDBClient creates a new DBClient instance with the provided DSN and configuration options.
func (*DBClient) BeginTx ¶
BeginTx starts a new transaction and returns a context with the transaction attached.
func (*DBClient) Statement ¶
func (d *DBClient) Statement(ctx context.Context) sq.StatementBuilderType
Statement provides a StatementBuilderType configured to use the DBClient's database connection. If a transaction exists in the context, it will be used (created lazily on first use).
func (*DBClient) TxStatement ¶
func (d *DBClient) TxStatement(ctx context.Context) (TxInterface, sq.StatementBuilderType, error)
TxStatement provides a StatementBuilderType configured to use a transaction.
func (*DBClient) WithTx ¶
WithTx executes a function within a transaction context. The transaction is created lazily on first database access. If the function returns an error, the transaction is rolled back. Otherwise, the transaction is committed. If no database operations occurred, no transaction is created or committed.
type DBClientInterface ¶
type LazyTxContextKey ¶
type LazyTxContextKey struct{}
type ReadOnlyContextKey ¶ added in v1.4.0
type ReadOnlyContextKey struct{}
type TxContextKey ¶
type TxContextKey struct{}
type TxInterface ¶
type TxInterface interface {
Commit() error
Rollback() error
sq.BaseRunner
}
func TxFromContext ¶
func TxFromContext(ctx context.Context) TxInterface
TxFromContext extracts a transaction from the context, returning nil if none exists.