Merge pull request #651 from alin104n/tmp

OrderID counter
This commit is contained in:
Dan Christian Bogos
2017-03-20 15:51:56 +01:00
committed by GitHub
4 changed files with 25 additions and 3 deletions

View File

@@ -112,6 +112,7 @@ func NewMongoStorage(host, port, db, user, pass, storageType string, cdrsIndexes
return nil, err
}
}
ms.cnter = utils.NewCounterGen(1000)
return
}
@@ -123,6 +124,7 @@ type MongoStorage struct {
cacheCfg *config.CacheConfig
loadHistorySize int
cdrsIndexes []string
cnter *utils.CounterGen
}
func (ms *MongoStorage) conn(col string) (*mgo.Session, *mgo.Collection) {

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()
cdr.OrderID = time.Now().UnixNano() + ms.cnter.Gen()
}
session, col := ms.conn(utils.TBL_CDRS)
defer session.Close()

View File

@@ -1519,7 +1519,7 @@ func testStorDBitCRUDCDRs(t *testing.T) {
&CDR{
CGRID: "88ed9c38005f07576a1e1af293063833b60edcc6",
RunID: "1",
OrderID: 1,
OrderID: 0,
OriginHost: "host1",
OriginID: "1",
CostDetails: &CallCost{Timespans: TimeSpans{}},
@@ -1528,7 +1528,7 @@ func testStorDBitCRUDCDRs(t *testing.T) {
&CDR{
CGRID: "88ed9c38005f07576a1e1af293063833b60edcc2",
RunID: "2",
OrderID: 2,
OrderID: 0,
OriginHost: "host2",
OriginID: "2",
CostDetails: &CallCost{Timespans: TimeSpans{}},

View File

@@ -36,9 +36,29 @@ import (
"runtime"
"strconv"
"strings"
"sync"
"time"
)
func NewCounterGen(limit int64) *CounterGen {
return &CounterGen{limit: limit}
}
type CounterGen struct {
cnt, limit int64
sync.Mutex
}
func (cg *CounterGen) Gen() int64 {
cg.Lock()
defer cg.Unlock()
cg.cnt += 1
if cg.cnt > cg.limit {
cg.cnt = 0
}
return cg.cnt
}
// Returns first non empty string out of vals. Useful to extract defaults
func FirstNonEmpty(vals ...string) string {
for _, val := range vals {