@@ -55,7 +55,11 @@ def __init__(self, nameservers: Optional[Sequence[str]] = None,
5555 raise RuntimeError (
5656 'aiodns needs a SelectorEventLoop on Windows. See more: https://github.com/saghul/aiodns/issues/86' )
5757 kwargs .pop ('sock_state_cb' , None )
58- self ._channel = pycares .Channel (sock_state_cb = self ._sock_state_cb , ** kwargs )
58+ timeout = kwargs .pop ('timeout' , None )
59+ self ._timeout = timeout
60+ self ._channel = pycares .Channel (sock_state_cb = self ._sock_state_cb ,
61+ timeout = timeout ,
62+ ** kwargs )
5963 if nameservers :
6064 self .nameservers = nameservers
6165 self ._read_fds = set () # type: Set[int]
@@ -119,7 +123,7 @@ def _sock_state_cb(self, fd: int, readable: bool, writable: bool) -> None:
119123 self .loop .add_writer (fd , self ._handle_event , fd , WRITE )
120124 self ._write_fds .add (fd )
121125 if self ._timer is None :
122- self ._timer = self . loop . call_later ( 1.0 , self . _timer_cb )
126+ self ._start_timer ( )
123127 else :
124128 # socket is now closed
125129 if fd in self ._read_fds :
@@ -146,6 +150,15 @@ def _handle_event(self, fd: int, event: Any) -> None:
146150 def _timer_cb (self ) -> None :
147151 if self ._read_fds or self ._write_fds :
148152 self ._channel .process_fd (pycares .ARES_SOCKET_BAD , pycares .ARES_SOCKET_BAD )
149- self ._timer = self . loop . call_later ( 1.0 , self . _timer_cb )
153+ self ._start_timer ( )
150154 else :
151155 self ._timer = None
156+
157+ def _start_timer (self ):
158+ timeout = self ._timeout
159+ if timeout is None or timeout < 0 or timeout > 1 :
160+ timeout = 1
161+ elif timeout == 0 :
162+ timeout = 0.1
163+
164+ self ._timer = self .loop .call_later (timeout , self ._timer_cb )
0 commit comments