[Twisted-Python] twisted.names and multicast DNS

Jp Calderone exarkun at divmod.com
Sun Jul 10 14:16:34 EDT 2005


On Sat, 9 Jul 2005 17:08:15 +1000, Tim Allen <screwtape at froup.com> wrote:
>So after two years of leaving it alone, I decided to have another look at 
>the multicast DNS module I was writing for Twisted, and lo and behold I 
>discover a serious design flaw in my response cache. Here's the basic 
>situation I'm dealing with:
>
> [snip]
>
>The problem is this: the Record_PTR class (just like all the other Record_* 
>classes) in t.names.dns stores the TTL as an instance variable! Therefore, 
>when I have an existing record and an updated record, they never match, and 
>I wind up with two identical records in my data structure that differ only 
>by TTL.
>
>As far as I can tell, I have three options from here:
>
>1. Write my own versions of all the Record_* classes that don't maintain a 
>TTL value, and translate backwards and forwards whenever I need to talk to 
>Twisted's DNS layer. I'd need to update this code whenever a new DNS record 
>type was added.
>2. Write a comparison function for comparing resource records that compares 
>instance variables that aren't named 'ttl'. This would probably need to be 
>updated less frequently, but is horribly hacky.

What's hacky about this?  It seems like a simple, straightforward solution.  Record classes have comparison defined one way, you want to compare them another way.  Comparison can only be defined one way using the "==" operator, so clearly one of these requirements has to resort to using something other than the "==" operator.

I think it's even nicer than you suggest, since the attributes which should be compared are all declared on each class.  eg, Record_NS.compareAttributes == ('name', 'ttl').  You should be able to write a comparison function that looks at this attribute and simply omits ttl.

Hope this helps,

Jp




More information about the Twisted-Python mailing list