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

@@ -16,10 +16,10 @@ type Request[Q any] struct {
// NOTE: This field is optional.
Reset func()
// Update is applied alongside other operations in the batch.
// Do is applied alongside other operations in the batch.
//
// NOTE: This method MUST NOT acquire any mutexes.
Update func(tx Q) error
Do func(tx Q) error
// OnCommit is called if the batch or a subset of the batch including
// this request all succeeded without failure. The passed error should
@@ -36,12 +36,17 @@ type SchedulerOptions struct {
// request when it comes in. This means that it can be scheduled later,
// allowing larger batches.
Lazy bool
// ReadOnly should be true if the request is read-only. By default,
// this is false.
ReadOnly bool
}
// NewDefaultSchedulerOpts returns a new SchedulerOptions with default values.
func NewDefaultSchedulerOpts() *SchedulerOptions {
return &SchedulerOptions{
Lazy: false,
Lazy: false,
ReadOnly: false,
}
}
@@ -68,6 +73,16 @@ func LazyAdd() SchedulerOption {
}
}
// ReadOnly will mark the request as read-only. This means that the
// transaction will be executed in read-only mode, and no changes will be
// made to the database. If any requests in the same batch are not read-only,
// then the entire batch will be executed in read-write mode.
func ReadOnly() SchedulerOption {
return func(opts *SchedulerOptions) {
opts.ReadOnly = true
}
}
// Scheduler abstracts a generic batching engine that accumulates an incoming
// set of Requests, executes them, and returns the error from the operation.
type Scheduler[Q any] interface {