dnspython icon indicating copy to clipboard operation
dnspython copied to clipboard

Refactoring socket creation code to facilitate connection reuse

Open tykling opened this issue 11 months ago • 2 comments

I am working on connection reuse in dns_exporter. I want to open a socket to, say, a DoT server and use it for many lookups without having to do the whole TCP+TLS handshake for every query. dnspython supports this by providing a socket to for example dns.query.tls() in the sock argument. To create that socket currently I have to import and copy a bunch of the socket creation logic from dnspython to dns_exporter.

I would help a lot if the socket creation code in the query functions could be refactored into seperate functions, maybe dns.query.tls() could call dns.query.get_tls_socket() when the sock argument is not provided, but then dns.query.get_tls_socket() could also be called by the implementer for connection reuse purposes. This would make it trivial to use dnspython with, say, a DoT socket doing many lookups but only getting the tcp+tls handshake penalty once.

I am happy to help implement this, but I wanted to gauge your interest before writing too much code. I have a local branch with a working example for DoT, and it isn't that big of a diff, plus it almost makes the code more clean to have the socket creation stuff in a seperate function. Maybe more testable too. Let me know what you think, and if you agree this would be good to have in dnspython then please let me know how you wish to proceed regarding implementation details, who does what, etc.

Context (please complete the following information):

  • dnspython 2.7.0
  • Python 3.12
  • OS: debian

tykling avatar Jan 17 '25 08:01 tykling

I'm ok with exposing a helper function to make a TLS socket. We'd need to do it for both query and asyncquery. I'm happy to do the work.

rthalley avatar Jan 18 '25 16:01 rthalley

OK great 🥇 looking forward to it!

It occurred to me that connection reuse could also be something entirely handled by dnspython, instead of by consumers of dnspython. I would be totally fine with doing it externally, I just wanted to mention it.

tykling avatar Jan 27 '25 12:01 tykling

merged.

rthalley avatar Aug 18 '25 18:08 rthalley