From 50fba3f91035b4248bd92d1479c3863b69e615a3 Mon Sep 17 00:00:00 2001 From: krau <71133316+krau@users.noreply.github.com> Date: Mon, 7 Apr 2025 10:23:50 +0800 Subject: [PATCH] feat: add configurable timeout for Telegram client initialization --- bot/bot.go | 4 ++-- config.example.toml | 10 +++------- config/viper.go | 2 ++ 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/bot/bot.go b/bot/bot.go index 45cc886..80a6741 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -27,10 +27,10 @@ func newProxyDialer(proxyUrl string) (proxy.Dialer, error) { } func Init() { - InitTelegraphClient() common.Log.Info("初始化 Telegram 客户端...") - ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), time.Duration(config.Cfg.Telegram.Timeout)*time.Second) defer cancel() + go InitTelegraphClient() resultChan := make(chan struct { client *gotgproto.Client err error diff --git a/config.example.toml b/config.example.toml index 6dc8651..2159f2a 100644 --- a/config.example.toml +++ b/config.example.toml @@ -13,6 +13,9 @@ token = "" # app_id = 123456 # app_hash = "0123456789abcdef0123456789abcdef" +# 初始化超时时间, 单位: 秒 +timeout = 60 + [telegram.proxy] # 启用代理连接 telegram, 只支持 socks5 enable = false @@ -30,12 +33,6 @@ enable = true # 文件保存根路径 base_path = "./downloads" -[[storages]] -name = "本机2" -type = "local" -enable = true -base_path = "./downloads/2" - [[storages]] name = "MyAlist" type = "alist" @@ -49,7 +46,6 @@ token_exp = 86400 # 86400--1天 604800--7天 1296000--15天 2592000--30 # 请自行在 alist 侧配置合理的 token 过期时间 # token = "" - [[storages]] name = "MyWebdav" type = "webdav" diff --git a/config/viper.go b/config/viper.go index e98910e..5f4bf8d 100644 --- a/config/viper.go +++ b/config/viper.go @@ -44,6 +44,7 @@ type telegramConfig struct { Token string `toml:"token" mapstructure:"token"` AppID int `toml:"app_id" mapstructure:"app_id" json:"app_id"` AppHash string `toml:"app_hash" mapstructure:"app_hash" json:"app_hash"` + Timeout int `toml:"timeout" mapstructure:"timeout" json:"timeout"` Proxy proxyConfig `toml:"proxy" mapstructure:"proxy"` // Deprecated @@ -82,6 +83,7 @@ func Init() error { viper.SetDefault("telegram.app_id", 1025907) viper.SetDefault("telegram.app_hash", "452b0359b988148995f22ff0f4229750") + viper.SetDefault("telegram.timeout", 60) viper.SetDefault("temp.base_path", "cache/") viper.SetDefault("temp.cache_ttl", 3600)