Skip to content

Commit

Permalink
Issue #6251 - Use CyclicTimeout for HTTP2Streams.
Browse files Browse the repository at this point in the history
Updates after review.

Signed-off-by: Simone Bordet <simone.bordet@gmail.com>
  • Loading branch information
sbordet committed May 13, 2021
1 parent 37f7634 commit 7d203e1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ private RequestTimeouts(Scheduler scheduler)
}

@Override
public Iterator<HttpExchange> iterator()
protected Iterator<HttpExchange> iterator()
{
return exchanges.iterator();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2320,7 +2320,7 @@ private StreamTimeouts(Scheduler scheduler)
}

@Override
public Iterator<HTTP2Stream> iterator()
protected Iterator<HTTP2Stream> iterator()
{
return streams.values().stream().map(HTTP2Stream.class::cast).iterator();
}
Expand Down
33 changes: 24 additions & 9 deletions jetty-io/src/main/java/org/eclipse/jetty/io/CyclicTimeouts.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

import org.eclipse.jetty.util.component.Destroyable;
import org.eclipse.jetty.util.thread.Scheduler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* <p>A {@link CyclicTimeout} that manages the timeouts for many entities.</p>
* <p>An implementation of a timeout that manages many {@link Expirable expirable} entities whose
* timeouts are mostly cancelled or re-scheduled.</p>
* <p>A typical scenario is for a parent entity to manage the timeouts of many children entities.</p>
* <p>When a new entity is created, call {@link #schedule(T)} with the new entity so that
* this instance can be aware and manage the timeout of the new entity.</p>
Expand All @@ -37,24 +39,32 @@
* <p>When the iteration is complete, this instance is re-scheduled with the earliest expiration time
* calculated during the iteration.</p>
*
* @param <T> the entity type
* @param <T> the {@link Expirable} entity type
* @see CyclicTimeout
*/
public abstract class CyclicTimeouts<T extends CyclicTimeouts.Expirable> extends CyclicTimeout implements Iterable<T>
public abstract class CyclicTimeouts<T extends CyclicTimeouts.Expirable> implements Destroyable
{
private static final Logger LOG = LoggerFactory.getLogger(CyclicTimeouts.class);

private final AtomicLong earliestTimeout = new AtomicLong(Long.MAX_VALUE);
private final CyclicTimeout cyclicTimeout;

public CyclicTimeouts(Scheduler scheduler)
{
super(scheduler);
cyclicTimeout = new CyclicTimeout(scheduler)
{
@Override
public void onTimeoutExpired()
{
CyclicTimeouts.this.onTimeoutExpired();
}
};
}

/**
* @return the entities to iterate over when this instance expires
*/
@Override
public abstract Iterator<T> iterator();
protected abstract Iterator<T> iterator();

/**
* <p>Invoked during the iteration when the given entity is expired.</p>
Expand All @@ -64,8 +74,7 @@ public CyclicTimeouts(Scheduler scheduler)
*/
protected abstract boolean onExpired(T expirable);

@Override
public void onTimeoutExpired()
private void onTimeoutExpired()
{
if (LOG.isDebugEnabled())
LOG.debug("{} timeouts check", this);
Expand Down Expand Up @@ -124,10 +133,16 @@ private void schedule(long expiresAt)
long delay = Math.max(0, expiresAt - System.nanoTime());
if (LOG.isDebugEnabled())
LOG.debug("{} scheduling timeout in {} ms", this, TimeUnit.NANOSECONDS.toMillis(delay));
schedule(delay, TimeUnit.NANOSECONDS);
cyclicTimeout.schedule(delay, TimeUnit.NANOSECONDS);
}
}

@Override
public void destroy()
{
cyclicTimeout.destroy();
}

/**
* <p>An entity that may expire.</p>
*/
Expand Down

0 comments on commit 7d203e1

Please sign in to comment.