Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenlagus committed Feb 25, 2017
1 parent 341cca7 commit 9c4b1be
Show file tree
Hide file tree
Showing 3 changed files with 532 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.apache.http.client.config.RequestConfig;
import org.telegram.telegrambots.generics.BotOptions;
import org.telegram.telegrambots.updatesreceivers.ExponentialBackOff;

import java.util.List;

Expand All @@ -14,6 +15,7 @@
public class DefaultBotOptions implements BotOptions {
private int maxThreads; ///< Max number of threads used for async methods executions (default 1)
private RequestConfig requestConfig;
private ExponentialBackOff exponentialBackOff;
private Integer maxWebhookConnections;
private List<String> allowedUpdates;

Expand Down Expand Up @@ -56,4 +58,16 @@ public void setAllowedUpdates(List<String> allowedUpdates) {
public void setRequestConfig(RequestConfig requestConfig) {
this.requestConfig = requestConfig;
}

public ExponentialBackOff getExponentialBackOff() {
return exponentialBackOff;
}

/**
* @implSpec Default implementation assumes starting at 500ms and max time of 60 minutes
* @param exponentialBackOff ExponentialBackOff to be used when long polling fails
*/
public void setExponentialBackOff(ExponentialBackOff exponentialBackOff) {
this.exponentialBackOff = exponentialBackOff;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public synchronized boolean isRunning() {

private class ReaderThread extends Thread implements UpdatesReader {
private CloseableHttpClient httpclient;
private ExponentialBackOff exponentialBackOff;
private RequestConfig requestConfig;

@Override
Expand All @@ -143,6 +144,11 @@ public synchronized void start() {
.setMaxConnTotal(100)
.build();
requestConfig = options.getRequestConfig();
exponentialBackOff = options.getExponentialBackOff();

if (exponentialBackOff == null) {
exponentialBackOff = new ExponentialBackOff();
}

if (requestConfig == null) {
requestConfig = RequestConfig.copy(RequestConfig.custom().build())
Expand Down Expand Up @@ -191,28 +197,37 @@ public void run() {
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
String responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
try {
List<Update> updates = request.deserializeResponse(responseContent);

if (updates.isEmpty()) {
synchronized (this) {
this.wait(500);
}
} else {
updates.removeIf(x -> x.getUpdateId() < lastReceivedUpdate);
lastReceivedUpdate = updates.parallelStream()
.map(
Update::getUpdateId)
.max(Integer::compareTo)
.orElse(0);
receivedUpdates.addAll(updates);

synchronized (receivedUpdates) {
receivedUpdates.notifyAll();
if (response.getStatusLine().getStatusCode() >= 500) {
BotLogger.warn(LOGTAG, responseContent);
synchronized (this) {
this.wait(500);
}
} else {
try {
List<Update> updates = request.deserializeResponse(responseContent);
exponentialBackOff.reset();

if (updates.isEmpty()) {
synchronized (this) {
this.wait(500);
}
} else {
updates.removeIf(x -> x.getUpdateId() < lastReceivedUpdate);
lastReceivedUpdate = updates.parallelStream()
.map(
Update::getUpdateId)
.max(Integer::compareTo)
.orElse(0);
receivedUpdates.addAll(updates);

synchronized (receivedUpdates) {
receivedUpdates.notifyAll();
}
}
} catch (JSONException e) {
BotLogger.severe(responseContent, LOGTAG, e);
}
}catch (JSONException e) {
BotLogger.severe(responseContent, LOGTAG, e);
}
} catch (InvalidObjectException | TelegramApiRequestException e) {
BotLogger.severe(LOGTAG, e);
Expand All @@ -226,7 +241,7 @@ public void run() {
BotLogger.severe(LOGTAG, global);
try {
synchronized (this) {
this.wait(500);
this.wait(exponentialBackOff.nextBackOffMillis());
}
} catch (InterruptedException e) {
if (!running) {
Expand Down
Loading

0 comments on commit 9c4b1be

Please sign in to comment.