From c2c39336c42740ebd8934e28c9208f304c2b3cc6 Mon Sep 17 00:00:00 2001 From: Trial97 Date: Tue, 29 Sep 2020 14:05:58 +0300 Subject: [PATCH] Added runtime.Gosched for context switching --- config/config.go | 3 ++- engine/caches.go | 5 +++-- engine/z_actions2_it_test.go | 3 ++- general_tests/cdrs_exp_it_test.go | 3 ++- services/apierv1.go | 4 ++-- services/cdrs.go | 4 ++-- sessions/session.go | 3 ++- sessions/sessions.go | 5 +++-- 8 files changed, 18 insertions(+), 12 deletions(-) diff --git a/config/config.go b/config/config.go index 559268c4e..ba7f48d94 100755 --- a/config/config.go +++ b/config/config.go @@ -28,6 +28,7 @@ import ( "net/url" "os" "path/filepath" + "runtime" "strings" "sync" "time" @@ -1514,7 +1515,7 @@ func (cfg *CGRConfig) reloadSections(sections ...string) (err error) { break } } - time.Sleep(1) + runtime.Gosched() for _, section := range sections { switch section { default: diff --git a/engine/caches.go b/engine/caches.go index 253d95edc..d1b8c4984 100644 --- a/engine/caches.go +++ b/engine/caches.go @@ -21,6 +21,7 @@ package engine import ( "encoding/gob" "fmt" + "runtime" "sync" "time" @@ -219,8 +220,8 @@ func (chS *CacheS) Precache() (err error) { wg.Done() }(cacheID) } - time.Sleep(1) // switch context - go func() { // report wg.Wait on doneChan + runtime.Gosched() // switch context + go func() { // report wg.Wait on doneChan wg.Wait() close(doneChan) }() diff --git a/engine/z_actions2_it_test.go b/engine/z_actions2_it_test.go index 2548064c8..5ddd5cec0 100644 --- a/engine/z_actions2_it_test.go +++ b/engine/z_actions2_it_test.go @@ -22,6 +22,7 @@ package engine import ( "net/rpc" "path" + "runtime" "testing" "time" @@ -268,7 +269,7 @@ func testActionsUpdateBalance(t *testing.T) { } else if reply != utils.OK { t.Errorf("Calling APIerSv1.ExecuteAction received: %s", reply) } - time.Sleep(1) + runtime.Gosched() attrsEA2 := &utils.AttrExecuteAction{Tenant: attrsSetAccount.Tenant, Account: attrsSetAccount.Account, ActionsId: changeBlockerAction.ActionsId} if err := actsRPC.Call(utils.APIerSv1ExecuteAction, attrsEA2, &reply); err != nil { t.Error("Got error on APIerSv1.ExecuteAction: ", err.Error()) diff --git a/general_tests/cdrs_exp_it_test.go b/general_tests/cdrs_exp_it_test.go index f80056bcf..d0f4aa0bf 100644 --- a/general_tests/cdrs_exp_it_test.go +++ b/general_tests/cdrs_exp_it_test.go @@ -33,6 +33,7 @@ import ( "os/exec" "path" "reflect" + "runtime" "testing" "time" @@ -206,7 +207,7 @@ func testCDRsExpPrepareAMQP(t *testing.T) { } func testCDRsExpStartEngine(t *testing.T) { - time.Sleep(1) + runtime.Gosched() if _, err := engine.StopStartEngine(cdrsExpCfgPath, *waitRater); err != nil { t.Fatal(err) } diff --git a/services/apierv1.go b/services/apierv1.go index f4fe62cb4..d2a7e0f09 100644 --- a/services/apierv1.go +++ b/services/apierv1.go @@ -20,8 +20,8 @@ package services import ( "fmt" + "runtime" "sync" - "time" v1 "github.com/cgrates/cgrates/apier/v1" "github.com/cgrates/cgrates/config" @@ -110,7 +110,7 @@ func (apiService *APIerSv1Service) Start() (err error) { // erS.exitChan <- true } }(apiService.api, apiService.syncStop) - time.Sleep(1) + runtime.Gosched() if !apiService.cfg.DispatcherSCfg().Enabled { apiService.server.RpcRegister(apiService.api) diff --git a/services/cdrs.go b/services/cdrs.go index d0059d8a8..fd8840091 100644 --- a/services/cdrs.go +++ b/services/cdrs.go @@ -20,8 +20,8 @@ package services import ( "fmt" + "runtime" "sync" - "time" v1 "github.com/cgrates/cgrates/apier/v1" v2 "github.com/cgrates/cgrates/apier/v2" @@ -95,7 +95,7 @@ func (cdrService *CDRServer) Start() (err error) { // erS.exitChan <- true } }(cdrService.cdrS, cdrService.syncStop) - time.Sleep(1) + runtime.Gosched() utils.Logger.Info("Registering CDRS HTTP Handlers.") cdrService.cdrS.RegisterHandlersToServer(cdrService.server) utils.Logger.Info("Registering CDRS RPC service.") diff --git a/sessions/session.go b/sessions/session.go index ac39db17d..b4bcbcda5 100644 --- a/sessions/session.go +++ b/sessions/session.go @@ -19,6 +19,7 @@ along with this program. If not, see package sessions import ( + "runtime" "sync" "time" @@ -249,7 +250,7 @@ func (s *Session) stopSTerminator() { func (s *Session) stopDebitLoops() { if s.debitStop != nil { close(s.debitStop) // Stop automatic debits - time.Sleep(1) + runtime.Gosched() s.debitStop = nil } } diff --git a/sessions/sessions.go b/sessions/sessions.go index 694164a1d..b77efb7e6 100644 --- a/sessions/sessions.go +++ b/sessions/sessions.go @@ -23,6 +23,7 @@ import ( "fmt" "math/rand" "reflect" + "runtime" "strings" "sync" "time" @@ -333,7 +334,7 @@ func (sS *SessionS) setSTerminator(s *Session, opts engine.MapEvent) { s.sTerminator.timer.Stop() } }() - time.Sleep(1) // force context switching + runtime.Gosched() // force context switching } // forceSTerminate is called when a session times-out or it is forced from CGRateS side @@ -1364,7 +1365,7 @@ func (sS *SessionS) initSessionDebitLoops(s *Session) { s.debitStop = make(chan struct{}) } go sS.debitLoopSession(s, i, s.DebitInterval) - time.Sleep(1) // allow the goroutine to be executed + runtime.Gosched() // allow the goroutine to be executed } } }