Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Copyright (C) 2015-2024 Red Hat, Inc. (https://github.com/Commonjava/jhttpc)
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -45,12 +45,14 @@ public class ConnectionManagerCache
private static final long EXPIRATION_MILLIS = TimeUnit.MILLISECONDS.convert( EXPIRATION_SECONDS, TimeUnit.SECONDS );

private final Map<SiteConnectionConfig, ConnectionManagerTracker> cache =
new HashMap<SiteConnectionConfig, ConnectionManagerTracker>();
new HashMap<>();

private final Timer timer = new Timer( "jhttpc-connection-manager-cache", true );

private final Logger logger = LoggerFactory.getLogger( getClass() );

private boolean shutdown;

public ConnectionManagerCache()
{
timer.scheduleAtFixedRate( new ExpirationSweeper( this ), EXPIRATION_MILLIS, EXPIRATION_MILLIS );
Expand All @@ -60,7 +62,7 @@ public synchronized void expireTrackersOlderThan( long duration, TimeUnit unit )
{
long expiration = System.currentTimeMillis() - TimeUnit.MILLISECONDS.convert( duration, unit );

for ( SiteConnectionConfig config : new HashSet<SiteConnectionConfig>( cache.keySet() ) )
for ( SiteConnectionConfig config : new HashSet<>( cache.keySet() ) )
{
ConnectionManagerTracker tracker = cache.get( config );
if ( tracker != null && tracker.getLastRetrieval() < expiration )
Expand All @@ -80,13 +82,12 @@ public synchronized void expireTrackersOlderThan( long duration, TimeUnit unit )
public synchronized ConnectionManagerTracker getTrackerFor( SiteConnectionConfig config )
throws JHttpCException
{
ConnectionManagerTracker tracker = cache.get( config );
if ( tracker == null )
if ( shutdown )
{
tracker = new ConnectionManagerTracker( config, this );
cache.put( config, tracker );
throw new JHttpCException( "Connection manager cache already shut down for: %s", null, config );
}

ConnectionManagerTracker tracker = cache.computeIfAbsent( config, c -> new ConnectionManagerTracker( c, this ) );
return tracker.retrieved();
}

Expand Down Expand Up @@ -147,6 +148,8 @@ private synchronized boolean doShutdown( Function<ConnectionManagerTracker, Bool
}

timer.cancel();
shutdown = true;
cache.clear();

return result;
}
Expand Down Expand Up @@ -186,7 +189,7 @@ static final class ExpirationSweeper
extends TimerTask
{

private ConnectionManagerCache cache;
private final ConnectionManagerCache cache;

public ExpirationSweeper( ConnectionManagerCache cache )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,23 @@ private boolean tryShutdown()
{
if ( detached && !isActive() )
{
manager.reallyShutdown();
closeManager();
managerCache.remove( config );
return true;
}

return false;
}

private void closeManager()
{
if ( manager != null )
{
manager.reallyShutdown();
manager = null;
}
}

public long getLastRetrieval()
{
return lastRetrieval;
Expand Down Expand Up @@ -180,7 +189,7 @@ public boolean isShutdown()
@Override
public synchronized boolean shutdownNow()
{
manager.reallyShutdown();
closeManager();
return true;
}

Expand All @@ -196,7 +205,7 @@ public synchronized boolean shutdownGracefully( final long timeoutMillis )

if ( !isActive() )
{
manager.reallyShutdown();
closeManager();
return true;
}

Expand Down
Loading