From be1b937ba10dd27b4f4bfdb9d3d724e510621836 Mon Sep 17 00:00:00 2001 From: Lyubomir Marinov Date: Sat, 5 Mar 2016 17:24:57 -0600 Subject: [PATCH] More aggressively weeds out abandoned sockets (in MuxServerSocketChannel scenarios). --- .../org/ice4j/socket/jdk8/DatagramBuffer.java | 31 +++----- .../socket/jdk8/MuxServerSocketChannel.java | 48 ++++++++++-- .../jdk8/MuxingServerSocketChannel.java | 78 ++++++++++++++++--- .../org/ice4j/socket/jdk8/Timestamped.java | 66 ++++++++++++++++ 4 files changed, 186 insertions(+), 37 deletions(-) create mode 100644 src/main/java/org/ice4j/socket/jdk8/Timestamped.java diff --git a/src/main/java/org/ice4j/socket/jdk8/DatagramBuffer.java b/src/main/java/org/ice4j/socket/jdk8/DatagramBuffer.java index 9c6593e..52dfa34 100644 --- a/src/main/java/org/ice4j/socket/jdk8/DatagramBuffer.java +++ b/src/main/java/org/ice4j/socket/jdk8/DatagramBuffer.java @@ -23,31 +23,20 @@ import java.nio.*; /** * Associates a {@link ByteBuffer} with a {@link DatagramPacket} so that the * {@code ByteBuffer} may be used for writing into a {@code byte} array and the - * {@code DatagramPacket} may be used for reader from the same {@code byte} + * {@code DatagramPacket} may be used for reading from the same {@code byte} * array. * * @author Lyubomir Marinov */ class DatagramBuffer + extends Timestamped { /** - * The {@code ByteBuffer} which is associated with {@link #datagramPacket} - * and shares its backing {@code array} with. - */ - private final ByteBuffer byteBuffer; - - /** - * The {@code DatagramPacket} which is associated with {@link #byteBuffer} - * and shares its {@code data} with. + * The {@code DatagramPacket} which is associated with the + * {@link ByteBuffer} {@link #o} and shares its {@code data} with. */ private final DatagramPacket datagramPacket; - /** - * The latest/last time in milliseconds at which {@code byte}s were written - * into this {@code DatagramBuffer}. - */ - long timestamp = -1; - /** * Initializes a new {@code DatagramBuffer} instance with a specific * capacity of {@code byte}s shared between a {@code ByteBuffer} and a @@ -58,12 +47,10 @@ class DatagramBuffer */ public DatagramBuffer(int capacity) { - byteBuffer = ByteBuffer.allocate(capacity); + super(ByteBuffer.allocate(capacity)); + datagramPacket - = new DatagramPacket( - byteBuffer.array(), - /* offset */ 0, - /* length */ 0); + = new DatagramPacket(o.array(), /* offset */ 0, /* length */ 0); } /** @@ -73,7 +60,7 @@ class DatagramBuffer */ public ByteBuffer getByteBuffer() { - return byteBuffer; + return o; } /** @@ -86,7 +73,7 @@ class DatagramBuffer */ public DatagramPacket getDatagramPacket() { - datagramPacket.setLength(byteBuffer.position()); + datagramPacket.setLength(getByteBuffer().position()); return datagramPacket; } } diff --git a/src/main/java/org/ice4j/socket/jdk8/MuxServerSocketChannel.java b/src/main/java/org/ice4j/socket/jdk8/MuxServerSocketChannel.java index 0fac6da..d4eb577 100644 --- a/src/main/java/org/ice4j/socket/jdk8/MuxServerSocketChannel.java +++ b/src/main/java/org/ice4j/socket/jdk8/MuxServerSocketChannel.java @@ -93,7 +93,8 @@ public class MuxServerSocketChannel * The (ordered) list (i.e. queue) of SocketChannels to be returned * by {@link #accept()}. */ - private final Queue acceptQ = new LinkedList<>(); + private final Queue> acceptQ + = new LinkedList<>(); /** * The {@code DatagramPacketFilter} which demultiplexes @@ -139,7 +140,7 @@ public class MuxServerSocketChannel public SocketChannel accept() throws IOException { - SocketChannel accepted; + SocketChannel accepted = null; boolean interrupted = false; // Pop a SocketChannel from acceptQ. @@ -159,8 +160,9 @@ public class MuxServerSocketChannel { synchronized (syncRoot) { - accepted = acceptQ.poll(); - if (accepted == null) + Timestamped timestamped = acceptQ.poll(); + + if (timestamped == null) { if (isBlocking()) { @@ -178,7 +180,7 @@ public class MuxServerSocketChannel break; } } - else if (accepted.isOpen()) + else if ((accepted = timestamped.o).isOpen()) { // Allow the MuxServerSocketChannel class and/or its // super(s) to have a final say on the accepted @@ -203,6 +205,39 @@ public class MuxServerSocketChannel return accepted; } + /** + * Weeds out abandoned {@code SocketChannels} from {@link #acceptQ} i.e. + * which were classified/filtered into this {@code MuxServerSocketChannel} + * but were not accepted (out of it) for a long time. + * + * @param now the (system) time in milliseconds at which the method is + * invoked + */ + void closeAbandonedSocketChannels(long now) + { + synchronized (syncRoot) + { + Collection> chs = acceptQ; + + if (!chs.isEmpty()) + { + for (Iterator> i = chs.iterator(); + i.hasNext();) + { + Timestamped ch = i.next(); + + if (now - ch.timestamp + >= MuxingServerSocketChannel + .SOCKET_CHANNEL_READ_TIMEOUT) + { + i.remove(); + MuxingServerSocketChannel.closeNoExceptions(ch.o); + } + } + } + } + } + /** * Determines whether the {@link #filter} of this instance accepts a * specific {@code SocketChannel} from which a specific @@ -268,7 +303,8 @@ public class MuxServerSocketChannel synchronized (syncRoot) { - if (acceptQ.offer(channel)) + if (acceptQ.offer( + new Timestamped<>(channel, System.currentTimeMillis()))) { syncRoot.notifyAll(); b = true; diff --git a/src/main/java/org/ice4j/socket/jdk8/MuxingServerSocketChannel.java b/src/main/java/org/ice4j/socket/jdk8/MuxingServerSocketChannel.java index d6c4f58..b800976 100644 --- a/src/main/java/org/ice4j/socket/jdk8/MuxingServerSocketChannel.java +++ b/src/main/java/org/ice4j/socket/jdk8/MuxingServerSocketChannel.java @@ -63,12 +63,21 @@ class MuxingServerSocketChannel muxingServerSocketChannels = new LinkedList<>(); + /** + * The maximum number of milliseconds to wait in a + * {@link Selector#select(long)}. The timeout should be a precaution though + * i.e. (1) it should better not be necessary and (2) it should be long + * enough to not unnecessarily hurt the performance of the application. + */ + private static final int SELECTOR_SELECT_TIMEOUT + = MuxServerSocketChannelFactory.SOCKET_CHANNEL_READ_TIMEOUT; + /** * The maximum number of milliseconds to wait for an accepted * {@code SocketChannel} to provide incoming/readable data before it is * considered abandoned by the client. */ - private static final int SOCKET_CHANNEL_READ_TIMEOUT + static final int SOCKET_CHANNEL_READ_TIMEOUT = MuxServerSocketChannelFactory.SOCKET_CHANNEL_READ_TIMEOUT; /** @@ -398,6 +407,14 @@ class MuxingServerSocketChannel Iterable channels, BiPredicate predicate) { + // The timeout to use when invoking Selector#select(long) on sel (i.e. + // the Selector supplied by selectorSupplier). It purpose is twofold: + // (1) to not wait too long in Selector#select() (hence + // SELECTOR_SELECT_TIMEOUT) and (2) to weed out abandoned channels + // (hence SOCKET_CHANNEL_READ_TIMEOUT). + final int selSelectTimeout + = Math.min(SELECTOR_SELECT_TIMEOUT, SOCKET_CHANNEL_READ_TIMEOUT); + do { Selector sel; @@ -484,7 +501,10 @@ class MuxingServerSocketChannel // to be true here.) try { - sel.select(); + // Even if no element of channels has its state(s) changed, do + // wake up after selSelectTimeout milliseconds in order to weed + // out abandoned channels. + sel.select(selSelectTimeout); } catch (ClosedSelectorException cse) { @@ -631,6 +651,26 @@ class MuxingServerSocketChannel readSelector = provider().openSelector(); } + /** + * {@inheritDoc} + */ + @Override + public SocketChannel accept() + throws IOException + { + SocketChannel ch = super.accept(); + + // Weeds out abandoned SocketChannels which were classified/filtered + // into MuxServerSocketChannel but were not accepted (out of it) for a + // long time. The accept() method of MuxingServerSocketChannel is a + // suitable place to do that because it is periodically invoked (by the + // runInAcceptThread() method) on a clock (in addition to network + // activity, of course). + closeAbandonedSocketChannels(); + + return ch; + } + /** * Adds a specific {@code MuxServerSocketChannel} to the list of * {@code MuxServerSocketChannel}s created by and delegating to this @@ -651,6 +691,27 @@ class MuxingServerSocketChannel } } + /** + * Weed out {@code SocketChannel}s which were classified/filtered into + * {@code MuxServerSocketChannel} but were not accepted (out of it) for a + * long time. + */ + private void closeAbandonedSocketChannels() + { + synchronized (syncRoot) + { + Collection chs = muxServerSocketChannels; + + if (!chs.isEmpty()) + { + long now = System.currentTimeMillis(); + + for (MuxServerSocketChannel ch : chs) + ch.closeAbandonedSocketChannels(now); + } + } + } + /** * Initializes a new {@code MuxServerSocketChannel} instance which is to * delegate to this instance and is to demultiplex incoming network @@ -677,10 +738,9 @@ class MuxingServerSocketChannel synchronized (syncRoot) { - Iterator i - = muxServerSocketChannels.iterator(); - - while (i.hasNext()) + for (Iterator i + = muxServerSocketChannels.iterator(); + i.hasNext();) { MuxServerSocketChannel aChannel = i.next(); @@ -989,14 +1049,14 @@ class MuxingServerSocketChannel if (read > 0 || db.timestamp == -1) db.timestamp = now; - DatagramPacket pkt = db.getDatagramPacket(); + DatagramPacket p = db.getDatagramPacket(); - if (pkt.getLength() > 0 && filterAccept(pkt, ch)) + if (p.getLength() > 0 && filterAccept(p, ch)) { remove = true; } else if (read <= 0 - && now - db.timestamp > SOCKET_CHANNEL_READ_TIMEOUT) + && now - db.timestamp >= SOCKET_CHANNEL_READ_TIMEOUT) { // The SocketChannel appears to have been abandoned by the // client. diff --git a/src/main/java/org/ice4j/socket/jdk8/Timestamped.java b/src/main/java/org/ice4j/socket/jdk8/Timestamped.java new file mode 100644 index 0000000..a46d2e1 --- /dev/null +++ b/src/main/java/org/ice4j/socket/jdk8/Timestamped.java @@ -0,0 +1,66 @@ +/* + * ice4j, the OpenSource Java Solution for NAT and Firewall Traversal. + * + * Copyright @ 2015 Atlassian Pty Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.ice4j.socket.jdk8; + +/** + * Represents an {@code Object} with a timestamp. + * + * @param the type of the {@code Object} which has a timestamp (associated) + * @author Lyubomir Marinov + */ +class Timestamped +{ + /** + * The {@code Object} associated with {@link #timestamp}. + */ + final T o; + + /** + * The timestamp (in milliseconds) associated with {@link #o}. + */ + long timestamp; + + /** + * Initializes a new {@code Timestamped} instance with a specific + * {@code Object} and a default timestamp (value). + * + * @param o the {@code Object} to associate with a timestamp + */ + public Timestamped(T o) + { + this(o, -1); + } + + /** + * Initializes a new {@code Timestamped} instance with a specific + * {@code Object} and a specific timestamp (value). + * + * @param o the {@code Object} to associate with {@code timestamp} + * @param timestamp the timestamp (in milliseconds) to associate with + * {@code o} + */ + public Timestamped(T o, long timestamp) + { + // It makes no sense to associate a timestamp with a null. + if (o == null) + throw new NullPointerException("o"); + + this.o = o; + this.timestamp = timestamp; + } +}