423926 - Remove code duplication in class IdleTimeout.

Removed code duplications, and also removed method close(),
unnecessary since onClose() was performing the exact same code.

Also reviewed subclasses of IdleTimeout to make sure that they always
call onClose() when they are "closed", to make sure that the timeout
does not fire and that there are no memory leaks (the scheduler
holding a reference to the timeout task, which in turn holds a
reference to the IdleTimeout instance).
This commit is contained in:
Simone Bordet 2013-12-12 16:57:03 +01:00
parent 2d3d912173
commit cb6bacb11c
6 changed files with 16 additions and 14 deletions

View File

@ -109,7 +109,7 @@ public abstract class AbstractEndPoint extends IdleTimeout implements EndPoint
@Override
public void close()
{
super.close();
onClose();
}
@Override

View File

@ -285,6 +285,7 @@ public class ByteArrayEndPoint extends AbstractEndPoint
@Override
public void close()
{
super.close();
_closed=true;
}

View File

@ -108,6 +108,7 @@ public class ChannelEndPoint extends AbstractEndPoint
@Override
public void close()
{
super.close();
LOG.debug("close {}", this);
try
{

View File

@ -84,14 +84,12 @@ public abstract class IdleTimeout
return;
// old timeout is too long, so cancel it.
Scheduler.Task oldTimeout = _timeout.getAndSet(null);
if (oldTimeout != null)
oldTimeout.cancel();
deactivate();
}
// If we have a new timeout, then check and reschedule
if (idleTimeout > 0 && isOpen())
_idleTask.run();
if (isOpen())
activate();
}
/**
@ -113,6 +111,11 @@ public abstract class IdleTimeout
}
public void onOpen()
{
activate();
}
private void activate()
{
if (_idleTimeout > 0)
_idleTask.run();
@ -120,12 +123,10 @@ public abstract class IdleTimeout
public void onClose()
{
Scheduler.Task oldTimeout = _timeout.getAndSet(null);
if (oldTimeout != null)
oldTimeout.cancel();
deactivate();
}
protected void close()
private void deactivate()
{
Scheduler.Task oldTimeout = _timeout.getAndSet(null);
if (oldTimeout != null)

View File

@ -23,7 +23,6 @@ import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.util.Arrays;
import java.util.concurrent.Executor;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLEngineResult;
import javax.net.ssl.SSLEngineResult.HandshakeStatus;
@ -877,6 +876,7 @@ public class SslConnection extends AbstractConnection
@Override
public void close()
{
super.close();
// First send the TLS Close Alert, then the FIN
shutdownOutput();
getEndPoint().close();

View File

@ -21,10 +21,9 @@ package org.eclipse.jetty.io;
import java.util.concurrent.TimeoutException;
import junit.framework.Assert;
import org.eclipse.jetty.util.thread.TimerScheduler;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
@ -100,7 +99,7 @@ public class IdleTimeoutTest
Thread.sleep(100);
_timeout.notIdle();
}
_timeout.close();
_timeout.onClose();
Thread.sleep(1500);
Assert.assertNull(_expired);
}