Changeset 9bc54f2


Ignore:
Timestamp:
Sep 8, 2012 9:56:05 PM (8 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
74e7539
Parents:
d9e6c06
Message:
  • ClientManager?:
    • Concurrent client map for faster lookup
    • Add by-hash client map for faster lookup by hash
    • More cleanups
File:
1 edited

Legend:

Unmodified
Added
Removed
  • router/java/src/net/i2p/router/client/ClientManager.java

    rd9e6c06 r9bc54f2  
    1414import java.util.HashSet;
    1515import java.util.Iterator;
     16import java.util.Map;
    1617import java.util.Set;
    1718import java.util.concurrent.LinkedBlockingQueue;
     19import java.util.concurrent.ConcurrentHashMap;
    1820
    1921import net.i2p.client.I2PSessionException;
     
    4345    private final Log _log;
    4446    private ClientListenerRunner _listener;
    45     private final HashMap<Destination, ClientConnectionRunner>  _runners;        // Destination --> ClientConnectionRunner
    46     private final Set<ClientConnectionRunner> _pendingRunners; // ClientConnectionRunner for clients w/out a Dest yet
     47    // Destination --> ClientConnectionRunner
     48    // Locked for adds/removes but not lookups
     49    private final Map<Destination, ClientConnectionRunner>  _runners;
     50    // Same as what's in _runners, but for fast lookup by Hash
     51    // Locked for adds/removes but not lookups
     52    private final Map<Hash, ClientConnectionRunner>  _runnersByHash;
     53    // ClientConnectionRunner for clients w/out a Dest yet
     54    private final Set<ClientConnectionRunner> _pendingRunners;
    4755    private final RouterContext _ctx;
    4856    private volatile boolean _isStarted;
     
    6270        //                                      "ClientMessages",
    6371        //                                      new long[] { 60*1000l, 60*60*1000l, 24*60*60*1000l });
    64         _runners = new HashMap();
     72        _runners = new ConcurrentHashMap();
     73        _runnersByHash = new ConcurrentHashMap();
    6574        _pendingRunners = new HashSet();
    6675        startListeners(port);
     
    117126            runner.disconnectClient(msg, Log.WARN);
    118127        }
     128        _runnersByHash.clear();
    119129    }
    120130   
     
    160170        if ( (runner.getConfig() != null) && (runner.getConfig().getDestination() != null) ) {
    161171            // after connection establishment
     172            Destination dest = runner.getConfig().getDestination();
    162173            synchronized (_runners) {
    163                 _runners.remove(runner.getConfig().getDestination());
     174                _runners.remove(dest);
     175                _runnersByHash.remove(dest.calculateHash());
    164176            }
    165177        }
     
    179191        boolean fail = false;
    180192        synchronized (_runners) {
    181             fail = _runners.containsKey(dest);
    182             if (!fail)
     193            fail = _runnersByHash.containsKey(dest.calculateHash());
     194            if (!fail) {
    183195                _runners.put(dest, runner);
     196                _runnersByHash.put(dest.calculateHash(), runner);
     197            }
    184198        }
    185199        if (fail) {
     
    291305    }
    292306   
     307    /**
     308     *  Unsynchronized
     309     */
    293310    public boolean isLocal(Destination dest) {
    294         boolean rv = false;
    295         long beforeLock = _ctx.clock().now();
    296         long inLock = 0;
    297         synchronized (_runners) {
    298             inLock = _ctx.clock().now();
    299             rv = _runners.containsKey(dest);
    300         }
    301         long afterLock = _ctx.clock().now();
    302 
    303         if (afterLock - beforeLock > 50) {
    304             _log.warn("isLocal(Destination).locking took too long: " + (afterLock-beforeLock)
    305                       + " overall, synchronized took " + (inLock - beforeLock));
    306         }
    307         return rv;
    308     }
    309 
     311        return _runners.containsKey(dest);
     312    }
     313
     314    /**
     315     *  Unsynchronized
     316     */
    310317    public boolean isLocal(Hash destHash) {
    311318        if (destHash == null) return false;
    312         synchronized (_runners) {
    313             for (Iterator iter = _runners.values().iterator(); iter.hasNext(); ) {
    314                 ClientConnectionRunner cur = (ClientConnectionRunner)iter.next();
    315                 if (destHash.equals(cur.getDestHash())) return true;
    316             }
    317         }
    318         return false;
     319        return _runnersByHash.containsKey(destHash);
    319320    }
    320321   
     
    329330    }
    330331
     332    /**
     333     *  Unsynchronized
     334     */
    331335    public Set<Destination> listClients() {
    332336        Set<Destination> rv = new HashSet();
    333         synchronized (_runners) {
    334             rv.addAll(_runners.keySet());
    335         }
     337        rv.addAll(_runners.keySet());
    336338        return rv;
    337339    }
    338340
    339341   
     342    /**
     343     *  Unsynchronized
     344     */
    340345    ClientConnectionRunner getRunner(Destination dest) {
    341         ClientConnectionRunner rv = null;
    342         long beforeLock = _ctx.clock().now();
    343         long inLock = 0;
    344         synchronized (_runners) {
    345             inLock = _ctx.clock().now();
    346             rv = _runners.get(dest);
    347         }
    348         long afterLock = _ctx.clock().now();
    349         if (afterLock - beforeLock > 50) {
    350             _log.warn("getRunner(Dest).locking took too long: " + (afterLock-beforeLock)
    351                       + " overall, synchronized took " + (inLock - beforeLock));
    352         }
    353         return rv;
     346        return _runners.get(dest);
    354347    }
    355348   
     
    379372    }
    380373   
     374    /**
     375     *  Unsynchronized
     376     */
    381377    private ClientConnectionRunner getRunner(Hash destHash) {
    382378        if (destHash == null)
    383379            return null;
    384         synchronized (_runners) {
    385             for (Iterator<ClientConnectionRunner> iter = _runners.values().iterator(); iter.hasNext(); ) {
    386                 ClientConnectionRunner cur = iter.next();
    387                 if (cur.getDestHash().equals(destHash))
    388                     return cur;
    389             }
    390         }
    391         return null;
     380        return _runnersByHash.get(destHash);
    392381    }
    393382   
     
    408397    Set<Destination> getRunnerDestinations() {
    409398        Set<Destination> dests = new HashSet();
    410         long beforeLock = _ctx.clock().now();
    411         long inLock = 0;
    412         synchronized (_runners) {
    413             inLock = _ctx.clock().now();
    414             dests.addAll(_runners.keySet());
    415         }
    416         long afterLock = _ctx.clock().now();
    417         if (afterLock - beforeLock > 50) {
    418             _log.warn("getRunnerDestinations().locking took too long: " + (afterLock-beforeLock)
    419                       + " overall, synchronized took " + (inLock - beforeLock));
    420         }
    421        
     399        dests.addAll(_runners.keySet());
    422400        return dests;
    423401    }
Note: See TracChangeset for help on using the changeset viewer.