Feature #4788

resolv.rb refactoring

Added by Makoto Kishimoto almost 3 years ago. Updated about 1 year ago.

[ruby-dev:43587]
Status:Closed
Priority:Normal
Assignee:Akira Tanaka
Category:lib
Target version:next minor

Description

このようなモンキーパッチが(私のコードではありませんが)
https://github.com/ioquatix/rubydns/blob/master/lib/rubydns/resolv.rb
#3835 ( )の結果、動かなくなっていたのでパッチを検討していたわけですが、
結論としてresolv.rbに以下のようなリファクタリングを施すのがいいのではないかと考えました。
パッチを添付します。


diff --git a/lib/resolv.rb b/lib/resolv.rb
index 1e18893..e9c2432 100644
--- a/lib/resolv.rb
+++ b/lib/resolv.rb
@@ -491,6 +491,12 @@ class Resolv
# #getresource for argument details.

 def each_resource(name, typeclass, &proc)
  • eachresource(name, typeclass) {|reply, reply_name|
  • extractresources(reply, replyname, typeclass, &proc)
  • }
  • end +
  • def eachresource(name, typeclass) lazyinitialize requester = makeudp_requester senders = {} @@ -517,7 +523,7 @@ class Resolv # response will not fit in an untruncated UDP packet. redo else
  • extractresources(reply, replyname, typeclass, &proc)
  • yield(reply, reply_name) end return when RCode::NXDomain

resolv_rb_patch.txt Magnifier (823 Bytes) Makoto Kishimoto, 05/27/2011 05:01 PM

resolv_rb_patch.txt Magnifier - method name updated (823 Bytes) Makoto Kishimoto, 07/14/2011 03:46 PM

Associated revisions

Revision 40159
Added by Akira Tanaka about 1 year ago

  • lib/resolv.rb (Resolv::DNS.fetch_resource): New method to obtain full result. [Feature #4788] proposed by Makoto Kishimoto.

History

#1 Updated by Makoto Kishimoto almost 3 years ago

とりあえず思いつかなかったのでパッチでは eachresource という名前にしてしまいましたが、fetch_resource という名前でどうでしょうか。

#2 Updated by Hiroshi Nakamura almost 3 years ago

  • Assignee set to Akira Tanaka

#3 Updated by Samuel Williams almost 3 years ago

I have a vested interest in this patch since I am the developer of RubyDNS. This update will allow RubyDNS to hook into resolve.rb more efficiently. Right now, I have to duplicate code in resolve.rb to get the right behaviour.

Also, I originally suggested then name fetch_resource and thus I support Makoto Kishimoto's proposal about this change.

Lets work to get this updated so that I can add good support for RubyDNS and Ruby 1.9.

Thanks.

#4 Updated by Yui NARUSE almost 3 years ago

  • Target version changed from 1.9.3 to 2.0.0

#6 Updated by Akira Tanaka over 2 years ago

2011/5/27 Makoto Kishimoto redmine@ruby-lang.org:

このようなモンキーパッチが(私のコードではありませんが)
https://github.com/ioquatix/rubydns/blob/master/lib/rubydns/resolv.rb
#3835 ( )の結果、動かなくなっていたのでパッチを検討していたわけですが、
結論としてresolv.rbに以下のようなリファクタリングを施すのがいいのではないかと考えました。

なぜそうするのがいいのか書いていないのでなんとも言い難いものがあります。

モンキーパッチがうまくいかないから、という理由は受け入れ難いです。

後で調べるはめになったときのために、
変更するときには意図をどこかに残しておきたいと思うのですが、
上記はその記述として十分なものとは思えません。
--
[田中 哲][たなか あきら][Tanaka Akira]

#7 Updated by Makoto Kishimoto over 2 years ago

元の問題は、lib/resolv.rb が、TCP へのフォールバックに対応して、その際に
makerequester --> makeudp_requester という名前の変更があったために、
(公開インターフェースではない)それに依存していたコードが動かなくなった、
というものです。

しかし、makerequester のような内部の非常に低いレイヤにあるメソッドに
rubydns ライブラリのコードが依存していた理由は、DNS#each
resource
メソッド中に、個々のリソースを取得するコードが一体不可分に含まれてしまって
いるため、DNS プロキシのようなものを作る場合、each_resource メソッド中の
リソース取得部分のコードの duplicate が、このようなライブラリの実装において
不可避であったためです。

パッチで示したように、リソース取得部分を切り離し、また、一種のコールバックで
ある extract_resources を、現状のハードコーディング状態から抽象化する
リファクタリングによって、ライブラリ側でのコード duplicate が必要なくなります。

パッチで追加される fetch_resource メソッドのユースケースとして、
ライブラリ側のコードがどうなるかというパッチを示します。

--- resolv.rb.orig 2011-05-27 18:15:59.000000000 +0900
+++ resolv.rb 2011-08-03 10:38:43.000000000 +0900
@@ -24,6 +24,13 @@
# This allows such responses to be passed upstream with little or no
# modification/reinterpretation.
def query(name, typeclass)
+ if respondto?(:fetchresource) then
+ fetchresource(name, typeclass) do |reply, replyname|
+ return reply, replyname
+ end
+ return
+ end
+
lazy
initialize
requester = make_requester
senders = {}

#8 Updated by Samuel Williams over 2 years ago

It would be nice to get some movement on this - I'm getting bug reports from people trying to use 1.9.x and RubyDNS.

Kind regards,
Samuel

#9 Updated by Shyouhei Urabe about 2 years ago

  • Status changed from Open to Assigned

#10 Updated by Samuel Williams about 2 years ago

Hi, still having users with problems and no consistent way to solve it. Merging this patch would be a great addition. Let me know if you require any further support or have any questions.

#11 Updated by Samuel Williams about 2 years ago

Here is a translation into English for the most recent message from Makoto:

The original problem is that 'lib/resolv.rb' had a fallback to TCP which was broken. There was a patch (#3835) which renamed ‘makerequester' to 'makeudp_requester’. This is not a public interface so the code which depended on it stopped working.

However, the reason why RubyDNS depended on such a method is because the high level interface performs breaks the response up into individual records but we are actually interested in the response in its entirety. This is specifically a problem when creating a DNS proxy where you want to forward requests with minimal changes. Duplicating the code for each_resource was unavoidable in the implementation of RubyDNS.

As in the patch provided by Makoto, code duplication can be reduced by removing the direct connection between eachresource and fetchresource, and providing a block to be executed per successful response.

A second patch shows how RubyDNS can be simplified once the proposed change is applied.

#12 Updated by Samuel Williams almost 2 years ago

Hi, still waiting for some progress on this issue. Lots of people are bugging me about it.

#13 Updated by Samuel Williams almost 2 years ago

Bump

#14 Updated by Shyouhei Urabe almost 2 years ago

@ioquatix isn't the only one who's interested in it (read: me too).

#15 Updated by Akira Tanaka over 1 year ago

  • Target version changed from 2.0.0 to next minor

#16 Updated by Akira Tanaka about 1 year ago

  • Status changed from Assigned to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r40159.
Makoto, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


  • lib/resolv.rb (Resolv::DNS.fetch_resource): New method to obtain full result. [Feature #4788] proposed by Makoto Kishimoto.

Also available in: Atom PDF