Skip to content

Commit 7e6b981

Browse files
committed
Create Batch [Logic]
1 parent af8503d commit 7e6b981

File tree

1 file changed

+12
-25
lines changed

1 file changed

+12
-25
lines changed

producer.go

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import (
77
log "github.com/sirupsen/logrus"
88
)
99

10-
var (
11-
itemCounter = 1
10+
var (
1211
DefaultMaxItems = uint64(100) // maximum no of items packed inside a Batch
1312
DefaultMaxWait = time.Duration(30) * time.Second //seconds
1413
DefaultBatchNo = int32(1)
@@ -74,24 +73,23 @@ func (p *BatchProducer) WatchProducer() {
7473
case item := <-p.Watcher:
7574

7675
item.BatchNo = int(p.getBatchNo())
77-
p.Log.WithFields(log.Fields{"Id": item.Id, "BatchNo": item.BatchNo, "Item": item.Item}).Info("BatchProducer")
78-
79-
items = append(items, *item)
80-
itemCounter++
81-
if p.isBatchReady() {
82-
p.Log.WithFields(log.Fields{"Item Size": len(items), "MaxItems": p.MaxItems}).Warn("BatchReady")
83-
84-
itemCounter = 0
76+
p.Log.WithFields(log.Fields{"Id": item.Id, "Batch_Break": item.Id / int(p.MaxItems), "BatchNo": item.BatchNo, "Item": item.Item}).Info("BatchProducer")
77+
78+
items = append(items, *item)
79+
80+
if (item.Id / int(p.MaxItems)) == item.BatchNo {
81+
p.Log.WithFields(log.Fields{"Item Size": len(items), "MaxItems": p.MaxItems}).Warn("BatchReady")
8582
items = p.releaseBatch(items)
83+
p.createBatchNo()
8684
}
87-
85+
8886
case <-time.After(p.MaxWait):
8987
p.Log.WithFields(log.Fields{"Items": len(items)}).Warn("MaxWait")
9088

9189
if len(items) == 0 {
9290
return
9391
}
94-
itemCounter = 0
92+
9593
items = p.releaseBatch(items)
9694
case <-p.Quit:
9795
p.Log.Warn("Quit BatchProducer")
@@ -128,23 +126,12 @@ func (p *BatchProducer) CheckRemainingItems(done chan bool) {
128126
done <- true
129127
}
130128

131-
// isBatchReady verfies that whether the batch ItemCounter++ increases to the MaxItems value
132-
// to create a Batch.
133-
func (p *BatchProducer) isBatchReady() bool {
134-
return uint64(itemCounter) >= p.MaxItems
135-
}
136-
137129
// addBatchNo will increases the current BatchNo to 1 atomically.
138-
func (p *BatchProducer) addBatchNo() {
130+
func (p *BatchProducer) createBatchNo() {
139131
atomic.AddInt32(&p.BatchNo, 1)
140132
}
141133

142134
// getBatchNo will get the current BatchNo from the atomic variable.
143135
func (p *BatchProducer) getBatchNo() int32 {
144-
145-
if itemCounter == 0 {
146-
p.addBatchNo()
147-
}
148-
149136
return atomic.LoadInt32(&p.BatchNo)
150-
}
137+
}

0 commit comments

Comments
 (0)