mina-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Trustin Lee" <trus...@gmail.com>
Subject Re: Proxy server example -- introduce a random delay from client to proxied server?
Date Wed, 22 Aug 2007 01:02:06 GMT
On 8/21/07, Maarten Bosteels <mbosteels.dns@gmail.com> wrote:
> On 8/21/07, Paul Furbacher <pfurbacher@mac.com> wrote:
> >
> >
> > Maarten,
> >
> > Thanks for the suggestion.  At first read, it seemed so obvious, so
> > simple.
> >
> > But besides a lot of debug error statements like the following
> >
> >   [...] ServerToProxyIoHandler - [localhost/127.0.0.1:9001]
> > java.lang.IllegalStateException: Timer already cancelled.
> >
> > and
> >
> >   [...] ClientToProxyIoHandler - [/127.0.0.1:60974]
> > java.lang.IllegalStateException: Timer already cancelled. ...
>
>
> seems  like you  added the  TimerTask to both ClientToProxy and
> ServerToProxy.
> I only added  it to ClientToProxy and didn't get these errors.
> But it is always possible that the proxied server closes the connection
> before you could forward the request.
>
> the messages arrive in the same order they were dispatched by the client.
>
>
> That's odd, maybe you need higher variation in the delay ?
> Do you have control over the server ? I mean, can you ensure that it does
> not close the connections too early ?
>
> The desired effect of having the randomized delay is to scramble the message
> > order, something like the following:
> >
> > Client send order   1 2 3 4 5 . . .
> >
> > Client receive order   3 1 4 5 2 . . .
> >
> > Perhaps an IoHandler is not the right place to do this? Would an IoFilter
> > be
> > more appropriate?
>
>
> I guess you only want to randomize the messages to test the server ?
> Wouldn't it be simpler then to write a test-case that sends a hard-coded
> list of messages (randomized by hand) ?

Giving delay just randomly can cause mixed order of messages.  For
example, giving 500ms delay to the msg #1 and 1ms to #2 can switch the
order of messages.  The following code will work better:

   private volatile long lastScheduledTime = System.currentTimeMillis();
   private final Random random = new Random();
   private final Timer timer = new Timer();

   public void messageReceived(final IoSession session, final Object
message) throws Exception {
       final int c = counter.incrementAndGet();
       ByteBuffer rb = (ByteBuffer) message;
       final ByteBuffer wb = ByteBuffer.allocate(rb.remaining());
       rb.mark();
       wb.put(rb);
       wb.flip();
       rb.reset();
       long scheduledTime = lastScheduledTime + random.nextInt(500);
       final IoSession proxySession = (IoSession) session.getAttachment();
       timer.schedule(
               new TimerTask() {
                   public void run() {
                       proxySession.write(wb);
                   }
               }, new Date(scheduledTime));;
       lastScheduledTime = scheduledTime;
   }

HTH,
Trustin
-- 
what we call human nature is actually human habit
--
http://gleamynode.net/
--
PGP Key ID: 0x0255ECA6

Mime
View raw message