public class InMemoryConnectionStore extends Object implements ResumptionSupportingConnectionStore, CloseSupportingConnectionStore
ResumptionSupportingConnectionStore with a
configurable maximum capacity and support for evicting stale connections
based on a least recently used policy.
The store keeps track of the connections' last-access time automatically. Every time a connection is read from or put to the store the access-time is updated.
A connection can be successfully added to the store if any of the following conditions is met:
This implementation uses three java.util.HashMap. One with a
connection's id as key as its backing store, one with the peer address as
key, and one with the session id as key. In addition to that the store keeps
a doubly-linked list of the connections in access-time order.
Insertion, lookup and removal of connections is done in O(log n).
Storing and reading to/from the store is thread safe.
Supports also a SessionCache implementation to keep sessions for
longer or in a distribute system. If this store evicts a connection in order
to gain storage for new connections, the associated session remains in the
cache. Therefore the cache requires a own, independent cleanup for stale
sessions. If a connection is removed by a critical ALERT, the session get's
removed also from the cache.
| Modifier and Type | Field and Description |
|---|---|
protected org.eclipse.californium.elements.util.LeastRecentlyUsedCache<ConnectionId,Connection> |
connections |
protected ConcurrentMap<InetSocketAddress,Connection> |
connectionsByAddress |
protected ConcurrentMap<SessionId,Connection> |
connectionsByEstablishedSession |
protected String |
tag |
| Constructor and Description |
|---|
InMemoryConnectionStore()
Creates a store with a capacity of 500000 connections and
a connection expiration threshold of 36 hours.
|
InMemoryConnectionStore(int capacity,
long threshold)
Creates a store based on given configuration parameters.
|
InMemoryConnectionStore(int capacity,
long threshold,
SessionCache sessionCache)
Creates a store based on given configuration parameters.
|
InMemoryConnectionStore(SessionCache sessionCache)
Creates a store with a capacity of 500000 connections and a connection
expiration threshold of 36 hours.
|
| Modifier and Type | Method and Description |
|---|---|
void |
attach(ConnectionIdGenerator connectionIdGenerator)
Attach connection id generator.
|
void |
clear()
Removes all connections from the store.
|
Connection |
find(SessionId id)
Finds a connection by its session ID.
|
Connection |
get(ConnectionId cid)
Gets a connection by its connection id.
|
Connection |
get(InetSocketAddress peerAddress)
Gets a connection by its peer address.
|
Iterator<Connection> |
iterator()
Get "weakly consistent" iterator over all connections.
|
void |
markAllAsResumptionRequired()
Mark all connections as resumption required.
|
boolean |
put(Connection connection)
Puts a connection into the store.
|
void |
putEstablishedSession(DTLSSession session,
Connection connection)
Associates the connection with the session id.
|
int |
remainingCapacity()
Gets the number of additional connection this store can manage.
|
boolean |
remove(Connection connection)
Removes a connection from the store and session cache.
|
boolean |
remove(Connection connection,
boolean removeFromSessionCache)
Removes a connection from the store and optional from the session cache.
|
boolean |
removeFromAddress(Connection connection)
Deprecated.
since 2.3 obsolete, see
Connection.close(Record). |
void |
removeFromEstablishedSessions(DTLSSession session,
Connection connection)
Remove the association of the connection with the session id.
|
void |
setConnectionListener(ConnectionListener listener) |
InMemoryConnectionStore |
setTag(String tag)
Set tag for logging outputs.
|
void |
stop(List<Runnable> pending)
Stop all serial executors of connections from the store.
|
boolean |
update(Connection connection,
InetSocketAddress newPeerAddress)
Update a connection in the store.
|
protected final org.eclipse.californium.elements.util.LeastRecentlyUsedCache<ConnectionId,Connection> connections
protected final ConcurrentMap<InetSocketAddress,Connection> connectionsByAddress
protected final ConcurrentMap<SessionId,Connection> connectionsByEstablishedSession
protected String tag
public InMemoryConnectionStore()
public InMemoryConnectionStore(SessionCache sessionCache)
sessionCache - a second level cache to use for current
connection state of established DTLS sessions.public InMemoryConnectionStore(int capacity,
long threshold)
capacity - the maximum number of connections the store can managethreshold - the period of time of inactivity (in seconds) after which a
connection is considered stale and can be evicted from the store if
a new connection is to be added to the storepublic InMemoryConnectionStore(int capacity,
long threshold,
SessionCache sessionCache)
capacity - the maximum number of connections the store can managethreshold - the period of time of inactivity (in seconds) after
which a connection is considered stale and can be evicted from
the store if a new connection is to be added to the storesessionCache - a second level cache to use for current
connection state of established DTLS sessions. If implements
ClientSessionCache, restore connection from the cache
and mark them to resume.public InMemoryConnectionStore setTag(String tag)
tag - tag for loggingpublic void setConnectionListener(ConnectionListener listener)
setConnectionListener in interface ResumptionSupportingConnectionStorepublic void attach(ConnectionIdGenerator connectionIdGenerator)
ResumptionSupportingConnectionStoreResumptionSupportingConnectionStore.put(Connection).attach in interface ResumptionSupportingConnectionStoreconnectionIdGenerator - connection id generator. If null a
default connection id generator is created.public boolean put(Connection connection)
Connection.getConnectionId(). If the connection doesn't have a
connection id, a unique connection id created with the
ResumptionSupportingConnectionStore.attach(ConnectionIdGenerator) is assigned. If the connection has
also a peer address and/or a established session, it get's associated
with that as well. It removes also an other connection from these
associations.
Note: ResumptionSupportingConnectionStore.attach(ConnectionIdGenerator) must be called before!
A connection can be successfully added to the store if any of the following conditions is met:
put in interface ResumptionSupportingConnectionStoreconnection - the connection to storetrue if the connection could be stored, false,
otherwise (e.g. because the store's capacity is exhausted)ResumptionSupportingConnectionStore.get(ConnectionId),
ResumptionSupportingConnectionStore.get(InetSocketAddress),
ResumptionSupportingConnectionStore.find(SessionId)public boolean update(Connection connection, InetSocketAddress newPeerAddress)
ResumptionSupportingConnectionStoreupdate in interface ResumptionSupportingConnectionStoreconnection - the connection to update.newPeerAddress - the (new) peer address. If null, don't
update the connection's address.true, if updated, false, otherwise.@Deprecated public boolean removeFromAddress(Connection connection)
Connection.close(Record).removeFromAddress in interface CloseSupportingConnectionStoreconnection - the connection to update.true, if removed, false, otherwise.public void putEstablishedSession(DTLSSession session, Connection connection)
ResumptionSupportingConnectionStoreputEstablishedSession in interface ResumptionSupportingConnectionStoresession - established session.connection - connection of established sessionpublic void removeFromEstablishedSessions(DTLSSession session, Connection connection)
ResumptionSupportingConnectionStoreremoveFromEstablishedSessions in interface ResumptionSupportingConnectionStoresession - established session.connection - connection of established sessionpublic Connection find(SessionId id)
ResumptionSupportingConnectionStorefind in interface ResumptionSupportingConnectionStoreid - the session IDnull if no connection
with an established session with the given ID existspublic void markAllAsResumptionRequired()
ResumptionSupportingConnectionStoremarkAllAsResumptionRequired in interface ResumptionSupportingConnectionStorepublic int remainingCapacity()
ResumptionSupportingConnectionStoreremainingCapacity in interface ResumptionSupportingConnectionStorepublic Connection get(InetSocketAddress peerAddress)
ResumptionSupportingConnectionStoreget in interface ResumptionSupportingConnectionStorepeerAddress - the peer addressnull if no connection
exists for the given addresspublic Connection get(ConnectionId cid)
ResumptionSupportingConnectionStoreget in interface ResumptionSupportingConnectionStorecid - connection idnull if no connection
exists for the given connection idpublic boolean remove(Connection connection)
ResumptionSupportingConnectionStoreremove in interface ResumptionSupportingConnectionStoreconnection - the connection to removetrue if the connection was removed,
false, otherwisepublic boolean remove(Connection connection, boolean removeFromSessionCache)
ResumptionSupportingConnectionStoreremove in interface ResumptionSupportingConnectionStoreconnection - the connection to removeremoveFromSessionCache - true if the session of the
connection should be removed from the session cache,
false, otherwisetrue if the connection was removed,
false, otherwisepublic final void clear()
ResumptionSupportingConnectionStoreclear in interface ResumptionSupportingConnectionStorepublic final void stop(List<Runnable> pending)
ResumptionSupportingConnectionStorestop in interface ResumptionSupportingConnectionStorepending - list to add pending jobspublic Iterator<Connection> iterator()
ConcurrentModificationException, and guarantees to traverse
elements as they existed upon construction of the iterator, and may (but
is not guaranteed to) reflect any modifications subsequent to
construction.iterator in interface ResumptionSupportingConnectionStoreLeastRecentlyUsedCache.valuesIterator()Copyright © 2023 Eclipse Foundation. All rights reserved.