Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring socket creation code to facilitate connection reuse #1176

Open
tykling opened this issue Jan 17, 2025 · 2 comments
Open

Refactoring socket creation code to facilitate connection reuse #1176

tykling opened this issue Jan 17, 2025 · 2 comments

Comments

@tykling
Copy link
Contributor

tykling commented Jan 17, 2025

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 tykling changed the title Closing sockets and connection reuse Refactoring socket creation code to facilitate connection reuse Jan 17, 2025
@rthalley
Copy link
Owner

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.

@tykling
Copy link
Contributor Author

tykling commented Jan 27, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants