interface.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package batch
  2. import "github.com/lightningnetwork/lnd/kvdb"
  3. // Request defines an operation that can be batched into a single bbolt
  4. // transaction.
  5. type Request struct {
  6. // Reset is called before each invocation of Update and is used to clear
  7. // any possible modifications to local state as a result of previous
  8. // calls to Update that were not committed due to a concurrent batch
  9. // failure.
  10. //
  11. // NOTE: This field is optional.
  12. Reset func()
  13. // Update is applied alongside other operations in the batch.
  14. //
  15. // NOTE: This method MUST NOT acquire any mutexes.
  16. Update func(tx kvdb.RwTx) error
  17. // OnCommit is called if the batch or a subset of the batch including
  18. // this request all succeeded without failure. The passed error should
  19. // contain the result of the transaction commit, as that can still fail
  20. // even if none of the closures returned an error.
  21. //
  22. // NOTE: This field is optional.
  23. OnCommit func(commitErr error) error
  24. // lazy should be true if we don't have to immediately execute this
  25. // request when it comes in. This means that it can be scheduled later,
  26. // allowing larger batches.
  27. lazy bool
  28. }
  29. // SchedulerOption is a type that can be used to supply options to a scheduled
  30. // request.
  31. type SchedulerOption func(r *Request)
  32. // LazyAdd will make the request be executed lazily, added to the next batch to
  33. // reduce db contention.
  34. func LazyAdd() SchedulerOption {
  35. return func(r *Request) {
  36. r.lazy = true
  37. }
  38. }
  39. // Scheduler abstracts a generic batching engine that accumulates an incoming
  40. // set of Requests, executes them, and returns the error from the operation.
  41. type Scheduler interface {
  42. // Execute schedules a Request for execution with the next available
  43. // batch. This method blocks until the underlying closure has been
  44. // run against the database. The resulting error is returned to the
  45. // caller.
  46. Execute(req *Request) error
  47. }