| package processinglock |
|
|
| import ( |
| "context" |
| "database/sql" |
| "sync" |
|
|
| "github.com/target/goalert/util/sqlutil" |
| ) |
|
|
| |
| type Conn struct { |
| l *Lock |
| conn *sql.Conn |
| mx sync.Mutex |
| } |
|
|
| |
| |
| |
| func (l *Lock) Conn(ctx context.Context) (*Conn, error) { |
| c, err := l.db.Conn(ctx) |
| if err != nil { |
| return nil, err |
| } |
| _, err = c.ExecContext(ctx, `SET idle_in_transaction_session_timeout = 3000`) |
| if err != nil { |
| _ = c.Close() |
| return nil, err |
| } |
|
|
| _, err = c.ExecContext(ctx, `SET lock_timeout = 8000`) |
| if err != nil { |
| _ = c.Close() |
| return nil, err |
| } |
|
|
| return &Conn{l: l, conn: c}, nil |
| } |
|
|
| |
| func (c *Conn) BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error) { |
| return c.l._BeginTx(ctx, c.conn, opts, false) |
| } |
|
|
| |
| func (c *Conn) WithTx(ctx context.Context, txFn func(tx *sql.Tx) error) error { |
| c.mx.Lock() |
| defer c.mx.Unlock() |
| tx, err := c.l._BeginTx(ctx, c.conn, nil, false) |
| if err != nil { |
| return err |
| } |
| defer sqlutil.Rollback(ctx, "rollback tx", tx) |
|
|
| err = txFn(tx) |
| if err != nil { |
| return err |
| } |
|
|
| return tx.Commit() |
| } |
|
|
| |
| func (c *Conn) Exec(ctx context.Context, stmt *sql.Stmt, args ...interface{}) (sql.Result, error) { |
| c.mx.Lock() |
| defer c.mx.Unlock() |
| return c.l._Exec(ctx, c.conn, stmt, args...) |
| } |
|
|
| |
| func (c *Conn) ExecWithoutLock(ctx context.Context, query string, args ...interface{}) (sql.Result, error) { |
| c.mx.Lock() |
| defer c.mx.Unlock() |
| return c.conn.ExecContext(ctx, query, args...) |
| } |
|
|
| |
| func (c *Conn) Close() { _ = c.conn.Close() } |
|
|