@@ -55,7 +55,11 @@ def __init__(self, nameservers: Optional[Sequence[str]] = None,
55
55
raise RuntimeError (
56
56
'aiodns needs a SelectorEventLoop on Windows. See more: https://github.com/saghul/aiodns/issues/86' )
57
57
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 )
59
63
if nameservers :
60
64
self .nameservers = nameservers
61
65
self ._read_fds = set () # type: Set[int]
@@ -119,7 +123,7 @@ def _sock_state_cb(self, fd: int, readable: bool, writable: bool) -> None:
119
123
self .loop .add_writer (fd , self ._handle_event , fd , WRITE )
120
124
self ._write_fds .add (fd )
121
125
if self ._timer is None :
122
- self ._timer = self . loop . call_later ( 1.0 , self . _timer_cb )
126
+ self ._start_timer ( )
123
127
else :
124
128
# socket is now closed
125
129
if fd in self ._read_fds :
@@ -146,6 +150,15 @@ def _handle_event(self, fd: int, event: Any) -> None:
146
150
def _timer_cb (self ) -> None :
147
151
if self ._read_fds or self ._write_fds :
148
152
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 ( )
150
154
else :
151
155
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