2020-08-19 14:38:27 +00:00
|
|
|
package sqlutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"go.uber.org/atomic"
|
|
|
|
)
|
|
|
|
|
2020-08-21 09:42:08 +00:00
|
|
|
// ExclusiveWriter implements sqlutil.Writer.
|
|
|
|
// ExclusiveWriter allows queuing database writes so that you don't
|
2020-08-19 14:38:27 +00:00
|
|
|
// contend on database locks in, e.g. SQLite. Only one task will run
|
2020-08-21 09:42:08 +00:00
|
|
|
// at a time on a given ExclusiveWriter.
|
|
|
|
type ExclusiveWriter struct {
|
2020-08-19 14:38:27 +00:00
|
|
|
running atomic.Bool
|
|
|
|
todo chan transactionWriterTask
|
|
|
|
}
|
|
|
|
|
2020-08-21 09:42:08 +00:00
|
|
|
func NewExclusiveWriter() Writer {
|
|
|
|
return &ExclusiveWriter{
|
2020-08-19 14:38:27 +00:00
|
|
|
todo: make(chan transactionWriterTask),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// transactionWriterTask represents a specific task.
|
|
|
|
type transactionWriterTask struct {
|
|
|
|
db *sql.DB
|
|
|
|
txn *sql.Tx
|
|
|
|
f func(txn *sql.Tx) error
|
|
|
|
wait chan error
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 is done.
|
2020-08-21 09:42:08 +00:00
|
|
|
func (w *ExclusiveWriter) Do(db *sql.DB, txn *sql.Tx, f func(txn *sql.Tx) error) error {
|
2020-08-19 14:38:27 +00:00
|
|
|
if w.todo == nil {
|
|
|
|
return errors.New("not initialised")
|
|
|
|
}
|
|
|
|
if !w.running.Load() {
|
|
|
|
go w.run()
|
|
|
|
}
|
|
|
|
task := transactionWriterTask{
|
|
|
|
db: db,
|
|
|
|
txn: txn,
|
|
|
|
f: f,
|
|
|
|
wait: make(chan error, 1),
|
|
|
|
}
|
|
|
|
w.todo <- task
|
|
|
|
return <-task.wait
|
|
|
|
}
|
|
|
|
|
|
|
|
// run processes the tasks for a given transaction writer. Only one
|
|
|
|
// of these goroutines will run at a time. A transaction will be
|
|
|
|
// opened using the database object from the task and then this will
|
|
|
|
// be passed as a parameter to the task function.
|
2020-08-21 09:42:08 +00:00
|
|
|
func (w *ExclusiveWriter) run() {
|
2020-08-19 14:38:27 +00:00
|
|
|
if !w.running.CAS(false, true) {
|
|
|
|
return
|
|
|
|
}
|
2020-09-08 16:30:05 +00:00
|
|
|
if tracingEnabled {
|
|
|
|
gid := goid()
|
|
|
|
goidToWriter.Store(gid, w)
|
|
|
|
defer goidToWriter.Delete(gid)
|
|
|
|
}
|
|
|
|
|
2020-08-19 14:38:27 +00:00
|
|
|
defer w.running.Store(false)
|
|
|
|
for task := range w.todo {
|
2020-08-21 09:42:08 +00:00
|
|
|
if task.db != nil && task.txn != nil {
|
2020-08-19 14:38:27 +00:00
|
|
|
task.wait <- task.f(task.txn)
|
2020-08-21 09:42:08 +00:00
|
|
|
} else if task.db != nil && task.txn == nil {
|
2020-08-19 14:38:27 +00:00
|
|
|
task.wait <- WithTransaction(task.db, func(txn *sql.Tx) error {
|
|
|
|
return task.f(txn)
|
|
|
|
})
|
|
|
|
} else {
|
2020-08-21 09:42:08 +00:00
|
|
|
task.wait <- task.f(nil)
|
2020-08-19 14:38:27 +00:00
|
|
|
}
|
|
|
|
close(task.wait)
|
|
|
|
}
|
|
|
|
}
|