mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-02-27 20:53:01 +00:00
Add timeouts and delays to backup sends
Add rate-limit friendly delays and context timeouts when sending backups via Telegram. Iterate admin IDs with index to sleep 1s between sends; add 30s context.WithTimeout for each SendDocument call and defer file.Close() for opened files; insert a 500ms pause between sending DB and config files. These changes improve resource cleanup and reduce chance of Telegram rate-limit/timeout failures.
This commit is contained in:
parent
8eb1225734
commit
3fa0da38c9
1 changed files with 18 additions and 3 deletions
|
|
@ -2585,8 +2585,12 @@ func (t *Tgbot) SendBackupToAdmins() {
|
||||||
if !t.IsRunning() {
|
if !t.IsRunning() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for _, adminId := range adminIds {
|
for i, adminId := range adminIds {
|
||||||
t.sendBackup(int64(adminId))
|
t.sendBackup(int64(adminId))
|
||||||
|
// Add delay between sends to avoid Telegram rate limits
|
||||||
|
if i < len(adminIds)-1 {
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -3596,13 +3600,17 @@ func (t *Tgbot) sendBackup(chatId int64) {
|
||||||
logger.Error("Error in trigger a checkpoint operation: ", err)
|
logger.Error("Error in trigger a checkpoint operation: ", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Send database backup
|
||||||
file, err := os.Open(config.GetDBPath())
|
file, err := os.Open(config.GetDBPath())
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
defer file.Close()
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||||
|
defer cancel()
|
||||||
document := tu.Document(
|
document := tu.Document(
|
||||||
tu.ID(chatId),
|
tu.ID(chatId),
|
||||||
tu.File(file),
|
tu.File(file),
|
||||||
)
|
)
|
||||||
_, err = bot.SendDocument(context.Background(), document)
|
_, err = bot.SendDocument(ctx, document)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("Error in uploading backup: ", err)
|
logger.Error("Error in uploading backup: ", err)
|
||||||
}
|
}
|
||||||
|
|
@ -3610,13 +3618,20 @@ func (t *Tgbot) sendBackup(chatId int64) {
|
||||||
logger.Error("Error in opening db file for backup: ", err)
|
logger.Error("Error in opening db file for backup: ", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Small delay between file sends
|
||||||
|
time.Sleep(500 * time.Millisecond)
|
||||||
|
|
||||||
|
// Send config.json backup
|
||||||
file, err = os.Open(xray.GetConfigPath())
|
file, err = os.Open(xray.GetConfigPath())
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
defer file.Close()
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||||
|
defer cancel()
|
||||||
document := tu.Document(
|
document := tu.Document(
|
||||||
tu.ID(chatId),
|
tu.ID(chatId),
|
||||||
tu.File(file),
|
tu.File(file),
|
||||||
)
|
)
|
||||||
_, err = bot.SendDocument(context.Background(), document)
|
_, err = bot.SendDocument(ctx, document)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("Error in uploading config.json: ", err)
|
logger.Error("Error in uploading config.json: ", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue