Skip to content

Commit 239d5ad

Browse files
committed
feat: update database session configuration and retry settings
1 parent e76f191 commit 239d5ad

File tree

4 files changed

+41
-34
lines changed

4 files changed

+41
-34
lines changed

bot/bot.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,11 @@ func Init() {
5656
config.Cfg.Telegram.AppHash,
5757
gotgproto.ClientTypeBot(config.Cfg.Telegram.Token),
5858
&gotgproto.ClientOpts{
59-
Session: sessionMaker.SqlSession(sqlite.Open("data/session.db")),
59+
Session: sessionMaker.SqlSession(sqlite.Open(config.Cfg.DB.Session)),
6060
DisableCopyright: true,
6161
Middlewares: FloodWaitMiddleware(),
6262
Resolver: resolver,
63+
MaxRetries: config.Cfg.Telegram.RpcRetry,
6364
},
6465
)
6566
if err != nil {

bot/middlewares.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
)
1515

1616
func FloodWaitMiddleware() []telegram.Middleware {
17-
waiter := floodwait.NewSimpleWaiter().WithMaxRetries(5)
17+
waiter := floodwait.NewSimpleWaiter().WithMaxRetries(uint(config.Cfg.Telegram.FloodRetry))
1818
ratelimiter := ratelimit.New(rate.Every(time.Millisecond*100), 5)
1919
return []telegram.Middleware{
2020
waiter,

config.example.toml

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,36 @@ stream = false # 使用stream模式, 详情请查看文档
1010
token = ""
1111
# Telegram API 配置, 若不配置也可运行, 将使用默认的 API ID 和 API HASH
1212
# 推荐使用自己的 API ID 和 API HASH (https://my.telegram.org)
13-
# app_id = 123456
14-
# app_hash = "0123456789abcdef0123456789abcdef"
13+
# app_id = 1025907
14+
# app_hash = "452b0359b988148995f22ff0f4229750"
1515

1616
# 初始化超时时间, 单位: 秒
1717
timeout = 60
1818

19+
# flood_retry = 5
20+
# rpc_retry = 5
21+
1922
[telegram.proxy]
2023
# 启用代理连接 telegram, 只支持 socks5
2124
enable = false
2225
url = "socks5://127.0.0.1:7890"
2326

27+
# 用户列表
28+
[[users]]
29+
# telegram user id
30+
id = 114514
31+
# 使用黑名单模式,开启后下方留空以使用所有存储,反之则为白名单,白名单请在下方输入允许的存储名
32+
blacklist = true
33+
# 将列表留空并开启黑名单模式以允许使用所有存储,此处示例为黑名单模式,用户 114514 可使用所有存储
34+
storages = []
35+
36+
[[users]]
37+
id = 123456
38+
blacklist = false # 使用白名单模式,此时,用户123456 仅可使用下方列表中的存储
39+
# 此时该用户只能使用名为 本机1 的存储
40+
storages = ["本机1"]
2441

25-
# 存储配置列表
42+
# 存储列表
2643
[[storages]]
2744
# 标识名, 需要唯一
2845
name = "本机1"
@@ -41,7 +58,9 @@ base_path = '/'
4158
url = 'https://alist.com'
4259
username = 'admin'
4360
password = 'password'
44-
token_exp = 86400 # 86400--1天 604800--7天 1296000--15天 2592000--30天 15552000--180天
61+
# alist token 刷新时间
62+
# 86400--1天 604800--7天 1296000--15天 2592000--30天 15552000--180天
63+
token_exp = 86400
4564
# alist 可直接使用 token 登录, 此时 username, password, token_exp 将被忽略
4665
# 请自行在 alist 侧配置合理的 token 过期时间
4766
# token = ""
@@ -66,23 +85,6 @@ secret_access_key = 'zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG'
6685
bucket_name = 'saveanybot'
6786
base_path = '/path/telegram'
6887

69-
# 用户列表
70-
[[users]]
71-
# telegram user id
72-
id = 114514
73-
# 开启黑名单,开启后下方留空以使用所有存储,反之则为白名单,白名单请在下方输入允许的存储名
74-
blacklist = true
75-
# 将列表留空并开启黑名单模式以允许使用所有存储,此处示例为黑名单模式,用户114514 可使用所有存储
76-
storages = []
77-
78-
79-
[[users]]
80-
id = 123456
81-
blacklist = false #开启白名单模式,此时,用户123456 仅可使用下方列表中的存储
82-
# 此时该用户只能使用名为 本机1 的存储
83-
storages = ["本机1"]
84-
85-
8688

8789
# 其他配置
8890

@@ -98,3 +100,5 @@ storages = ["本机1"]
98100

99101
# [db]
100102
# path = "data/data.db" # 数据库文件路径
103+
# session = "data/session.db"
104+

config/viper.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,19 @@ type logConfig struct {
3838
}
3939

4040
type dbConfig struct {
41-
Path string `toml:"path" mapstructure:"path"`
42-
Expire int64 `toml:"expire" mapstructure:"expire"`
41+
Path string `toml:"path" mapstructure:"path"`
42+
Session string `toml:"session" mapstructure:"session"`
43+
Expire int64 `toml:"expire" mapstructure:"expire"`
4344
}
4445

4546
type telegramConfig struct {
46-
Token string `toml:"token" mapstructure:"token"`
47-
AppID int `toml:"app_id" mapstructure:"app_id" json:"app_id"`
48-
AppHash string `toml:"app_hash" mapstructure:"app_hash" json:"app_hash"`
49-
Timeout int `toml:"timeout" mapstructure:"timeout" json:"timeout"`
50-
Proxy proxyConfig `toml:"proxy" mapstructure:"proxy"`
51-
52-
// Deprecated
53-
Admins []int64 `toml:"admins" mapstructure:"admins"`
47+
Token string `toml:"token" mapstructure:"token"`
48+
AppID int `toml:"app_id" mapstructure:"app_id" json:"app_id"`
49+
AppHash string `toml:"app_hash" mapstructure:"app_hash" json:"app_hash"`
50+
Timeout int `toml:"timeout" mapstructure:"timeout" json:"timeout"`
51+
Proxy proxyConfig `toml:"proxy" mapstructure:"proxy"`
52+
FloodRetry int `toml:"flood_retry" mapstructure:"flood_retry" json:"flood_retry"`
53+
RpcRetry int `toml:"rpc_retry" mapstructure:"rpc_retry" json:"rpc_retry"`
5454
}
5555

5656
type proxyConfig struct {
@@ -86,6 +86,8 @@ func Init() error {
8686
viper.SetDefault("telegram.app_id", 1025907)
8787
viper.SetDefault("telegram.app_hash", "452b0359b988148995f22ff0f4229750")
8888
viper.SetDefault("telegram.timeout", 60)
89+
viper.SetDefault("telegram.flood_retry", 5)
90+
viper.SetDefault("telegram.rpc_retry", 5)
8991

9092
viper.SetDefault("temp.base_path", "cache/")
9193
viper.SetDefault("temp.cache_ttl", 3600)
@@ -95,6 +97,7 @@ func Init() error {
9597
viper.SetDefault("log.backup_count", 7)
9698

9799
viper.SetDefault("db.path", "data/saveany.db")
100+
viper.SetDefault("db.session", "data/session.db")
98101
viper.SetDefault("db.expire", 86400*5)
99102

100103
if err := viper.SafeWriteConfigAs("config.toml"); err != nil {
@@ -150,7 +153,6 @@ func Init() error {
150153
}
151154
}
152155

153-
154156
return nil
155157
}
156158

0 commit comments

Comments
 (0)