Skip to content

Commit 86d6405

Browse files
committed
delay calling push url after getting HTTP 429 Too Many Requests
1 parent 7bd3e8a commit 86d6405

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

android-refimpl-app/app/src/main/java/com/zoffcc/applications/trifa/HelperFriend.java

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* [TRIfA], Java part of Tox Reference Implementation for Android
3-
* Copyright (C) 2020 Zoff <[email protected]>
3+
* Copyright (C) 2020 - 2025 Zoff <[email protected]>
44
* <p>
55
* This program is free software; you can redistribute it and/or
66
* modify it under the terms of the GNU General Public License
@@ -27,6 +27,7 @@
2727
import java.net.InetSocketAddress;
2828
import java.net.Proxy;
2929
import java.nio.ByteBuffer;
30+
import java.util.HashMap;
3031
import java.util.Iterator;
3132
import java.util.List;
3233
import java.util.concurrent.TimeUnit;
@@ -69,6 +70,9 @@ public class HelperFriend
6970
{
7071
private static final String TAG = "trifa.Hlp.Friend";
7172

73+
static HashMap<String, Long> ping_push_blocker_cache = new HashMap<>();
74+
final static long TWO_HOURS_IN_MILLIS = (2 * 3600 * 1000); // 2 hours in millis
75+
7276
static FriendList main_get_friend(long friendnum)
7377
{
7478
FriendList f = null;
@@ -1423,6 +1427,22 @@ static boolean friend_do_actual_weburl_call(final String friend_pubkey, final St
14231427
}
14241428
}
14251429

1430+
long check_for_http_too_many_request_timeout = 0L;
1431+
try
1432+
{
1433+
//noinspection DataFlowIssue
1434+
check_for_http_too_many_request_timeout = ping_push_blocker_cache.getOrDefault(pushurl_for_friend, 0L);
1435+
}
1436+
catch(Exception ignored)
1437+
{
1438+
}
1439+
1440+
if (check_for_http_too_many_request_timeout + TWO_HOURS_IN_MILLIS > System.currentTimeMillis())
1441+
{
1442+
Log.i(TAG, "friend_call_push_url:HTTP 429: too many requests -> timeout");
1443+
return false;
1444+
}
1445+
14261446
if (PREF__orbot_enabled)
14271447
{
14281448
InetSocketAddress proxyAddr = new InetSocketAddress(ORBOT_PROXY_HOST, (int) ORBOT_PROXY_PORT);
@@ -1461,13 +1481,24 @@ static boolean friend_do_actual_weburl_call(final String friend_pubkey, final St
14611481
try (Response response = client.newCall(request).execute())
14621482
{
14631483
Log.i(TAG, "friend_call_push_url"); // :url=" + pushurl_for_friend + " RES=" + response.code());
1464-
if ((response.code() < 300) && (response.code() > 199))
1484+
if (response.code() == 429)
14651485
{
1466-
if (update_message_flag)
1486+
// HINT: set timestamp of last 429 HTTP code (Error Too Many Requests)
1487+
ping_push_blocker_cache.put(pushurl_for_friend, System.currentTimeMillis());
1488+
if (ping_push_blocker_cache.size() >= 20000)
14671489
{
1468-
update_message_in_db_sent_push_set(friend_pubkey, message_timestamp_circa);
1490+
// HINT: too many entries. just clear the hasmap.
1491+
// but probably nobody will have 20k friends in this app in sum?
1492+
ping_push_blocker_cache.clear();
14691493
}
14701494
}
1495+
else if ((response.code() < 300) && (response.code() > 199))
1496+
{
1497+
if (update_message_flag)
1498+
{
1499+
update_message_in_db_sent_push_set(friend_pubkey, message_timestamp_circa);
1500+
}
1501+
}
14711502
}
14721503
catch (Exception e1)
14731504
{

0 commit comments

Comments
 (0)