|
1 | 1 | /** |
2 | 2 | * [TRIfA], Java part of Tox Reference Implementation for Android |
3 | | - * Copyright (C) 2020 Zoff <[email protected]> |
| 3 | + * Copyright (C) 2020 - 2025 Zoff <[email protected]> |
4 | 4 | * <p> |
5 | 5 | * This program is free software; you can redistribute it and/or |
6 | 6 | * modify it under the terms of the GNU General Public License |
|
27 | 27 | import java.net.InetSocketAddress; |
28 | 28 | import java.net.Proxy; |
29 | 29 | import java.nio.ByteBuffer; |
| 30 | +import java.util.HashMap; |
30 | 31 | import java.util.Iterator; |
31 | 32 | import java.util.List; |
32 | 33 | import java.util.concurrent.TimeUnit; |
@@ -69,6 +70,9 @@ public class HelperFriend |
69 | 70 | { |
70 | 71 | private static final String TAG = "trifa.Hlp.Friend"; |
71 | 72 |
|
| 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 | + |
72 | 76 | static FriendList main_get_friend(long friendnum) |
73 | 77 | { |
74 | 78 | FriendList f = null; |
@@ -1423,6 +1427,22 @@ static boolean friend_do_actual_weburl_call(final String friend_pubkey, final St |
1423 | 1427 | } |
1424 | 1428 | } |
1425 | 1429 |
|
| 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 | + |
1426 | 1446 | if (PREF__orbot_enabled) |
1427 | 1447 | { |
1428 | 1448 | 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 |
1461 | 1481 | try (Response response = client.newCall(request).execute()) |
1462 | 1482 | { |
1463 | 1483 | 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) |
1465 | 1485 | { |
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) |
1467 | 1489 | { |
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(); |
1469 | 1493 | } |
1470 | 1494 | } |
| 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 | + } |
1471 | 1502 | } |
1472 | 1503 | catch (Exception e1) |
1473 | 1504 | { |
|
0 commit comments