-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #697 from batazor/optimization_write_to_store
Banch package
- Loading branch information
Showing
27 changed files
with
731 additions
and
559 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
.* | ||
docs | ||
github.com | ||
initialState | ||
*.yml | ||
*.yaml | ||
*.md | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package batch | ||
|
||
import ( | ||
"context" | ||
"time" | ||
) | ||
|
||
// TODO: add config for as timeout, retries, etc... | ||
// New - create a new batch | ||
func New(ctx context.Context, cb func([]*Item) interface{}) (*Config, error) { | ||
cnf := Config{ | ||
cb: cb, | ||
Interval: time.Millisecond * 100, | ||
} | ||
|
||
// run background job | ||
go cnf.run(ctx) | ||
|
||
return &cnf, nil | ||
} | ||
|
||
func (c *Config) Push(item interface{}) (chan interface{}, error) { | ||
// create new item | ||
el := NewItem(item) | ||
|
||
c.mx.Lock() | ||
c.items = append(c.items, el) | ||
c.mx.Unlock() | ||
|
||
return el.CB, nil | ||
} | ||
|
||
// run - starts a loop flushing at the Interval | ||
func (c *Config) run(ctx context.Context) { | ||
ticker := time.NewTicker(c.Interval) | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
for key := range c.items { | ||
c.items[key].CB <- "ctx close" | ||
} | ||
|
||
break | ||
case <-ticker.C: | ||
c.mx.Lock() | ||
|
||
// skip if items empty | ||
if len(c.items) > 0 { | ||
// apply func for all items | ||
c.cb(c.items) | ||
|
||
// clear items | ||
c.items = []*Item{} | ||
} | ||
|
||
c.mx.Unlock() | ||
} | ||
} | ||
} | ||
|
||
func NewItem(item interface{}) *Item { | ||
cb := make(chan interface{}) | ||
return &Item{ | ||
CB: cb, | ||
Item: item, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package batch | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
//func TestMain(m *testing.M) { | ||
// goleak.VerifyTestMain(m) | ||
//} | ||
|
||
func TestNew(t *testing.T) { | ||
t.Run("Create new a batch", func(t *testing.T) { | ||
// Add events | ||
wg := sync.WaitGroup{} | ||
|
||
ctx := context.Background() | ||
ctx, cancel := context.WithCancel(ctx) | ||
aggrCB := func(args []*Item) interface{} { | ||
// Get string | ||
for _, item := range args { | ||
//time.Sleep(time.Second * 2) // Emulate long work | ||
|
||
item.CB <- item.Item.(string) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
b, err := New(ctx, aggrCB) | ||
assert.Nil(t, err) | ||
|
||
request := []string{"A", "B", "C", "D"} | ||
for key := range request { | ||
wg.Add(1) | ||
res, err := b.Push(request[key]) | ||
assert.Nil(t, err) | ||
go func(key int) { | ||
assert.Equal(t, <-res, request[key]) | ||
wg.Done() | ||
}(key) | ||
} | ||
|
||
time.Sleep(time.Second * 1) | ||
cancel() | ||
for key := range request { | ||
wg.Add(1) | ||
res, err := b.Push(request[key]) | ||
assert.Nil(t, err) | ||
go func() { | ||
assert.Equal(t, <-res, "ctx close") | ||
wg.Done() | ||
}() | ||
} | ||
wg.Wait() | ||
}) | ||
} |
Oops, something went wrong.