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

Fix timeout #110

Merged
merged 2 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions aiodns/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ def __init__(self, nameservers: Optional[Sequence[str]] = None,
raise RuntimeError(
'aiodns needs a SelectorEventLoop on Windows. See more: https://github.com/saghul/aiodns/issues/86')
kwargs.pop('sock_state_cb', None)
self._channel = pycares.Channel(sock_state_cb=self._sock_state_cb, **kwargs)
timeout = kwargs.pop('timeout', None)
self._timeout = timeout
self._channel = pycares.Channel(sock_state_cb=self._sock_state_cb,
timeout=timeout,
**kwargs)
if nameservers:
self.nameservers = nameservers
self._read_fds = set() # type: Set[int]
Expand Down Expand Up @@ -119,7 +123,7 @@ def _sock_state_cb(self, fd: int, readable: bool, writable: bool) -> None:
self.loop.add_writer(fd, self._handle_event, fd, WRITE)
self._write_fds.add(fd)
if self._timer is None:
self._timer = self.loop.call_later(1.0, self._timer_cb)
self._start_timer()
else:
# socket is now closed
if fd in self._read_fds:
Expand All @@ -146,6 +150,15 @@ def _handle_event(self, fd: int, event: Any) -> None:
def _timer_cb(self) -> None:
if self._read_fds or self._write_fds:
self._channel.process_fd(pycares.ARES_SOCKET_BAD, pycares.ARES_SOCKET_BAD)
self._timer = self.loop.call_later(1.0, self._timer_cb)
self._start_timer()
else:
self._timer = None

def _start_timer(self):
timeout = self._timeout
if timeout is None or timeout < 0 or timeout > 1:
timeout = 1
elif timeout == 0:
timeout = 0.1

self._timer = self.loop.call_later(timeout, self._timer_cb)
6 changes: 5 additions & 1 deletion tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import unittest
import socket
import sys
import time

import aiodns

Expand Down Expand Up @@ -98,13 +99,16 @@ def test_query_bad_class(self):
self.assertRaises(ValueError, self.resolver.query, 'google.com', 'A', "INVALIDCLASS")

def test_query_timeout(self):
self.resolver = aiodns.DNSResolver(timeout=0.1, loop=self.loop)
self.resolver = aiodns.DNSResolver(timeout=0.1, tries=1, loop=self.loop)
self.resolver.nameservers = ['1.2.3.4']
f = self.resolver.query('google.com', 'A')
started = time.monotonic()
try:
self.loop.run_until_complete(f)
except aiodns.error.DNSError as e:
self.assertEqual(e.args[0], aiodns.error.ARES_ETIMEOUT)
# Ensure timeout really cuts time deadline. Limit duration to one second
self.assertLess(time.monotonic() - started, 1)

def test_query_cancel(self):
f = self.resolver.query('google.com', 'A')
Expand Down
Loading