-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Throttle simultaneous DNS requests #1924 #2111
Merged
+234
−16
Merged
Changes from 12 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
9460c95
Throttle simultaneous DNS requests
pfreixes 3d2a05d
Explicit loop for sleep calls
pfreixes 3800002
Fixed typos in documentation
pfreixes 2c27b41
Merge remote-tracking branch 'upstream/master' into throttle_dns_requ…
pfreixes 2dc2486
Added DNS request throttling in CHANGES.rst
pfreixes 4139d24
Dogpile invalid word by doc-spelling
pfreixes 89accd7
Get full coverage for locks
pfreixes ec597fe
Throttle DNS always enabled
pfreixes 4f35784
Merge branch 'master' into throttle_dns_requests
pfreixes 7e11ed4
Remove implicit loop tests for locks.Event
pfreixes 2ab0132
Use create_future helper
pfreixes 5586325
Updated spelling wordlist
pfreixes 11afb32
Use a simple Event wrapper instead of a full Event class implementation
pfreixes 8db6db1
Pass the loop explicitly
pfreixes a7704a5
Cancel pending throttled DNS requests if connector is close
pfreixes 517dfca
Removed not used attribute
pfreixes 1eebd9a
Pass loop explicit
pfreixes e885b98
Merge branch 'master' into throttle_dns_requests
pfreixes fa5b68d
Throtlle DNS feature adapted to CHANGES protocol
pfreixes 5b1500b
Update 2111.feature
asvetlov f138cde
Changed class name
pfreixes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import asyncio | ||
import collections | ||
|
||
from .helpers import create_future | ||
|
||
|
||
class Event: | ||
""" | ||
Adhoc Event class mainly copied from the official asyncio.locks.Event, but | ||
modifying the `set` method. It allows to pass an exception to wake | ||
the waiters with an exception. | ||
|
||
This is used when the event creator cant accommplish the requirements | ||
due to an exception, instead of try to built a sophisticated solution | ||
the same exeption is passed to the waiters. | ||
""" | ||
|
||
def __init__(self, *, loop=None): | ||
self._waiters = collections.deque() | ||
self._value = False | ||
if loop is not None: | ||
self._loop = loop | ||
else: | ||
self._loop = asyncio.get_event_loop() | ||
|
||
def __repr__(self): | ||
res = super().__repr__() | ||
extra = 'set' if self._value else 'unset' | ||
if self._waiters: | ||
extra = '{},waiters:{}'.format(extra, len(self._waiters)) | ||
return '<{} [{}]>'.format(res[1:-1], extra) | ||
|
||
def is_set(self): | ||
"""Return True if and only if the internal flag is true.""" | ||
return self._value | ||
|
||
def set(self, exc=None): | ||
"""Set the internal flag to true. All coroutines waiting for it to | ||
become true are awakened. Coroutine that call wait() once the flag is | ||
true will not block at all. | ||
|
||
If `exc` is different than None the `future.set_exception` is called | ||
""" | ||
if not self._value: | ||
self._value = True | ||
|
||
for fut in self._waiters: | ||
if not fut.done(): | ||
if not exc: | ||
fut.set_result(True) | ||
else: | ||
fut.set_exception(exc) | ||
|
||
def clear(self): | ||
"""Reset the internal flag to false. Subsequently, coroutines calling | ||
wait() will block until set() is called to set the internal flag | ||
to true again.""" | ||
self._value = False | ||
|
||
@asyncio.coroutine | ||
def wait(self): | ||
"""Block until the internal flag is true. | ||
|
||
If the internal flag is true on entry, return True | ||
immediately. Otherwise, block until another coroutine calls | ||
set() to set the flag to true, then return True. | ||
""" | ||
if self._value: | ||
return True | ||
|
||
fut = create_future(self._loop) | ||
self._waiters.append(fut) | ||
try: | ||
yield from fut | ||
return True | ||
finally: | ||
self._waiters.remove(fut) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
"""Tests of custom aiohttp locks implementations""" | ||
import asyncio | ||
|
||
from aiohttp import helpers | ||
from aiohttp.locks import Event | ||
|
||
|
||
class TestEvent: | ||
|
||
@asyncio.coroutine | ||
def test_set_exception(self, loop): | ||
ev = Event(loop=loop) | ||
|
||
@asyncio.coroutine | ||
def c(): | ||
try: | ||
yield from ev.wait() | ||
except Exception as e: | ||
return e | ||
return 1 | ||
|
||
t = helpers.ensure_future(c(), loop=loop) | ||
yield from asyncio.sleep(0, loop=loop) | ||
e = Exception() | ||
ev.set(exc=e) | ||
yield from asyncio.sleep(0, loop=loop) | ||
assert t.result() == e | ||
|
||
@asyncio.coroutine | ||
def test_set(self, loop): | ||
ev = Event(loop=loop) | ||
|
||
@asyncio.coroutine | ||
def c(): | ||
yield from ev.wait() | ||
return 1 | ||
|
||
t = helpers.ensure_future(c(), loop=loop) | ||
yield from asyncio.sleep(0, loop=loop) | ||
ev.set() | ||
yield from asyncio.sleep(0, loop=loop) | ||
assert t.result() == 1 | ||
|
||
# next lines help to get the 100% coverage. | ||
ev.set() | ||
ev.clear() | ||
t = helpers.ensure_future(c(), loop=loop) | ||
yield from asyncio.sleep(0, loop=loop) | ||
t.cancel() | ||
ev.set() | ||
|
||
@asyncio.coroutine | ||
def test_set_no_blocking(self, loop): | ||
ev = Event(loop=loop) | ||
ev.set() | ||
|
||
@asyncio.coroutine | ||
def c(): | ||
yield from ev.wait() | ||
return 1 | ||
|
||
t = helpers.ensure_future(c(), loop=loop) | ||
yield from asyncio.sleep(0, loop=loop) | ||
assert t.result() == 1 | ||
|
||
@asyncio.coroutine | ||
def test_repr(self, loop): | ||
ev = Event(loop=loop) | ||
assert "waiters" not in repr(ev) | ||
|
||
@asyncio.coroutine | ||
def c(): | ||
yield from ev.wait() | ||
|
||
helpers.ensure_future(c(), loop=loop) | ||
yield from asyncio.sleep(0, loop=loop) | ||
assert "waiters" in repr(ev) | ||
|
||
@asyncio.coroutine | ||
def test_is_set(self, loop): | ||
ev = Event(loop=loop) | ||
assert not ev.is_set() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't it possible to inherint from asyncio.Event and override set method instead? I'm not sure we want to maintain whole own Even lock.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No longer used that pattern