Mongo counter fix

This commit is contained in:
alin104n
2017-03-20 20:39:55 +02:00
parent ce2dfabf03
commit 5dd96f2a09
3 changed files with 7 additions and 4 deletions

View File

@@ -112,7 +112,7 @@ func NewMongoStorage(host, port, db, user, pass, storageType string, cdrsIndexes
return nil, err
}
}
ms.cnter = utils.NewCounterGen(1000)
ms.cnter = utils.NewCounterGen(0)
return
}

View File

@@ -883,7 +883,7 @@ func (ms *MongoStorage) GetSMCosts(cgrid, runid, originHost, originIDPrefix stri
func (ms *MongoStorage) SetCDR(cdr *CDR, allowUpdate bool) (err error) {
if cdr.OrderID == 0 {
cdr.OrderID = time.Now().UnixNano() + ms.cnter.Gen()
cdr.OrderID = ms.cnter.Gen()
}
session, col := ms.conn(utils.TBL_CDRS)
defer session.Close()

View File

@@ -41,7 +41,10 @@ import (
)
func NewCounterGen(limit int64) *CounterGen {
return &CounterGen{limit: limit}
return &CounterGen{
cnt: time.Now().UnixNano(),
limit: limit,
}
}
type CounterGen struct {
@@ -53,7 +56,7 @@ func (cg *CounterGen) Gen() int64 {
cg.Lock()
defer cg.Unlock()
cg.cnt += 1
if cg.cnt > cg.limit {
if cg.limit > 0 && cg.cnt > cg.limit {
cg.cnt = 0
}
return cg.cnt