Add telemtClient to bot struct and implement makeDirectTransport function for enhanced HTTP client configuration in main.go.
Publish telemt-bot Docker image / build-and-push (push) Successful in 56s

This commit is contained in:
Denozordec
2026-03-21 12:11:36 +07:00
parent 0018b5d8c6
commit a310d7e6d4
+19 -1
View File
@@ -46,6 +46,7 @@ type config struct {
type bot struct { type bot struct {
cfg config cfg config
httpClient *http.Client httpClient *http.Client
telemtClient *http.Client
telegramBase string telegramBase string
offset int64 offset int64
} }
@@ -158,6 +159,17 @@ func makeHTTPTransport() (*http.Transport, error) {
return tr, nil return tr, nil
} }
func makeDirectTransport() *http.Transport {
return &http.Transport{
MaxIdleConns: 16,
MaxIdleConnsPerHost: 8,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 5 * time.Second,
ForceAttemptHTTP2: true,
DisableCompression: false,
}
}
func getProxyURL() string { func getProxyURL() string {
for _, key := range []string{"ALL_PROXY", "all_proxy", "HTTPS_PROXY", "https_proxy", "HTTP_PROXY", "http_proxy"} { for _, key := range []string{"ALL_PROXY", "all_proxy", "HTTPS_PROXY", "https_proxy", "HTTP_PROXY", "http_proxy"} {
if v := strings.TrimSpace(os.Getenv(key)); v != "" { if v := strings.TrimSpace(os.Getenv(key)); v != "" {
@@ -183,9 +195,15 @@ func main() {
Timeout: cfg.TelegramTimeout, Timeout: cfg.TelegramTimeout,
} }
telemtClient := &http.Client{
Transport: makeDirectTransport(),
Timeout: cfg.TelemtTimeout,
}
b := &bot{ b := &bot{
cfg: cfg, cfg: cfg,
httpClient: httpClient, httpClient: httpClient,
telemtClient: telemtClient,
telegramBase: "https://api.telegram.org/bot" + cfg.TelegramBotToken, telegramBase: "https://api.telegram.org/bot" + cfg.TelegramBotToken,
} }
@@ -611,7 +629,7 @@ func (b *bot) callTelemtJSON(ctx context.Context, method, path string, payload a
req.Header.Set("Authorization", b.cfg.TelemtAPIAuth) req.Header.Set("Authorization", b.cfg.TelemtAPIAuth)
} }
resp, err := b.httpClient.Do(req) resp, err := b.telemtClient.Do(req)
if err != nil { if err != nil {
return err return err
} }