batch: update to allow for read-only calls

In this commit, we update the batch schedular so that it has the ability
to do read-only calls. It will do a best effort attempt at keeping a
transaction in read-only mode and then if any requests get added to a
batch that require a read-write tx, then the entire batch's tx will be
upgraded to use a read-write tx.
This commit is contained in:
Elle Mouton
2025-05-22 09:46:57 +02:00
parent e743878bd5
commit df1e6da94e
6 changed files with 332 additions and 25 deletions

View File

@@ -26,7 +26,16 @@ func (t *BoltBatcher[Q]) ExecTx(_ context.Context, opts sqldb.TxOptions,
txBody func(Q) error, reset func()) error {
if opts.ReadOnly() {
return fmt.Errorf("read-only transactions not supported")
return kvdb.View(t.db, func(tx kvdb.RTx) error {
q, ok := any(tx).(Q)
if !ok {
return fmt.Errorf("unable to cast tx(%T) "+
"into the type expected by the "+
"BoltBatcher(%T)", tx, t)
}
return txBody(q)
}, reset)
}
return kvdb.Update(t.db, func(tx kvdb.RwTx) error {