fix: initialize userbot context once

Replace the racy lazy init with sync.OnceValue.
This commit is contained in:
krau
2026-08-16 20:28:33 +08:00
parent 0dfb6af153
commit 56660a7705

View File

@@ -2,6 +2,7 @@ package user
import (
"context"
"sync"
"time"
"github.com/celestix/gotgproto"
@@ -20,17 +21,18 @@ import (
)
var uc *gotgproto.Client
var ectx *ext.Context
// getEctx lazily creates the user-client ext.Context exactly once. Guarded by
// sync.OnceValue so concurrent GetCtx calls cannot race on ectx creation.
var getEctx = sync.OnceValue(func() *ext.Context {
return uc.CreateContext()
})
func GetCtx() *ext.Context {
if ectx != nil {
return ectx
}
if uc == nil {
return nil
}
ectx = uc.CreateContext()
return ectx
return getEctx()
}
func Login(ctx context.Context) (*gotgproto.Client, error) {