[Nitro] de-bouncing Submit Order button

Bill Kelly billk at cts.com
Wed Oct 10 01:40:56 EDT 2007


Howdy!

I'm writing an online store, and I don't want mine to be one of
the ones that has to put bold-face warning text by the Submit
button, saying: DANGER: PRESS THIS BUTTON ONLY ONCE!

:)

My current plan is to have any and all POSTs from the Submit
button to redirect to a processing_order page, which uses a
meta refresh every three seconds or so to poll for the final
order completion page.

   Multiple Submit POSTs     does order processing?   redirects to:
     1st                        yes                      processing_order page
     2nd                        no                       processing_order page
     3rd                        no                       processing_order page

Coupled with that, I need a way to indicate that the current
order is in the processing state, and this has to be totally
atomic.

I'm not sure how fancy I need to get.

For example:

  unless session[:order_in_process]
    session[:order_in_process] = true
    # bill credit card
  end

That's essentially what I want to do, but I don't like the
potential race condition there.  I'll be using Mongrel to 
host my Nitro app.  My understanding is Mongrel uses ruby
green threads, and I'm not planning to run multiple Mongrel
proxys, just one.  I'm also using the MemorySessionStore.

So as far as I know, all my requests will be handled in the
context of a single ruby process.

If so, I presume I could eliminate the above race condition
with a global mutex...

  do_order_processing = false
  $order_sync_mutex.synchronize {
    unless session[:order_in_process]
      session[:order_in_process] = true
      do_order_processing = true
    end
  }

  if do_order_processing
    # bill credit card
  end

Anyway, I just wanted to ask if my approach seems reasonable,
and/or if I've missed some horrible loophole.


Thanks,

Bill




More information about the Nitro-general mailing list