Do queues a task to be run by a TransactionWriter. The function provided will be ran within a transaction as supplied by the txn parameter if one is supplied, and if not, will take out a new transaction from the database supplied in the database parameter. Either way, this will block until the task
(db *sql.DB, txn *sql.Tx, f func(txn *sql.Tx) error)
| 36 | // new transaction from the database supplied in the database |
| 37 | // parameter. Either way, this will block until the task is done. |
| 38 | func (w *ExclusiveWriter) Do(db *sql.DB, txn *sql.Tx, f func(txn *sql.Tx) error) error { |
| 39 | if w.todo == nil { |
| 40 | return errors.New("not initialised") |
| 41 | } |
| 42 | if !w.running.Load() { |
| 43 | go w.run() |
| 44 | } |
| 45 | task := transactionWriterTask{ |
| 46 | db: db, |
| 47 | txn: txn, |
| 48 | f: f, |
| 49 | wait: make(chan error, 1), |
| 50 | } |
| 51 | w.todo <- task |
| 52 | return <-task.wait |
| 53 | } |
| 54 | |
| 55 | // run processes the tasks for a given transaction writer. Only one |
| 56 | // of these goroutines will run at a time. A transaction will be |