Hello All,
I have a simple class that is used to track the number of active
connections per IP address. At the moment it has 2 synchronized
methods: addConnection(String IP) and removeConnection(String IP).
In my IoHandler sessionOpened I do the following:
// get the remote IP from session, then:
if (IP != null) {
session.setAttribute(ClientTracker.TYPE.ALLIP, IP);
ClientTracker.getInstance().addConnection(IP, ClientTracker.TYPE.ALLIP);
}
in the sessionClosed I have the following code:
final Object IP = session.getAttribute(ClientTracker.TYPE.ALLIP);
if (IP != null) {
ClientTracker.getInstance().removeConnection((String) IP,
ClientTracker.TYPE.ALLIP);
}
I'm using the OrderedThreadPool for the acceptor which should
guarantee the correct order of sessionOpened and sessionClosed events.
THE PROBLEM: after some hours of running when I get the stats from the
ClientTracker, there are IP addresses with several connections listed,
while actually there are no such connections to the server (checked
with netstat).
When I get the sessions from the acceptor using getManagedSessions()
and count connections, the actual numbers are less that reported by my
ClientTracker class and match netstat report.
Idle period is 60 seconds and I do close sessions from exceptionCaught
and sessionIdle overridden methods in my IoHandler. I also do print
all the exceptions that are not IOException to the log and don't see
anything fatal reported from MINA.
It looks like in some rare cases sessionClosed is not called at all or
is called before sessionOpened. The weird thing is that I'm using the
same ClientTracker class in the connector (NioSocketConnector) with
another instance of the OrderedThreadPool and connector connections
always match those reported by getManagedSessions(). So it seems to be
specific to the acceptor (NioSocketAcceptor).
You'd say that I should use getManagedSessions() for this task, but
I'm curious why such thing happens as it may produce unpredictable
problems in the more complicated situations when you rely on the fact
that sessionOpened and sessionClosed are called in the right order and
that each call to the first one is matched by the call to the second
one.
Any ideas?
(Using MINA 2.0.0-M4(RC1 trunk) on CentOS 5.2, Sun JDK 1.6.0_12).
--
Best regards,
Serge