From 33401a78f7f2b1b32960ffbb561ec9ead92f16f2 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Tue, 29 Mar 2016 19:33:42 +0300 Subject: [PATCH 1/4] extra duraction improvements --- sessionmanager/smg_session.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/sessionmanager/smg_session.go b/sessionmanager/smg_session.go index 84a7ec7dd..ba02dbb60 100644 --- a/sessionmanager/smg_session.go +++ b/sessionmanager/smg_session.go @@ -81,10 +81,11 @@ func (self *SMGSession) debit(dur time.Duration, lastUsed time.Duration) (time.D requestedDuration := dur self.totalUsage += lastUsed // Should reflect the total usage so far - //utils.Logger.Debug(fmt.Sprintf("ExtraDuration: %d", self.extraDuration)) + //utils.Logger.Debug(fmt.Sprintf("lastUsage: %f lastUsed: %f", self.lastUsage.Seconds(), lastUsed.Seconds())) + //utils.Logger.Debug(fmt.Sprintf("ExtraDuration: %f", self.extraDuration.Seconds())) if lastUsed > 0 { self.extraDuration = self.lastUsage - lastUsed - //utils.Logger.Debug(fmt.Sprintf("ExtraDuration LastUsed: %d", self.extraDuration)) + //utils.Logger.Debug(fmt.Sprintf("ExtraDuration LastUsed: %f", self.extraDuration.Seconds())) } // apply correction from previous run if self.extraDuration < dur { @@ -94,7 +95,7 @@ func (self *SMGSession) debit(dur time.Duration, lastUsed time.Duration) (time.D self.extraDuration -= dur return ccDuration, nil } - + //utils.Logger.Debug(fmt.Sprintf("dur: %f", dur.Seconds())) self.extraDuration = 0 if self.cd.LoopIndex > 0 { self.cd.TimeStart = self.cd.TimeEnd @@ -110,6 +111,7 @@ func (self *SMGSession) debit(dur time.Duration, lastUsed time.Duration) (time.D self.cd.TimeEnd = cc.GetEndTime() // set debited timeEnd // update call duration with real debited duration ccDuration := cc.GetDuration() + initialExtraDuration := self.extraDuration if ccDuration != dur { self.extraDuration = ccDuration - dur } @@ -119,12 +121,12 @@ func (self *SMGSession) debit(dur time.Duration, lastUsed time.Duration) (time.D self.cd.LoopIndex += 1 self.sessionCds = append(self.sessionCds, self.cd.Clone()) self.callCosts = append(self.callCosts, cc) - self.lastUsage = ccDuration + self.lastUsage = initialExtraDuration + ccDuration if ccDuration >= dur { // we got what we asked to be debited return requestedDuration, nil } - return ccDuration, nil + return initialExtraDuration + ccDuration, nil } // Attempts to refund a duration, error on failure From c3feacac7d28bb79e2b7d6b40b956d0ea203d09c Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Tue, 29 Mar 2016 19:43:45 +0300 Subject: [PATCH 2/4] last usage test fixes --- sessionmanager/data_it_test.go | 6 +++--- sessionmanager/smg_session.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/sessionmanager/data_it_test.go b/sessionmanager/data_it_test.go index 513941df0..b281c2985 100644 --- a/sessionmanager/data_it_test.go +++ b/sessionmanager/data_it_test.go @@ -258,7 +258,7 @@ func TestSMGDataLastUsedMultipleData(t *testing.T) { if maxUsage != 1.048576e+06 { t.Error("Bad max usage: ", maxUsage) } - eAcntVal = 49997849600.000000 // 20480 + eAcntVal = 49998883840.000000 // 20480 if err := smgRPC.Call("ApierV2.GetAccount", attrs, &acnt); err != nil { t.Error(err) } else if acnt.BalanceMap[utils.DATA].GetTotalValue() != eAcntVal { @@ -285,7 +285,7 @@ func TestSMGDataLastUsedMultipleData(t *testing.T) { if maxUsage != 1.048576e+06 { t.Error("Bad max usage: ", maxUsage) } - eAcntVal = 49997829120.000000 // 20480 + eAcntVal = 49998863360.000000 // 20480 if err := smgRPC.Call("ApierV2.GetAccount", attrs, &acnt); err != nil { t.Error(err) } else if acnt.BalanceMap[utils.DATA].GetTotalValue() != eAcntVal { @@ -311,7 +311,7 @@ func TestSMGDataLastUsedMultipleData(t *testing.T) { if maxUsage != 1.048576e+06 { t.Error("Bad max usage: ", maxUsage) } - eAcntVal = 49996774400.000000 // 1054720 + eAcntVal = 49998842880.000000 // 20480 if err := smgRPC.Call("ApierV2.GetAccount", attrs, &acnt); err != nil { t.Error(err) } else if acnt.BalanceMap[utils.DATA].GetTotalValue() != eAcntVal { diff --git a/sessionmanager/smg_session.go b/sessionmanager/smg_session.go index ba02dbb60..4f4c7ed28 100644 --- a/sessionmanager/smg_session.go +++ b/sessionmanager/smg_session.go @@ -96,6 +96,7 @@ func (self *SMGSession) debit(dur time.Duration, lastUsed time.Duration) (time.D return ccDuration, nil } //utils.Logger.Debug(fmt.Sprintf("dur: %f", dur.Seconds())) + initialExtraDuration := self.extraDuration self.extraDuration = 0 if self.cd.LoopIndex > 0 { self.cd.TimeStart = self.cd.TimeEnd @@ -111,7 +112,6 @@ func (self *SMGSession) debit(dur time.Duration, lastUsed time.Duration) (time.D self.cd.TimeEnd = cc.GetEndTime() // set debited timeEnd // update call duration with real debited duration ccDuration := cc.GetDuration() - initialExtraDuration := self.extraDuration if ccDuration != dur { self.extraDuration = ccDuration - dur } From c2c79742331751676b68a0f93c58ef6eacb7c861 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Tue, 29 Mar 2016 19:50:57 +0300 Subject: [PATCH 3/4] another test fix --- sessionmanager/smg_it_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sessionmanager/smg_it_test.go b/sessionmanager/smg_it_test.go index e553ba148..ba131c5e0 100644 --- a/sessionmanager/smg_it_test.go +++ b/sessionmanager/smg_it_test.go @@ -392,7 +392,7 @@ func TestSMGLastUsed(t *testing.T) { if maxUsage != 120 { t.Error("Bad max usage: ", maxUsage) } - eAcntVal = 6.490110 + eAcntVal = 6.590100 if err := smgRPC.Call("ApierV2.GetAccount", attrs, &acnt); err != nil { t.Error(err) } else if acnt.BalanceMap[utils.MONETARY].GetTotalValue() != eAcntVal { From 587a23e2ccf77e09fd5735d01ad36fab4c569e37 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Tue, 29 Mar 2016 20:16:07 +0300 Subject: [PATCH 4/4] fix potential concurent map panic --- engine/action.go | 12 ++++++++++-- engine/cdrs.go | 7 ++++++- utils/httpclient.go | 2 +- utils/httpclient_local_test.go | 8 ++++---- 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/engine/action.go b/engine/action.go index fff13356c..6c57e8aaa 100644 --- a/engine/action.go +++ b/engine/action.go @@ -426,9 +426,13 @@ func callUrl(ub *Account, sq *StatsQueueTriggered, a *Action, acs Actions) error if sq != nil { o = sq } + jsn, err := json.Marshal(o) + if err != nil { + return err + } cfg := config.CgrConfig() fallbackPath := path.Join(cfg.HttpFailedDir, fmt.Sprintf("act_%s_%s_%s.json", a.ActionType, a.ExtraParameters, utils.GenUUID())) - _, err := utils.HttpPoster(a.ExtraParameters, cfg.HttpSkipTlsVerify, o, utils.CONTENT_JSON, 1, fallbackPath) + _, err = utils.HttpPoster(a.ExtraParameters, cfg.HttpSkipTlsVerify, jsn, utils.CONTENT_JSON, 1, fallbackPath) return err } @@ -441,9 +445,13 @@ func callUrlAsync(ub *Account, sq *StatsQueueTriggered, a *Action, acs Actions) if sq != nil { o = sq } + jsn, err := json.Marshal(o) + if err != nil { + return err + } cfg := config.CgrConfig() fallbackPath := path.Join(cfg.HttpFailedDir, fmt.Sprintf("act_%s_%s_%s.json", a.ActionType, a.ExtraParameters, utils.GenUUID())) - go utils.HttpPoster(a.ExtraParameters, cfg.HttpSkipTlsVerify, o, utils.CONTENT_JSON, 3, fallbackPath) + go utils.HttpPoster(a.ExtraParameters, cfg.HttpSkipTlsVerify, jsn, utils.CONTENT_JSON, 3, fallbackPath) return nil } diff --git a/engine/cdrs.go b/engine/cdrs.go index 1b2d2e563..3cca1b6b3 100644 --- a/engine/cdrs.go +++ b/engine/cdrs.go @@ -19,6 +19,7 @@ along with this program. If not, see package engine import ( + "encoding/json" "fmt" "io/ioutil" "net/http" @@ -424,7 +425,11 @@ func (self *CdrServer) replicateCdr(cdr *CDR) error { body = cdr.AsHttpForm() case utils.META_HTTP_JSON: content = utils.CONTENT_JSON - body = cdr + jsn, err := json.Marshal(cdr) + if err != nil { + return err + } + body = jsn } errChan := make(chan error) go func(body interface{}, rplCfg *config.CdrReplicationCfg, content string, errChan chan error) { diff --git a/utils/httpclient.go b/utils/httpclient.go index 5e048ebd0..0fad55afb 100644 --- a/utils/httpclient.go +++ b/utils/httpclient.go @@ -80,7 +80,7 @@ func HttpPoster(addr string, skipTlsVerify bool, content interface{}, contentTyp var err error switch contentType { case CONTENT_JSON: - body, err = json.Marshal(content) + body = content.([]byte) case CONTENT_FORM: urlData = content.(url.Values) case CONTENT_TEXT: diff --git a/utils/httpclient_local_test.go b/utils/httpclient_local_test.go index 7d73b00bd..ecdf49481 100644 --- a/utils/httpclient_local_test.go +++ b/utils/httpclient_local_test.go @@ -39,15 +39,15 @@ func TestHttpJsonPoster(t *testing.T) { return } content := &TestContent{Var1: "Val1", Var2: "Val2"} + jsn, _ := json.Marshal(content) filePath := "/tmp/cgr_test_http_poster.json" - if _, err := HttpPoster("http://localhost:8080/invalid", true, content, CONTENT_JSON, 3, filePath); err != nil { + if _, err := HttpPoster("http://localhost:8080/invalid", true, jsn, CONTENT_JSON, 3, filePath); err != nil { t.Error(err) } - jsnContent, _ := json.Marshal(content) if readBytes, err := ioutil.ReadFile(filePath); err != nil { t.Error(err) - } else if !reflect.DeepEqual(jsnContent, readBytes) { - t.Errorf("Expecting: %q, received: %q", string(jsnContent), string(readBytes)) + } else if !reflect.DeepEqual(jsn, readBytes) { + t.Errorf("Expecting: %q, received: %q", string(jsn), string(readBytes)) } if err := os.Remove(filePath); err != nil { t.Error("Failed removing file: ", filePath)