Files
3x-ui/internal/web/service/tgbot/stoppable_caller.go
T
PluS98 121123eaaa fix: keep panel restarts from hanging on the CPU job and Telegram (#7029)
Restarting the panel left it unreachable for up to a minute. CheckCpuJob sampled cpu.Percent over its whole one-minute period, so cron.Stop nearly always waited on it; it now diffs cpu.Times against its previous run (iowait counted as idle, as before). With Telegram unreachable, bus.Stop waited on an in-flight send and tgbot.Start ran getMe/setMyCommands inline; each bot call now returns once StopBot closes the bot's stop channel, the bot stops before the bus, and the artwork/commands warm-up runs in the background.
2026-11-02 15:34:22 +01:00

44 lines
899 B
Go

package tgbot
import (
"context"
"errors"
ta "github.com/mymmrac/telego/telegoapi"
)
var errBotStopped = errors.New("telegram bot stopped")
// stoppableCaller returns as soon as its bot is stopped or the call's context
// ends; fasthttp honours only deadlines, so a blocked route held panel restarts.
type stoppableCaller struct {
inner ta.Caller
stopped <-chan struct{}
}
type callResult struct {
resp *ta.Response
err error
}
func (c stoppableCaller) Call(ctx context.Context, url string, data *ta.RequestData) (*ta.Response, error) {
select {
case <-c.stopped:
return nil, errBotStopped
default:
}
done := make(chan callResult, 1)
go func() {
resp, err := c.inner.Call(ctx, url, data)
done <- callResult{resp, err}
}()
select {
case r := <-done:
return r.resp, r.err
case <-c.stopped:
return nil, errBotStopped
case <-ctx.Done():
return nil, ctx.Err()
}
}