Question about integrating udns in Ruby EventMachine

Iñaki Baz Castillo ibc at aliax.net
Mon Feb 7 13:18:23 MSK 2011


2011/2/7 Michael Tokarev <mjt at tls.msk.ru>:
>> But in my case I cannot control poll() (EventMachine does it for me).
>> But I think the following can work (I would say it's lredy working in
>> my code):
>
> Um, that's somewhat ugly.  It may work, but it's over-complicated, in
> my opinion.  It is difficult for me to believe that your EventMachine
> does not allow one to hook into the place before poll/select (usually
> they do), but even if it's so, maybe the timer callback I mentioned
> above will do the trick.  It is a bit more difficult to understand
> (and hence use right), however.

Hi, the fact is that I implement it with EventMachine timers. Let me
show my Ruby code doing the job:

-------------------------------------------
  class Resolver

    def set_timeouts
       @timeouts_timer.cancel() unless not @timeouts_timer
       if next_timer = timeouts()
          @timeouts_timer = EM::Timer.new(next_timer) do
             set_timeouts() }
          end
       end

  end
-------------------------------------------

- After dns_submit_xxx() is called, the above set_timeouts() Ruby
method is called.

- This method calls Ruby timeouts() method which is a wrapper for
dns_timeouts() and returns a positive number or NULL/nil (in case of
-1).

- If 'timeouts()' returns positive number, it's assigned to
'next_timer' Ruby variable, and the Ruby set_timeouts() method is
again scheduled to run after 'next_timer' seconds (exactly when udns
library wants the control back).

- If 'timeouts()' returns NULL/nil (dns_timeouts returns -1) then then
timer is not scheduled again (will be scheduled again after next
dns_submit_xxx() is invoked).

- Also, at the beginning of the set_timeouts() Ruby method, the
current timer is canceled (to avoid duplicated calls).


I've tested it with high traffic, by using 2 (non-)responding
nameservers (NAMESERVERS="1.1.1.1 2.2.2.2"), with local working
"unbound", and so on. And IMHO it's working very well, I get all the
callbacks called (even when timeouts occur), retransmissions occur
after 4-8 seconds. :)

So IMHO my issue it solved, but perhaps I miss something in the above
mechanims. However I'll investigate what you say about setting a call
before poll/select (I strongly think this is not possible since
EventMachine IO-evented core library is written in C/C++, but the API
it provides is for Ruby language). Also note that my code depends on
EventMachine as it is, this is, I cannot modify EventMachine C/C++
core.


PS: I've read dns_set_tmcbck() documentation (NOTE: the manual says
"dns_set_cbck()" but the existing name is "dns_set_tmcbck()" as you
say). Honestly I don't understand how I could use such function,
neither how to integrate it with EventMachine reactor. Anyhow, would
it be better than my above approach?

Thanks a lot.


-- 
Iñaki Baz Castillo
<ibc at aliax.net>


More information about the udns mailing list