pengrad / java-telegram-bot-api

Telegram Bot API for Java

Home Page:https://core.telegram.org/bots

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Stream of errors when network is N/A

maxim-kukushkin opened this issue · comments

I need to make my bot robust for scenarios when a network connection goes down. However if it happens my log is flooded with exception messages from the bot:

Mar 10, 2023 1:25:08 AM com.pengrad.telegrambot.impl.UpdatesHandler$1 onFailure
INFO: Update listener failure
java.net.UnknownHostException: api.telegram.org
	at java.net.InetAddress$CachedAddresses.get(InetAddress.java:764)
InetAddress.java:764
	at java.net.InetAddress.getAllByName0(InetAddress.java:1282)
InetAddress.java:1282
	at java.net.InetAddress.getAllByName(InetAddress.java:1140)
InetAddress.java:1140
	at java.net.InetAddress.getAllByName(InetAddress.java:1064)
InetAddress.java:1064
	at okhttp3.Dns$Companion$DnsSystem.lookup(Dns.kt:49)
Dns.java:49
	at okhttp3.internal.connection.RouteSelector.resetNextInetSocketAddress(RouteSelector.kt:164)
RouteSelector.java:164
	at okhttp3.internal.connection.RouteSelector.nextProxy(RouteSelector.kt:129)
RouteSelector.java:129
	at okhttp3.internal.connection.RouteSelector.next(RouteSelector.kt:71)
RouteSelector.java:71
	at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.kt:205)
ExchangeFinder.java:205
	at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.kt:106)
ExchangeFinder.java:106
	at okhttp3.internal.connection.ExchangeFinder.find(ExchangeFinder.kt:74)
ExchangeFinder.java:74
	at okhttp3.internal.connection.RealCall.initExchange$okhttp(RealCall.kt:255)
	at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:32)
ConnectInterceptor.java:32
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
RealInterceptorChain.java:109
	at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:95)
CacheInterceptor.java:95
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
RealInterceptorChain.java:109
	at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:83)
BridgeInterceptor.java:83
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
RealInterceptorChain.java:109
	at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:76)
RetryAndFollowUpInterceptor.java:76
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
RealInterceptorChain.java:109
	at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:201)
	at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:517)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
ThreadPoolExecutor.java:1149
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
ThreadPoolExecutor.java:624
	at java.lang.Thread.run(Thread.java:750)
Thread.java:750

Is it possible to limit the amount of retries, maybe using some backoff? (perhaps it can be made configurable?)

  1. Can use ExceptionHandler #317 and manually stop it with bot.removeGetUpdatesListener() and restart
  2. Can change sleep interval between retries new TelegramBot.Builder("token").updateListenerSleep(10_000)

Thanks!
I wish it was a bit more clear from the documentation and samples.. May be you can consider adding it?

Reiterating on this: setting updates listener sleep has side effects. I.e., if I use the code above and .updateListenerSleep(10_000), then it indeed limits the amount of reconnect requests to 1 in 10 seconds.

However, it also limits the traffic - now I can only receive new messages once in 10 seconds.

I don't think these 2 should be coupled. One thing is the rate of receiving messages, and completely other thing is how often to do reconnect attempts (this one is often dynamic, using a back off strategy or randomized intervals).

Also, is there any way to do long polling like other telegram clients do? I.e., instead of constant GetUpdates (updates listener effectively does them under the hood) use a long poll which as far as I understand is part of Telegram's API?