Skip to content

Commit

Permalink
Merged branch 'jetty-12.0.x' into 'jetty-12.1.x'.
Browse files Browse the repository at this point in the history
Signed-off-by: Simone Bordet <[email protected]>
  • Loading branch information
sbordet committed Sep 20, 2024
2 parents 838b97d + 115ee1c commit 3b1f38e
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -653,11 +653,11 @@ protected void exitScope(Request request, Context lastContext, ClassLoader lastL
*/
protected void notifyExitScope(Request request)
{
for (int i = _contextListeners.size(); i-- > 0; )
for (ContextScopeListener listener : TypeUtil.reverse(_contextListeners))
{
try
{
_contextListeners.get(i).exitScope(_context, request);
listener.exitScope(_context, request);
}
catch (Throwable e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,36 +79,6 @@ public ConcurrentPool(StrategyType strategyType, int maxSize)
this(strategyType, maxSize, pooled -> 1);
}

/**
* <p>Creates an instance with the specified strategy.</p>
*
* @param strategyType the strategy to used to lookup entries
* @param maxSize the maximum number of pooled entries
* @param cache whether a {@link ThreadLocal} cache should be used for the most recently released entry
* @deprecated cache is no longer supported. Use {@link StrategyType#THREAD_ID}
*/
@Deprecated
public ConcurrentPool(StrategyType strategyType, int maxSize, boolean cache)
{
this(strategyType, maxSize, pooled -> 1);
}

/**
* <p>Creates an instance with the specified strategy.
* and a function that returns the max multiplex count for a given pooled object.</p>
*
* @param strategyType the strategy to used to lookup entries
* @param maxSize the maximum number of pooled entries
* @param cache whether a {@link ThreadLocal} cache should be used for the most recently released entry
* @param maxMultiplex a function that given the pooled object returns the max multiplex count
* @deprecated cache is no longer supported. Use {@link StrategyType#THREAD_ID}
*/
@Deprecated
public ConcurrentPool(StrategyType strategyType, int maxSize, boolean cache, ToIntFunction<P> maxMultiplex)
{
this(strategyType, maxSize, maxMultiplex);
}

/**
* <p>Creates an instance with the specified strategy.
* and a function that returns the max multiplex count for a given pooled object.</p>
Expand Down Expand Up @@ -148,7 +118,7 @@ private void leaked(Holder<P> holder)
{
leaked.increment();
if (LOG.isDebugEnabled())
LOG.debug("Leaked " + holder);
LOG.debug("Leaked {}", holder);
leaked();
}

Expand Down Expand Up @@ -195,15 +165,14 @@ public Entry<P> reserve()

void sweep()
{
for (int i = 0; i < entries.size(); i++)
// Remove entries atomically with respect to remove(Entry).
entries.removeIf(holder ->
{
Holder<P> holder = entries.get(i);
if (holder.getEntry() == null)
{
entries.remove(i--);
boolean remove = holder.getEntry() == null;
if (remove)
leaked(holder);
}
}
return remove;
});
}

@Override
Expand Down Expand Up @@ -285,8 +254,7 @@ private boolean remove(Entry<P> entry)
if (!removed)
return false;

// No need to lock, no race with reserve()
// and the race with terminate() is harmless.
// In a harmless race with reserve()/sweep()/terminate().
Holder<P> holder = ((ConcurrentEntry<P>)entry).getHolder();
boolean evicted = entries.remove(holder);
if (LOG.isDebugEnabled())
Expand All @@ -313,10 +281,7 @@ public Collection<Entry<P>> terminate()
// Field this.terminated must be modified with the lock held
// because the list of entries is modified, see reserve().
terminated = true;
copy = entries.stream()
.map(Holder::getEntry)
.filter(Objects::nonNull)
.toList();
copy = stream().toList();
entries.clear();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,21 @@ public static <T> List<T> asList(T[] a)
return Arrays.asList(a);
}

/**
* <p>Returns a new list with the elements of the specified list in reverse order.</p>
* <p>The specified list is not modified, differently from {@link Collections#reverse(List)}.</p>
*
* @param list the list whose elements are to be reversed
* @return a new list with the elements in reverse order
* @param <T> the element type
*/
public static <T> List<T> reverse(List<T> list)
{
List<T> result = new ArrayList<>(list);
Collections.reverse(result);
return result;
}

/**
* Class from a canonical name for a type.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
import org.eclipse.jetty.util.ExceptionUtil;
import org.eclipse.jetty.util.Loader;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.TypeUtil;
import org.eclipse.jetty.util.URIUtil;
import org.eclipse.jetty.util.annotation.ManagedAttribute;
import org.eclipse.jetty.util.annotation.ManagedObject;
Expand Down Expand Up @@ -516,8 +517,7 @@ public void contextDestroyed() throws Exception
//Call context listeners
Throwable multiException = null;
ServletContextEvent event = new ServletContextEvent(getServletContext());
Collections.reverse(_destroyServletContextListeners);
for (ServletContextListener listener : _destroyServletContextListeners)
for (ServletContextListener listener : TypeUtil.reverse(_destroyServletContextListeners))
{
try
{
Expand Down Expand Up @@ -568,17 +568,17 @@ protected void requestDestroyed(Request baseRequest, HttpServletRequest request)
if (!_servletRequestListeners.isEmpty())
{
final ServletRequestEvent sre = new ServletRequestEvent(getServletContext(), request);
for (int i = _servletRequestListeners.size(); i-- > 0; )
for (ServletRequestListener listener : TypeUtil.reverse(_servletRequestListeners))
{
_servletRequestListeners.get(i).requestDestroyed(sre);
listener.requestDestroyed(sre);
}
}

if (!_servletRequestAttributeListeners.isEmpty())
{
for (int i = _servletRequestAttributeListeners.size(); i-- > 0; )
for (ServletRequestAttributeListener listener : TypeUtil.reverse(_servletRequestAttributeListeners))
{
scopedRequest.removeEventListener(_servletRequestAttributeListeners.get(i));
scopedRequest.removeEventListener(listener);
}
}
}
Expand Down Expand Up @@ -1217,11 +1217,11 @@ protected void notifyExitScope(Request request)
ServletContextRequest scopedRequest = Request.as(request, ServletContextRequest.class);
if (!_contextListeners.isEmpty())
{
for (int i = _contextListeners.size(); i-- > 0; )
for (ServletContextScopeListener listener : TypeUtil.reverse(_contextListeners))
{
try
{
_contextListeners.get(i).exitScope(getContext(), scopedRequest);
listener.exitScope(getContext(), scopedRequest);
}
catch (Throwable e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.eclipse.jetty.session.ManagedSession;
import org.eclipse.jetty.session.SessionConfig;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.util.TypeUtil;

public class SessionHandler extends AbstractSessionManager implements Handler.Singleton
{
Expand Down Expand Up @@ -571,9 +572,9 @@ public void onSessionDestroyed(Session session)
getSessionContext().run(() ->
{
HttpSessionEvent event = new HttpSessionEvent(session.getApi());
for (int i = _sessionListeners.size() - 1; i >= 0; i--)
for (HttpSessionListener listener : TypeUtil.reverse(_sessionListeners))
{
_sessionListeners.get(i).sessionDestroyed(event);
listener.sessionDestroyed(event);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
import org.eclipse.jetty.util.ExceptionUtil;
import org.eclipse.jetty.util.Loader;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.TypeUtil;
import org.eclipse.jetty.util.URIUtil;
import org.eclipse.jetty.util.annotation.ManagedAttribute;
import org.eclipse.jetty.util.annotation.ManagedObject;
Expand Down Expand Up @@ -515,8 +516,7 @@ public void contextDestroyed() throws Exception
//Call context listeners
Throwable multiException = null;
ServletContextEvent event = new ServletContextEvent(getServletContext());
Collections.reverse(_destroyServletContextListeners);
for (ServletContextListener listener : _destroyServletContextListeners)
for (ServletContextListener listener : TypeUtil.reverse(_destroyServletContextListeners))
{
try
{
Expand Down Expand Up @@ -566,18 +566,18 @@ protected void requestDestroyed(Request baseRequest, HttpServletRequest request)
// Handle more REALLY SILLY request events!
if (!_servletRequestListeners.isEmpty())
{
final ServletRequestEvent sre = new ServletRequestEvent(getServletContext(), request);
for (int i = _servletRequestListeners.size(); i-- > 0; )
ServletRequestEvent sre = new ServletRequestEvent(getServletContext(), request);
for (ServletRequestListener listener : TypeUtil.reverse(_servletRequestListeners))
{
_servletRequestListeners.get(i).requestDestroyed(sre);
listener.requestDestroyed(sre);
}
}

if (!_servletRequestAttributeListeners.isEmpty())
{
for (int i = _servletRequestAttributeListeners.size(); i-- > 0; )
for (ServletRequestAttributeListener listener : TypeUtil.reverse(_servletRequestAttributeListeners))
{
scopedRequest.removeEventListener(_servletRequestAttributeListeners.get(i));
scopedRequest.removeEventListener(listener);
}
}
}
Expand Down Expand Up @@ -1217,11 +1217,11 @@ protected void notifyExitScope(Request request)
ServletContextRequest scopedRequest = Request.as(request, ServletContextRequest.class);
if (!_contextListeners.isEmpty())
{
for (int i = _contextListeners.size(); i-- > 0; )
for (ServletContextScopeListener listener : TypeUtil.reverse(_contextListeners))
{
try
{
_contextListeners.get(i).exitScope(getContext(), scopedRequest);
listener.exitScope(getContext(), scopedRequest);
}
catch (Throwable e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.eclipse.jetty.session.ManagedSession;
import org.eclipse.jetty.session.SessionConfig;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.util.TypeUtil;

public class SessionHandler extends AbstractSessionManager implements Handler.Singleton
{
Expand Down Expand Up @@ -615,9 +616,9 @@ public void onSessionDestroyed(Session session)
getSessionContext().run(() ->
{
HttpSessionEvent event = new HttpSessionEvent(session.getApi());
for (int i = _sessionListeners.size() - 1; i >= 0; i--)
for (HttpSessionListener listener : TypeUtil.reverse(_sessionListeners))
{
_sessionListeners.get(i).sessionDestroyed(event);
listener.sessionDestroyed(event);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1000,17 +1000,17 @@ protected void requestDestroyed(Request baseRequest, HttpServletRequest request)
if (!_servletRequestListeners.isEmpty())
{
final ServletRequestEvent sre = new ServletRequestEvent(_apiContext, request);
for (int i = _servletRequestListeners.size(); i-- > 0; )
for (ServletRequestListener listener : TypeUtil.reverse(_servletRequestListeners))
{
_servletRequestListeners.get(i).requestDestroyed(sre);
listener.requestDestroyed(sre);
}
}

if (!_servletRequestAttributeListeners.isEmpty())
{
for (int i = _servletRequestAttributeListeners.size(); i-- > 0; )
for (ServletRequestAttributeListener listener : TypeUtil.reverse(_servletRequestAttributeListeners))
{
baseRequest.removeEventListener(_servletRequestAttributeListeners.get(i));
baseRequest.removeEventListener(listener);
}
}
}
Expand Down Expand Up @@ -1070,11 +1070,11 @@ protected void exitScope(Request request)
{
if (!_contextListeners.isEmpty())
{
for (int i = _contextListeners.size(); i-- > 0; )
for (ContextScopeListener listener : TypeUtil.reverse(_contextListeners))
{
try
{
_contextListeners.get(i).exitScope(_apiContext, request);
listener.exitScope(_apiContext, request);
}
catch (Throwable e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.eclipse.jetty.session.SessionManager;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.TypeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -834,9 +835,9 @@ public void onSessionDestroyed(Session session)
Runnable r = () ->
{
HttpSessionEvent event = new HttpSessionEvent(session.getApi());
for (int i = _sessionListeners.size() - 1; i >= 0; i--)
for (HttpSessionListener listener : TypeUtil.reverse(_sessionListeners))
{
_sessionListeners.get(i).sessionDestroyed(event);
listener.sessionDestroyed(event);
}
};
_contextHandler.getCoreContextHandler().getContext().run(r);
Expand Down

0 comments on commit 3b1f38e

Please sign in to comment.