From e1086b099a1ee8f12860f9756aa7a75d5916ffea Mon Sep 17 00:00:00 2001 From: Trial97 Date: Tue, 29 Sep 2020 15:54:28 +0300 Subject: [PATCH] Added runtime.Gosched for context switching --- config/config.go | 3 ++- engine/actions2_it_test.go | 4 ++-- engine/caches.go | 5 +++-- services/apierv1.go | 4 ++-- services/cdrs.go | 4 ++-- sessions/session.go | 3 ++- sessions/sessions.go | 5 +++-- 7 files changed, 16 insertions(+), 12 deletions(-) diff --git a/config/config.go b/config/config.go index 127f5e53e..feda79c8e 100755 --- a/config/config.go +++ b/config/config.go @@ -28,6 +28,7 @@ import ( "net/url" "os" "path/filepath" + "runtime" "strings" "sync" "time" @@ -1406,7 +1407,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/actions2_it_test.go b/engine/actions2_it_test.go index 900a897d8..78694e728 100644 --- a/engine/actions2_it_test.go +++ b/engine/actions2_it_test.go @@ -22,8 +22,8 @@ package engine import ( "net/rpc" "path" + "runtime" "testing" - "time" "github.com/cgrates/cgrates/config" "github.com/cgrates/cgrates/utils" @@ -269,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/engine/caches.go b/engine/caches.go index 26a028168..cda80607c 100644 --- a/engine/caches.go +++ b/engine/caches.go @@ -21,6 +21,7 @@ package engine import ( "encoding/gob" "fmt" + "runtime" "sync" "time" @@ -104,8 +105,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/services/apierv1.go b/services/apierv1.go index 91a97da8d..13d62fb53 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" @@ -111,7 +111,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 ac6e886fe..7ab71c06b 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 c7a5bc1d5..6da3be3de 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" @@ -246,7 +247,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 68a30d729..eb2339cbe 100644 --- a/sessions/sessions.go +++ b/sessions/sessions.go @@ -23,6 +23,7 @@ import ( "fmt" "math/rand" "reflect" + "runtime" "strings" "sync" "time" @@ -309,7 +310,7 @@ func (sS *SessionS) setSTerminator(s *Session) { 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 @@ -1274,7 +1275,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 } } }