Skip to content

Commit

Permalink
update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
nikandfor committed Jan 5, 2024
1 parent 4ae750b commit c4a7e8b
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,28 @@ This is all without timeouts, additional goroutines, allocations, and channels.
## Usage

```
var tx int
var sum int
bc := batch.Controller{
bc := batch.Controller[int]{
// Required
Commit: func(ctx context.Context) (interface{}, error) {
// commit tx
return res, err
Commit: func(ctx context.Context) (int, error) {
// commit sum
return sum, err
},
}
for j := 0; j < N; j++ {
go func(j int) {
ctx := context.WithValue(ctx, workerID{}, j) // can be accessed in Controller.Commit
ctx := context.WithValue(ctx, workerID{}, j) // can be obtained in Controller.Commit
b := bc.Enter()
b, i := bc.Enter(true)
defer b.Exit()
if b.Index() == 0 { // we are first in batch, reset it
tx = 0
if i == 0 { // we are first in batch, reset it
sum = 0 // or you can do in in the end of commit function
}
tx++ // add work to the batch
sum++ // add work to the batch
res, err := b.Commit(ctx)
if err != nil { // works the same as we had independent commit in each goroutine
Expand All @@ -72,10 +72,10 @@ b.QueueUp() // now we will be waited for.
// prepare data
x := 3
b.Enter() // enter syncronized section
b.Enter(true) // enter syncronized section
// add data to a common batch
tx += x
sum += x
_, _ = b.Commit(ctx)
Expand Down

0 comments on commit c4a7e8b

Please sign in to comment.