Hi community, Recently, I've been working on traffic throttling filters, which prevents unwanted OutOfMemoryError due to high bandwidth asynchronous operations, and I want to get as much feed back as possible from you.
Currently, you can specify pre-programmed on-flood policy and set per-session, per-service and global threshold both for reads and for writes. I am still thinking out loudly about integrating traffic shaping into these filters. Any idea is welcome! Please browse the following directory, and feel free to give us some feed back: http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/filter/traffic/ Because the documentation is not ready yet, let me show you some examples: ReadThrottleFilterTest.java: public class ReadThrottleFilterTest { // Try to beat with the following command: // while true; do echo 1234567890; done | nc localhost 1234 public static void main(String[] args) throws Exception { SocketAcceptor acceptor = new NioSocketAcceptor(); acceptor.getFilterChain().addLast("executor", new ExecutorFilter()); acceptor.getFilterChain().addLast("readThrottle", new ReadThrottleFilter(ReadThrottlePolicy.BLOCK)); acceptor.setHandler(new IoHandlerAdapter() { @Override public void messageReceived(IoSession session, Object message) throws Exception { Thread.sleep(1000); } }); acceptor.setLocalAddress(new InetSocketAddress(1234)); acceptor.bind(); } } SlowServer.java and WriteThrottleFilterTest.java: (Launch the SlowServer and see if WriteThrottleFilterTest doesn't flood.) public class SlowServer { public static void main(String[] args) throws Exception { SocketAcceptor acceptor = new NioSocketAcceptor(); acceptor.getSessionConfig().setReceiveBufferSize(1024); acceptor.setLocalAddress(new InetSocketAddress(1234)); acceptor.setHandler(new IoHandlerAdapter() { @Override public void messageReceived(IoSession session, Object message) throws Exception { Thread.sleep(1000); } }); acceptor.bind(); } } --- and --- public class WriteThrottleFilterTest { public static void main(String[] args) throws Exception { SocketConnector connector = new NioSocketConnector(); connector.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new TextLineCodecFactory())); connector.getSessionConfig().setSendBufferSize(2048); connector.getFilterChain().addLast( "writeThrottle", new WriteThrottleFilter(WriteThrottlePolicy.BLOCK, 10, 0, 10, 0, 20, 0)); connector.setHandler(new IoHandlerAdapter()); IoSession session1 = connector.connect(new InetSocketAddress("localhost", 1234)).awaitUninterruptibly().getSession(); IoSession session2 = connector.connect(new InetSocketAddress("localhost", 1234)).awaitUninterruptibly().getSession(); String data = ""; for (int i = 0; i < 1024; i ++) { data += "."; } for (;;) { session1.write(data); session2.write(data); System.out.println(session1.getScheduledWriteMessages()); Thread.sleep(100); } } } -- what we call human nature is actually human habit -- http://gleamynode.net/ -- PGP Key ID: 0x0255ECA6
