Improve removal of expired PRESENCE txns in websocket cache

This commit is contained in:
catbref 2020-12-31 11:26:13 +00:00
parent 918a331609
commit 0ee8d7da0f

View File

@ -32,6 +32,7 @@ import org.qortal.repository.RepositoryManager;
import org.qortal.transaction.PresenceTransaction.PresenceType; import org.qortal.transaction.PresenceTransaction.PresenceType;
import org.qortal.transaction.Transaction.TransactionType; import org.qortal.transaction.Transaction.TransactionType;
import org.qortal.utils.Base58; import org.qortal.utils.Base58;
import org.qortal.utils.NTP;
@WebSocket @WebSocket
@SuppressWarnings("serial") @SuppressWarnings("serial")
@ -98,7 +99,14 @@ public class PresenceWebSocket extends ApiWebSocket implements Listener {
@Override @Override
public void listen(Event event) { public void listen(Event event) {
if (!(event instanceof Controller.NewTransactionEvent)) // We use NewBlockEvent as a proxy for 1-minute timer
if (!(event instanceof Controller.NewTransactionEvent) && !(event instanceof Controller.NewBlockEvent))
return;
removeOldEntries();
if (event instanceof Controller.NewBlockEvent)
// We only wanted a chance to cull old entries
return; return;
TransactionData transactionData = ((Controller.NewTransactionEvent) event).getTransactionData(); TransactionData transactionData = ((Controller.NewTransactionEvent) event).getTransactionData();
@ -224,4 +232,13 @@ public class PresenceWebSocket extends ApiWebSocket implements Listener {
return typedPubkeyTimestamps.compute(pubKey58, (somePubKey58, currentTimestamp) -> (currentTimestamp == null || currentTimestamp < ourTimestamp) ? ourTimestamp : currentTimestamp); return typedPubkeyTimestamps.compute(pubKey58, (somePubKey58, currentTimestamp) -> (currentTimestamp == null || currentTimestamp < ourTimestamp) ? ourTimestamp : currentTimestamp);
} }
private static void removeOldEntries() {
long now = NTP.getTime();
currentEntries.entrySet().forEach(entry -> {
long expiryThreshold = now - entry.getKey().getLifetime();
entry.getValue().entrySet().removeIf(pubkeyTimestamp -> pubkeyTimestamp.getValue() < expiryThreshold);
});
}
} }