diff --git a/common/utils/tphutil/tph.go b/common/utils/tphutil/tph.go index 627f5ce..6cdd3a7 100644 --- a/common/utils/tphutil/tph.go +++ b/common/utils/tphutil/tph.go @@ -3,28 +3,37 @@ package tphutil import ( "encoding/json" "strings" + "sync" "github.com/krau/SaveAny-Bot/config" "github.com/krau/SaveAny-Bot/pkg/telegraph" ) -var tphClient *telegraph.Client +var ( + tphClient *telegraph.Client + once sync.Once +) func DefaultClient() *telegraph.Client { - if tphClient != nil { - return tphClient - } + once.Do(func() { + tphClient = initDefault() + }) + return tphClient +} + +func initDefault() *telegraph.Client { + var client *telegraph.Client if config.C().Telegram.Proxy.Enable && config.C().Telegram.Proxy.URL != "" { proxyUrl := config.C().Telegram.Proxy.URL var err error - tphClient, err = telegraph.NewClientWithProxy(proxyUrl) + client, err = telegraph.NewClientWithProxy(proxyUrl) if err != nil { - tphClient = telegraph.NewClient() + client = telegraph.NewClient() } } else { - tphClient = telegraph.NewClient() + client = telegraph.NewClient() } - return tphClient + return client } func GetNodeImages(node telegraph.Node) []string {