Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
361dca5c85
|
|||
9f85a243e8
|
|||
dc6cb274ee
|
@@ -20,6 +20,7 @@ Potentially needs `export GOPRIVATE="gogs.mikescher.com"`
|
||||
| zipext | Mike | Utility for zip/gzip/tar etc |
|
||||
| reflectext | Mike | Utility for golang reflection |
|
||||
| fsext | Mike | Utility for filesytem access |
|
||||
| ctxext | Mike | Utility for context.Context |
|
||||
| | | |
|
||||
| mongoext | Mike | Utility/Helper functions for mongodb (kinda abandoned) |
|
||||
| cursortoken | Mike | MongoDB cursortoken implementation |
|
||||
|
27
ctxext/getter.go
Normal file
27
ctxext/getter.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package ctxext
|
||||
|
||||
import "context"
|
||||
|
||||
func Value[T any](ctx context.Context, key any) (T, bool) {
|
||||
v := ctx.Value(key)
|
||||
if v == nil {
|
||||
return *new(T), false
|
||||
}
|
||||
if tv, ok := v.(T); !ok {
|
||||
return *new(T), false
|
||||
} else {
|
||||
return tv, true
|
||||
}
|
||||
}
|
||||
|
||||
func ValueOrDefault[T any](ctx context.Context, key any, def T) T {
|
||||
v := ctx.Value(key)
|
||||
if v == nil {
|
||||
return def
|
||||
}
|
||||
if tv, ok := v.(T); !ok {
|
||||
return def
|
||||
} else {
|
||||
return tv
|
||||
}
|
||||
}
|
@@ -1,5 +1,5 @@
|
||||
package goext
|
||||
|
||||
const GoextVersion = "0.0.518"
|
||||
const GoextVersion = "0.0.521"
|
||||
|
||||
const GoextVersionTimestamp = "2024-10-05T00:45:55+0200"
|
||||
const GoextVersionTimestamp = "2024-10-05T01:06:36+0200"
|
||||
|
@@ -61,7 +61,7 @@ func (db *database) Exec(ctx context.Context, sqlstr string, prep PP) (sql.Resul
|
||||
|
||||
t0 := time.Now()
|
||||
|
||||
preMeta := PreExecMeta{}
|
||||
preMeta := PreExecMeta{Context: ctx, TransactionConstructorContext: nil}
|
||||
for _, v := range db.lstr {
|
||||
err := v.PreExec(ctx, nil, &sqlstr, &prep, preMeta)
|
||||
if err != nil {
|
||||
@@ -73,7 +73,7 @@ func (db *database) Exec(ctx context.Context, sqlstr string, prep PP) (sql.Resul
|
||||
|
||||
res, err := db.db.NamedExecContext(ctx, sqlstr, prep)
|
||||
|
||||
postMeta := PostExecMeta{Init: t0, Start: t1, End: time.Now()}
|
||||
postMeta := PostExecMeta{Context: ctx, TransactionConstructorContext: nil, Init: t0, Start: t1, End: time.Now()}
|
||||
for _, v := range db.lstr {
|
||||
v.PostExec(nil, origsql, sqlstr, prep, postMeta)
|
||||
}
|
||||
@@ -90,7 +90,7 @@ func (db *database) Query(ctx context.Context, sqlstr string, prep PP) (*sqlx.Ro
|
||||
|
||||
t0 := time.Now()
|
||||
|
||||
preMeta := PreQueryMeta{}
|
||||
preMeta := PreQueryMeta{Context: ctx, TransactionConstructorContext: nil}
|
||||
for _, v := range db.lstr {
|
||||
err := v.PreQuery(ctx, nil, &sqlstr, &prep, preMeta)
|
||||
if err != nil {
|
||||
@@ -102,7 +102,7 @@ func (db *database) Query(ctx context.Context, sqlstr string, prep PP) (*sqlx.Ro
|
||||
|
||||
rows, err := sqlx.NamedQueryContext(ctx, db.db, sqlstr, prep)
|
||||
|
||||
postMeta := PostQueryMeta{Init: t0, Start: t1, End: time.Now()}
|
||||
postMeta := PostQueryMeta{Context: ctx, TransactionConstructorContext: nil, Init: t0, Start: t1, End: time.Now()}
|
||||
for _, v := range db.lstr {
|
||||
v.PostQuery(nil, origsql, sqlstr, prep, postMeta)
|
||||
}
|
||||
@@ -118,7 +118,7 @@ func (db *database) Ping(ctx context.Context) error {
|
||||
|
||||
t0 := time.Now()
|
||||
|
||||
preMeta := PrePingMeta{}
|
||||
preMeta := PrePingMeta{Context: ctx}
|
||||
for _, v := range db.lstr {
|
||||
err := v.PrePing(ctx, preMeta)
|
||||
if err != nil {
|
||||
@@ -130,7 +130,7 @@ func (db *database) Ping(ctx context.Context) error {
|
||||
|
||||
err := db.db.PingContext(ctx)
|
||||
|
||||
postMeta := PostPingMeta{Init: t0, Start: t1, End: time.Now()}
|
||||
postMeta := PostPingMeta{Context: ctx, Init: t0, Start: t1, End: time.Now()}
|
||||
for _, v := range db.lstr {
|
||||
v.PostPing(err, postMeta)
|
||||
}
|
||||
@@ -151,7 +151,7 @@ func (db *database) BeginTransaction(ctx context.Context, iso sql.IsolationLevel
|
||||
db.txctr += 1 // with overflow !
|
||||
db.lock.Unlock()
|
||||
|
||||
preMeta := PreTxBeginMeta{}
|
||||
preMeta := PreTxBeginMeta{Context: ctx}
|
||||
for _, v := range db.lstr {
|
||||
err := v.PreTxBegin(ctx, txid, preMeta)
|
||||
if err != nil {
|
||||
@@ -163,7 +163,7 @@ func (db *database) BeginTransaction(ctx context.Context, iso sql.IsolationLevel
|
||||
|
||||
xtx, err := db.db.BeginTxx(ctx, &sql.TxOptions{Isolation: iso})
|
||||
|
||||
postMeta := PostTxBeginMeta{Init: t0, Start: t1, End: time.Now()}
|
||||
postMeta := PostTxBeginMeta{Context: ctx, Init: t0, Start: t1, End: time.Now()}
|
||||
for _, v := range db.lstr {
|
||||
v.PostTxBegin(txid, err, postMeta)
|
||||
}
|
||||
@@ -172,7 +172,7 @@ func (db *database) BeginTransaction(ctx context.Context, iso sql.IsolationLevel
|
||||
return nil, exerr.Wrap(err, "Failed to start sql transaction").Build()
|
||||
}
|
||||
|
||||
return NewTransaction(xtx, txid, db), nil
|
||||
return newTransaction(ctx, xtx, txid, db), nil
|
||||
}
|
||||
|
||||
func (db *database) Exit() error {
|
||||
|
@@ -6,61 +6,78 @@ import (
|
||||
)
|
||||
|
||||
type PrePingMeta struct {
|
||||
Context context.Context
|
||||
}
|
||||
|
||||
type PreTxBeginMeta struct {
|
||||
Context context.Context
|
||||
ConstructorContext context.Context
|
||||
}
|
||||
|
||||
type PreTxCommitMeta struct {
|
||||
ConstructorContext context.Context
|
||||
}
|
||||
|
||||
type PreTxRollbackMeta struct {
|
||||
ConstructorContext context.Context
|
||||
}
|
||||
|
||||
type PreQueryMeta struct {
|
||||
Context context.Context
|
||||
TransactionConstructorContext context.Context
|
||||
}
|
||||
|
||||
type PreExecMeta struct {
|
||||
Context context.Context
|
||||
TransactionConstructorContext context.Context
|
||||
}
|
||||
|
||||
type PostPingMeta struct {
|
||||
Init time.Time
|
||||
Start time.Time
|
||||
End time.Time
|
||||
Context context.Context
|
||||
Init time.Time
|
||||
Start time.Time
|
||||
End time.Time
|
||||
}
|
||||
|
||||
type PostTxBeginMeta struct {
|
||||
Init time.Time
|
||||
Start time.Time
|
||||
End time.Time
|
||||
Context context.Context
|
||||
Init time.Time
|
||||
Start time.Time
|
||||
End time.Time
|
||||
}
|
||||
|
||||
type PostTxCommitMeta struct {
|
||||
Init time.Time
|
||||
Start time.Time
|
||||
End time.Time
|
||||
ExecCounter int
|
||||
QueryCounter int
|
||||
ConstructorContext context.Context
|
||||
Init time.Time
|
||||
Start time.Time
|
||||
End time.Time
|
||||
ExecCounter int
|
||||
QueryCounter int
|
||||
}
|
||||
|
||||
type PostTxRollbackMeta struct {
|
||||
Init time.Time
|
||||
Start time.Time
|
||||
End time.Time
|
||||
ExecCounter int
|
||||
QueryCounter int
|
||||
ConstructorContext context.Context
|
||||
Init time.Time
|
||||
Start time.Time
|
||||
End time.Time
|
||||
ExecCounter int
|
||||
QueryCounter int
|
||||
}
|
||||
|
||||
type PostQueryMeta struct {
|
||||
Init time.Time
|
||||
Start time.Time
|
||||
End time.Time
|
||||
Context context.Context
|
||||
TransactionConstructorContext context.Context
|
||||
Init time.Time
|
||||
Start time.Time
|
||||
End time.Time
|
||||
}
|
||||
|
||||
type PostExecMeta struct {
|
||||
Init time.Time
|
||||
Start time.Time
|
||||
End time.Time
|
||||
Context context.Context
|
||||
TransactionConstructorContext context.Context
|
||||
Init time.Time
|
||||
Start time.Time
|
||||
End time.Time
|
||||
}
|
||||
|
||||
type Listener interface {
|
||||
|
@@ -27,22 +27,24 @@ type Tx interface {
|
||||
}
|
||||
|
||||
type transaction struct {
|
||||
tx *sqlx.Tx
|
||||
id uint16
|
||||
status TxStatus
|
||||
execCtr int
|
||||
queryCtr int
|
||||
db *database
|
||||
constructorContext context.Context
|
||||
tx *sqlx.Tx
|
||||
id uint16
|
||||
status TxStatus
|
||||
execCtr int
|
||||
queryCtr int
|
||||
db *database
|
||||
}
|
||||
|
||||
func NewTransaction(xtx *sqlx.Tx, txid uint16, db *database) Tx {
|
||||
func newTransaction(ctx context.Context, xtx *sqlx.Tx, txid uint16, db *database) Tx {
|
||||
return &transaction{
|
||||
tx: xtx,
|
||||
id: txid,
|
||||
status: TxStatusInitial,
|
||||
execCtr: 0,
|
||||
queryCtr: 0,
|
||||
db: db,
|
||||
constructorContext: ctx,
|
||||
tx: xtx,
|
||||
id: txid,
|
||||
status: TxStatusInitial,
|
||||
execCtr: 0,
|
||||
queryCtr: 0,
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,7 +52,7 @@ func (tx *transaction) Rollback() error {
|
||||
|
||||
t0 := time.Now()
|
||||
|
||||
preMeta := PreTxRollbackMeta{}
|
||||
preMeta := PreTxRollbackMeta{ConstructorContext: tx.constructorContext}
|
||||
for _, v := range tx.db.lstr {
|
||||
err := v.PreTxRollback(tx.id, preMeta)
|
||||
if err != nil {
|
||||
@@ -66,7 +68,7 @@ func (tx *transaction) Rollback() error {
|
||||
tx.status = TxStatusRollback
|
||||
}
|
||||
|
||||
postMeta := PostTxRollbackMeta{Init: t0, Start: t1, End: time.Now(), ExecCounter: tx.execCtr, QueryCounter: tx.queryCtr}
|
||||
postMeta := PostTxRollbackMeta{ConstructorContext: tx.constructorContext, Init: t0, Start: t1, End: time.Now(), ExecCounter: tx.execCtr, QueryCounter: tx.queryCtr}
|
||||
for _, v := range tx.db.lstr {
|
||||
v.PostTxRollback(tx.id, result, postMeta)
|
||||
}
|
||||
@@ -78,7 +80,7 @@ func (tx *transaction) Commit() error {
|
||||
|
||||
t0 := time.Now()
|
||||
|
||||
preMeta := PreTxCommitMeta{}
|
||||
preMeta := PreTxCommitMeta{ConstructorContext: tx.constructorContext}
|
||||
for _, v := range tx.db.lstr {
|
||||
err := v.PreTxCommit(tx.id, preMeta)
|
||||
if err != nil {
|
||||
@@ -94,7 +96,7 @@ func (tx *transaction) Commit() error {
|
||||
tx.status = TxStatusComitted
|
||||
}
|
||||
|
||||
postMeta := PostTxCommitMeta{Init: t0, Start: t1, End: time.Now(), ExecCounter: tx.execCtr, QueryCounter: tx.queryCtr}
|
||||
postMeta := PostTxCommitMeta{ConstructorContext: tx.constructorContext, Init: t0, Start: t1, End: time.Now(), ExecCounter: tx.execCtr, QueryCounter: tx.queryCtr}
|
||||
for _, v := range tx.db.lstr {
|
||||
v.PostTxCommit(tx.id, result, postMeta)
|
||||
}
|
||||
@@ -107,7 +109,7 @@ func (tx *transaction) Exec(ctx context.Context, sqlstr string, prep PP) (sql.Re
|
||||
|
||||
t0 := time.Now()
|
||||
|
||||
preMeta := PreExecMeta{}
|
||||
preMeta := PreExecMeta{Context: ctx, TransactionConstructorContext: tx.constructorContext}
|
||||
for _, v := range tx.db.lstr {
|
||||
err := v.PreExec(ctx, langext.Ptr(tx.id), &sqlstr, &prep, preMeta)
|
||||
if err != nil {
|
||||
@@ -124,7 +126,7 @@ func (tx *transaction) Exec(ctx context.Context, sqlstr string, prep PP) (sql.Re
|
||||
tx.status = TxStatusActive
|
||||
}
|
||||
|
||||
postMeta := PostExecMeta{Init: t0, Start: t1, End: time.Now()}
|
||||
postMeta := PostExecMeta{Context: ctx, TransactionConstructorContext: tx.constructorContext, Init: t0, Start: t1, End: time.Now()}
|
||||
for _, v := range tx.db.lstr {
|
||||
v.PostExec(langext.Ptr(tx.id), origsql, sqlstr, prep, postMeta)
|
||||
}
|
||||
@@ -140,7 +142,7 @@ func (tx *transaction) Query(ctx context.Context, sqlstr string, prep PP) (*sqlx
|
||||
|
||||
t0 := time.Now()
|
||||
|
||||
preMeta := PreQueryMeta{}
|
||||
preMeta := PreQueryMeta{Context: ctx, TransactionConstructorContext: tx.constructorContext}
|
||||
for _, v := range tx.db.lstr {
|
||||
err := v.PreQuery(ctx, langext.Ptr(tx.id), &sqlstr, &prep, preMeta)
|
||||
if err != nil {
|
||||
@@ -157,7 +159,7 @@ func (tx *transaction) Query(ctx context.Context, sqlstr string, prep PP) (*sqlx
|
||||
tx.status = TxStatusActive
|
||||
}
|
||||
|
||||
postMeta := PostQueryMeta{Init: t0, Start: t1, End: time.Now()}
|
||||
postMeta := PostQueryMeta{Context: ctx, TransactionConstructorContext: tx.constructorContext, Init: t0, Start: t1, End: time.Now()}
|
||||
for _, v := range tx.db.lstr {
|
||||
v.PostQuery(langext.Ptr(tx.id), origsql, sqlstr, prep, postMeta)
|
||||
}
|
||||
|
Reference in New Issue
Block a user