More aggressively weeds out abandoned sockets (in MuxServerSocketChannel scenarios).
This commit is contained in:
parent
20f39c0037
commit
be1b937ba1
@ -23,31 +23,20 @@ import java.nio.*;
|
|||||||
/**
|
/**
|
||||||
* Associates a {@link ByteBuffer} with a {@link DatagramPacket} so that the
|
* 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 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.
|
* array.
|
||||||
*
|
*
|
||||||
* @author Lyubomir Marinov
|
* @author Lyubomir Marinov
|
||||||
*/
|
*/
|
||||||
class DatagramBuffer
|
class DatagramBuffer
|
||||||
|
extends Timestamped<ByteBuffer>
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* The {@code ByteBuffer} which is associated with {@link #datagramPacket}
|
* The {@code DatagramPacket} which is associated with the
|
||||||
* and shares its backing {@code array} with.
|
* {@link ByteBuffer} {@link #o} and shares its {@code data} with.
|
||||||
*/
|
|
||||||
private final ByteBuffer byteBuffer;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The {@code DatagramPacket} which is associated with {@link #byteBuffer}
|
|
||||||
* and shares its {@code data} with.
|
|
||||||
*/
|
*/
|
||||||
private final DatagramPacket datagramPacket;
|
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
|
* Initializes a new {@code DatagramBuffer} instance with a specific
|
||||||
* capacity of {@code byte}s shared between a {@code ByteBuffer} and a
|
* capacity of {@code byte}s shared between a {@code ByteBuffer} and a
|
||||||
@ -58,12 +47,10 @@ class DatagramBuffer
|
|||||||
*/
|
*/
|
||||||
public DatagramBuffer(int capacity)
|
public DatagramBuffer(int capacity)
|
||||||
{
|
{
|
||||||
byteBuffer = ByteBuffer.allocate(capacity);
|
super(ByteBuffer.allocate(capacity));
|
||||||
|
|
||||||
datagramPacket
|
datagramPacket
|
||||||
= new DatagramPacket(
|
= new DatagramPacket(o.array(), /* offset */ 0, /* length */ 0);
|
||||||
byteBuffer.array(),
|
|
||||||
/* offset */ 0,
|
|
||||||
/* length */ 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -73,7 +60,7 @@ class DatagramBuffer
|
|||||||
*/
|
*/
|
||||||
public ByteBuffer getByteBuffer()
|
public ByteBuffer getByteBuffer()
|
||||||
{
|
{
|
||||||
return byteBuffer;
|
return o;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -86,7 +73,7 @@ class DatagramBuffer
|
|||||||
*/
|
*/
|
||||||
public DatagramPacket getDatagramPacket()
|
public DatagramPacket getDatagramPacket()
|
||||||
{
|
{
|
||||||
datagramPacket.setLength(byteBuffer.position());
|
datagramPacket.setLength(getByteBuffer().position());
|
||||||
return datagramPacket;
|
return datagramPacket;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -93,7 +93,8 @@ public class MuxServerSocketChannel
|
|||||||
* The (ordered) list (i.e. queue) of <tt>SocketChannel</tt>s to be returned
|
* The (ordered) list (i.e. queue) of <tt>SocketChannel</tt>s to be returned
|
||||||
* by {@link #accept()}.
|
* by {@link #accept()}.
|
||||||
*/
|
*/
|
||||||
private final Queue<SocketChannel> acceptQ = new LinkedList<>();
|
private final Queue<Timestamped<SocketChannel>> acceptQ
|
||||||
|
= new LinkedList<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The {@code DatagramPacketFilter} which demultiplexes
|
* The {@code DatagramPacketFilter} which demultiplexes
|
||||||
@ -139,7 +140,7 @@ public class MuxServerSocketChannel
|
|||||||
public SocketChannel accept()
|
public SocketChannel accept()
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
SocketChannel accepted;
|
SocketChannel accepted = null;
|
||||||
boolean interrupted = false;
|
boolean interrupted = false;
|
||||||
|
|
||||||
// Pop a SocketChannel from acceptQ.
|
// Pop a SocketChannel from acceptQ.
|
||||||
@ -159,8 +160,9 @@ public class MuxServerSocketChannel
|
|||||||
{
|
{
|
||||||
synchronized (syncRoot)
|
synchronized (syncRoot)
|
||||||
{
|
{
|
||||||
accepted = acceptQ.poll();
|
Timestamped<SocketChannel> timestamped = acceptQ.poll();
|
||||||
if (accepted == null)
|
|
||||||
|
if (timestamped == null)
|
||||||
{
|
{
|
||||||
if (isBlocking())
|
if (isBlocking())
|
||||||
{
|
{
|
||||||
@ -178,7 +180,7 @@ public class MuxServerSocketChannel
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (accepted.isOpen())
|
else if ((accepted = timestamped.o).isOpen())
|
||||||
{
|
{
|
||||||
// Allow the MuxServerSocketChannel class and/or its
|
// Allow the MuxServerSocketChannel class and/or its
|
||||||
// super(s) to have a final say on the accepted
|
// super(s) to have a final say on the accepted
|
||||||
@ -203,6 +205,39 @@ public class MuxServerSocketChannel
|
|||||||
return accepted;
|
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<Timestamped<SocketChannel>> chs = acceptQ;
|
||||||
|
|
||||||
|
if (!chs.isEmpty())
|
||||||
|
{
|
||||||
|
for (Iterator<Timestamped<SocketChannel>> i = chs.iterator();
|
||||||
|
i.hasNext();)
|
||||||
|
{
|
||||||
|
Timestamped<SocketChannel> 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
|
* Determines whether the {@link #filter} of this instance accepts a
|
||||||
* specific {@code SocketChannel} from which a specific
|
* specific {@code SocketChannel} from which a specific
|
||||||
@ -268,7 +303,8 @@ public class MuxServerSocketChannel
|
|||||||
|
|
||||||
synchronized (syncRoot)
|
synchronized (syncRoot)
|
||||||
{
|
{
|
||||||
if (acceptQ.offer(channel))
|
if (acceptQ.offer(
|
||||||
|
new Timestamped<>(channel, System.currentTimeMillis())))
|
||||||
{
|
{
|
||||||
syncRoot.notifyAll();
|
syncRoot.notifyAll();
|
||||||
b = true;
|
b = true;
|
||||||
|
|||||||
@ -63,12 +63,21 @@ class MuxingServerSocketChannel
|
|||||||
muxingServerSocketChannels
|
muxingServerSocketChannels
|
||||||
= new LinkedList<>();
|
= 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
|
* The maximum number of milliseconds to wait for an accepted
|
||||||
* {@code SocketChannel} to provide incoming/readable data before it is
|
* {@code SocketChannel} to provide incoming/readable data before it is
|
||||||
* considered abandoned by the client.
|
* 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;
|
= MuxServerSocketChannelFactory.SOCKET_CHANNEL_READ_TIMEOUT;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -398,6 +407,14 @@ class MuxingServerSocketChannel
|
|||||||
Iterable<T> channels,
|
Iterable<T> channels,
|
||||||
BiPredicate<T, SelectionKey> predicate)
|
BiPredicate<T, SelectionKey> 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
|
do
|
||||||
{
|
{
|
||||||
Selector sel;
|
Selector sel;
|
||||||
@ -484,7 +501,10 @@ class MuxingServerSocketChannel
|
|||||||
// to be true here.)
|
// to be true here.)
|
||||||
try
|
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)
|
catch (ClosedSelectorException cse)
|
||||||
{
|
{
|
||||||
@ -631,6 +651,26 @@ class MuxingServerSocketChannel
|
|||||||
readSelector = provider().openSelector();
|
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
|
* Adds a specific {@code MuxServerSocketChannel} to the list of
|
||||||
* {@code MuxServerSocketChannel}s created by and delegating to this
|
* {@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<MuxServerSocketChannel> 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
|
* Initializes a new {@code MuxServerSocketChannel} instance which is to
|
||||||
* delegate to this instance and is to demultiplex incoming network
|
* delegate to this instance and is to demultiplex incoming network
|
||||||
@ -677,10 +738,9 @@ class MuxingServerSocketChannel
|
|||||||
|
|
||||||
synchronized (syncRoot)
|
synchronized (syncRoot)
|
||||||
{
|
{
|
||||||
Iterator<MuxServerSocketChannel> i
|
for (Iterator<MuxServerSocketChannel> i
|
||||||
= muxServerSocketChannels.iterator();
|
= muxServerSocketChannels.iterator();
|
||||||
|
i.hasNext();)
|
||||||
while (i.hasNext())
|
|
||||||
{
|
{
|
||||||
MuxServerSocketChannel aChannel = i.next();
|
MuxServerSocketChannel aChannel = i.next();
|
||||||
|
|
||||||
@ -989,14 +1049,14 @@ class MuxingServerSocketChannel
|
|||||||
if (read > 0 || db.timestamp == -1)
|
if (read > 0 || db.timestamp == -1)
|
||||||
db.timestamp = now;
|
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;
|
remove = true;
|
||||||
}
|
}
|
||||||
else if (read <= 0
|
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
|
// The SocketChannel appears to have been abandoned by the
|
||||||
// client.
|
// client.
|
||||||
|
|||||||
66
src/main/java/org/ice4j/socket/jdk8/Timestamped.java
Normal file
66
src/main/java/org/ice4j/socket/jdk8/Timestamped.java
Normal file
@ -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 <T> the type of the {@code Object} which has a timestamp (associated)
|
||||||
|
* @author Lyubomir Marinov
|
||||||
|
*/
|
||||||
|
class Timestamped<T>
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user