1
0
mirror of https://github.com/ManyakRus/starter.git synced 2025-11-24 22:53:52 +02:00

сделал contextmain.SetContext(ctx)

This commit is contained in:
Nikitin Aleksandr
2025-04-03 16:23:44 +03:00
parent 9ac89940cf
commit 85d6d45cf1
25 changed files with 121 additions and 33 deletions

View File

@@ -257,7 +257,10 @@ func Start_ctx(ctx *context.Context, WaitGroup *sync.WaitGroup, HandleJob func(c
var err error
//запомним к себе контекст
contextmain.Ctx = ctx
if contextmain.Ctx != ctx {
contextmain.SetContext(ctx)
}
//contextmain.Ctx = ctx
if ctx == nil {
contextmain.GetContext()
}

View File

@@ -217,7 +217,10 @@ func Start_ctx(ctx *context.Context, WaitGroup *sync.WaitGroup, HandleJob func(c
var err error
//запомним к себе контекст
contextmain.Ctx = ctx
if contextmain.Ctx != ctx {
contextmain.SetContext(ctx)
}
//contextmain.Ctx = ctx
if ctx == nil {
contextmain.GetContext()
}

View File

@@ -163,7 +163,10 @@ func Start_ctx(ctx *context.Context, WaitGroup *sync.WaitGroup) error {
var err error
//запомним к себе контекст
contextmain.Ctx = ctx
if contextmain.Ctx != ctx {
contextmain.SetContext(ctx)
}
//contextmain.Ctx = ctx
if ctx == nil {
contextmain.GetContext()
}

View File

@@ -159,7 +159,10 @@ func Start_ctx(ctx *context.Context, WaitGroup *sync.WaitGroup) error {
var err error
//запомним к себе контекст
contextmain.Ctx = ctx
if contextmain.Ctx != ctx {
contextmain.SetContext(ctx)
}
//contextmain.Ctx = ctx
if ctx == nil {
contextmain.GetContext()
}

View File

@@ -18,24 +18,26 @@ var CancelContext func()
// onceCtx - гарантирует единственное создание контеста
var onceCtx sync.Once
// lockContextMain - гарантирует единственное создание контеста
// var lockContextMain sync.Mutex
// MutexContextMain - гарантирует единственное создание контеста
var MutexContextMain sync.RWMutex
// GetContext - возвращает глобальный контекст приложения
func GetContext() context.Context {
//lockContextMain.Lock()
//defer lockContextMain.Unlock()
//
MutexContextMain.RLock()
defer MutexContextMain.RUnlock()
//if Ctx == nil {
// CtxBg := context.Background()
// Ctx, CancelContext = context.WithCancel(CtxBg)
//}
onceCtx.Do(func() {
CtxBg := context.Background()
var Ctx0 context.Context
Ctx0, CancelContext = context.WithCancel(CtxBg)
Ctx = &Ctx0
if Ctx == nil { //можно заполнить свой контекст, поэтому if
CtxBg := context.Background()
var Ctx0 context.Context
Ctx0, CancelContext = context.WithCancel(CtxBg)
Ctx = &Ctx0
}
})
return *Ctx
@@ -50,3 +52,17 @@ func GetNewContext() context.Context {
return *Ctx
}
// SetContext - устанавливает глобальный контекст, с учётом Mutex
func SetContext(ctx *context.Context) {
MutexContextMain.Lock()
defer MutexContextMain.Unlock()
Ctx = ctx
}
// SetCancelContext - устанавливает функцию глобального отмены контекста, с учётом Mutex
func SetCancelContext(cancelContext func()) {
MutexContextMain.Lock()
defer MutexContextMain.Unlock()
CancelContext = cancelContext
}

View File

@@ -259,7 +259,10 @@ func Start_ctx(ctx *context.Context, WaitGroup *sync.WaitGroup) error {
var err error
//запомним к себе контекст
contextmain.Ctx = ctx
if contextmain.Ctx != ctx {
contextmain.SetContext(ctx)
}
//contextmain.Ctx = ctx
if ctx == nil {
contextmain.GetContext()
}

View File

@@ -289,7 +289,10 @@ func Start_ctx(ctx *context.Context, WaitGroup *sync.WaitGroup) error {
var err error
//запомним к себе контекст
contextmain.Ctx = ctx
if contextmain.Ctx != ctx {
contextmain.SetContext(ctx)
}
//contextmain.Ctx = ctx
if ctx == nil {
contextmain.GetContext()
}

View File

@@ -145,7 +145,10 @@ func Start_ctx(ctx *context.Context, WaitGroup *sync.WaitGroup) {
//var err error
//запомним к себе контекст
contextmain.Ctx = ctx
if contextmain.Ctx != ctx {
contextmain.SetContext(ctx)
}
//contextmain.Ctx = ctx
if ctx == nil {
contextmain.GetContext()
}

View File

@@ -120,7 +120,10 @@ func Start_ctx(ctx *context.Context, WaitGroup *sync.WaitGroup) error {
var err error
//запомним к себе контекст
contextmain.Ctx = ctx
if contextmain.Ctx != ctx {
contextmain.SetContext(ctx)
}
//contextmain.Ctx = ctx
if ctx == nil {
contextmain.GetContext()
}

View File

@@ -49,7 +49,10 @@ func Start_ctx(ctx *context.Context, WaitGroup *sync.WaitGroup) {
//var err error
//запомним к себе контекст
contextmain.Ctx = ctx
if contextmain.Ctx != ctx {
contextmain.SetContext(ctx)
}
//contextmain.Ctx = ctx
if ctx == nil {
contextmain.GetContext()
}

View File

@@ -217,7 +217,10 @@ func Start_ctx(ctx *context.Context, WaitGroup *sync.WaitGroup) error {
var err error
//запомним к себе контекст
contextmain.Ctx = ctx
if contextmain.Ctx != ctx {
contextmain.SetContext(ctx)
}
//contextmain.Ctx = ctx
if ctx == nil {
contextmain.GetContext()
}

View File

@@ -223,7 +223,10 @@ func Start_ctx(ctx *context.Context, WaitGroup *sync.WaitGroup) error {
var err error
//запомним к себе контекст
contextmain.Ctx = ctx
if contextmain.Ctx != ctx {
contextmain.SetContext(ctx)
}
//contextmain.Ctx = ctx
if ctx == nil {
contextmain.GetContext()
}

View File

@@ -237,7 +237,10 @@ func Start_ctx(ctx *context.Context, WaitGroup *sync.WaitGroup) error {
var err error
//запомним к себе контекст
contextmain.Ctx = ctx
if contextmain.Ctx != ctx {
contextmain.SetContext(ctx)
}
//contextmain.Ctx = ctx
if ctx == nil {
contextmain.GetContext()
}

View File

@@ -88,7 +88,10 @@ func Start_ctx(ctx *context.Context, WaitGroup *sync.WaitGroup, ServiceName stri
var err error
//запомним к себе контекст
contextmain.Ctx = ctx
if contextmain.Ctx != ctx {
contextmain.SetContext(ctx)
}
//contextmain.Ctx = ctx
if ctx == nil {
contextmain.GetContext()
}

View File

@@ -212,7 +212,10 @@ func Start_ctx(ctx *context.Context, WaitGroup *sync.WaitGroup, ServiceName stri
var err error
//запомним к себе контекст
contextmain.Ctx = ctx
if contextmain.Ctx != ctx {
contextmain.SetContext(ctx)
}
//contextmain.Ctx = ctx
if ctx == nil {
contextmain.GetContext()
}

View File

@@ -235,7 +235,10 @@ func Start_ctx(ctx *context.Context, WaitGroup *sync.WaitGroup) error {
var err error
//запомним к себе контекст
contextmain.Ctx = ctx
if contextmain.Ctx != ctx {
contextmain.SetContext(ctx)
}
//contextmain.Ctx = ctx
if ctx == nil {
contextmain.GetContext()
}

View File

@@ -295,7 +295,10 @@ func Start_ctx(ctx *context.Context, WaitGroup *sync.WaitGroup) error {
var err error
//запомним к себе контекст
contextmain.Ctx = ctx
if contextmain.Ctx != ctx {
contextmain.SetContext(ctx)
}
//contextmain.Ctx = ctx
if ctx == nil {
contextmain.GetContext()
}

View File

@@ -297,7 +297,10 @@ func Start_ctx(ctx *context.Context, WaitGroup *sync.WaitGroup) error {
var err error
//запомним к себе контекст
contextmain.Ctx = ctx
if contextmain.Ctx != ctx {
contextmain.SetContext(ctx)
}
//contextmain.Ctx = ctx
if ctx == nil {
contextmain.GetContext()
}

View File

@@ -266,7 +266,10 @@ func Start_ctx(ctx *context.Context, WaitGroup *sync.WaitGroup) error {
var err error
//запомним к себе контекст
contextmain.Ctx = ctx
if contextmain.Ctx != ctx {
contextmain.SetContext(ctx)
}
//contextmain.Ctx = ctx
if ctx == nil {
contextmain.GetContext()
}

View File

@@ -293,7 +293,10 @@ func Start_ctx(ctx *context.Context, WaitGroup *sync.WaitGroup, Connection conne
var err error
//запомним к себе контекст
contextmain.Ctx = ctx
if contextmain.Ctx != ctx {
contextmain.SetContext(ctx)
}
//contextmain.Ctx = ctx
if ctx == nil {
contextmain.GetContext()
}

View File

@@ -66,7 +66,10 @@ func Start_ctx(ctx *context.Context, WaitGroup *sync.WaitGroup, ServiceName, Ser
var err error
//запомним к себе контекст
contextmain.Ctx = ctx
if contextmain.Ctx != ctx {
contextmain.SetContext(ctx)
}
//contextmain.Ctx = ctx
if ctx == nil {
contextmain.GetContext()
}

View File

@@ -264,7 +264,10 @@ func Start_ctx(ctx *context.Context, WaitGroup *sync.WaitGroup) error {
var err error
//запомним к себе контекст
contextmain.Ctx = ctx
if contextmain.Ctx != ctx {
contextmain.SetContext(ctx)
}
//contextmain.Ctx = ctx
if ctx == nil {
contextmain.GetContext()
}

View File

@@ -854,7 +854,10 @@ func Start_ctx(ctx *context.Context, WaitGroup *sync.WaitGroup, func_OnNewMessag
var err error
//запомним к себе контекст
contextmain.Ctx = ctx
if contextmain.Ctx != ctx {
contextmain.SetContext(ctx)
}
//contextmain.Ctx = ctx
if ctx == nil {
contextmain.GetContext()
}

View File

@@ -155,7 +155,10 @@ func Start() {
// wg - глобальный WaitGroup приложения
func Start_ctx(ctx *context.Context, wg *sync.WaitGroup) error {
var err error
contextmain.Ctx = ctx
if contextmain.Ctx != ctx {
contextmain.SetContext(ctx)
}
//contextmain.Ctx = ctx
stopapp.SetWaitGroup_Main(wg)
err = Connect_err()

View File

@@ -327,7 +327,10 @@ func Start_ctx(ctx *context.Context, WaitGroup *sync.WaitGroup, eventHandler fun
var err error
//запомним к себе контекст
contextmain.Ctx = ctx
if contextmain.Ctx != ctx {
contextmain.SetContext(ctx)
}
//contextmain.Ctx = ctx
if ctx == nil {
contextmain.GetContext()
}