Peer: replace pingIntervals array with Deque

This commit is contained in:
Sean Gilligan 2023-03-19 13:35:23 -07:00 committed by Andreas Schildbach
parent 98c3a14a54
commit 78cc85dc1e

View file

@ -48,10 +48,11 @@ import javax.annotation.Nullable;
import java.net.SocketAddress; import java.net.SocketAddress;
import java.time.Duration; import java.time.Duration;
import java.time.Instant; import java.time.Instant;
import java.util.ArrayDeque;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Deque;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
@ -169,7 +170,7 @@ public class Peer extends PeerSocketHandler {
// Outstanding pings against this peer and how long the last one took to complete. // Outstanding pings against this peer and how long the last one took to complete.
private final ReentrantLock pingIntervalsLock = new ReentrantLock(); private final ReentrantLock pingIntervalsLock = new ReentrantLock();
@GuardedBy("pingIntervalsLock") private Duration[] pingIntervals = null; @GuardedBy("pingIntervalsLock") private final Deque<Duration> pingIntervals = new ArrayDeque<>(PING_MOVING_AVERAGE_WINDOW);
private final CopyOnWriteArrayList<PendingPing> pendingPings; private final CopyOnWriteArrayList<PendingPing> pendingPings;
// Disconnect from a peer that is not responding to Pings // Disconnect from a peer that is not responding to Pings
private static final int PENDING_PINGS_LIMIT = 50; private static final int PENDING_PINGS_LIMIT = 50;
@ -1511,15 +1512,16 @@ public class Peer extends PeerSocketHandler {
private void addPingInterval(Duration sample) { private void addPingInterval(Duration sample) {
pingIntervalsLock.lock(); pingIntervalsLock.lock();
try { try {
if (pingIntervals == null) { if (pingIntervals.size() == 0) {
pingIntervals = new Duration[PING_MOVING_AVERAGE_WINDOW];
// Initialize the averaging window to the first sample. // Initialize the averaging window to the first sample.
Arrays.fill(pingIntervals, sample); for (int i = 0; i < PING_MOVING_AVERAGE_WINDOW ; i++) {
pingIntervals.add(sample);
}
} else { } else {
// Shift all elements backwards by one. // Remove oldest sample from front of queue
System.arraycopy(pingIntervals, 1, pingIntervals, 0, pingIntervals.length - 1); pingIntervals.remove();
// And append the new sample to the end. // Add new sample to end of queue
pingIntervals[pingIntervals.length - 1] = sample; pingIntervals.add(sample);
} }
} finally { } finally {
pingIntervalsLock.unlock(); pingIntervalsLock.unlock();
@ -1567,9 +1569,7 @@ public class Peer extends PeerSocketHandler {
public Optional<Duration> lastPingInterval() { public Optional<Duration> lastPingInterval() {
pingIntervalsLock.lock(); pingIntervalsLock.lock();
try { try {
if (pingIntervals == null || pingIntervals.length == 0) return Optional.ofNullable(pingIntervals.peekLast());
return Optional.empty();
return Optional.of(pingIntervals[pingIntervals.length - 1]);
} finally { } finally {
pingIntervalsLock.unlock(); pingIntervalsLock.unlock();
} }
@ -1592,11 +1592,9 @@ public class Peer extends PeerSocketHandler {
public Optional<Duration> pingInterval() { public Optional<Duration> pingInterval() {
pingIntervalsLock.lock(); pingIntervalsLock.lock();
try { try {
if (pingIntervals == null || pingIntervals.length == 0) return pingIntervals.stream()
return Optional.empty(); .reduce(Duration::plus)
Duration sum = Duration.ZERO; .map(d -> d.dividedBy(pingIntervals.size()));
for (Duration i : pingIntervals) sum = sum.plus(i);
return Optional.of(sum.dividedBy(pingIntervals.length));
} finally { } finally {
pingIntervalsLock.unlock(); pingIntervalsLock.unlock();
} }