-
Notifications
You must be signed in to change notification settings - Fork 264
Bump to v7.16.0 #457
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
Merged
Merged
Bump to v7.16.0 #457
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f3a0b9b
DP-5290 python-sdk/cdn: 查询域名带宽,支持type参数
yuansl 1ca9520
fix: improve file cache of regions in concurrent scenarios
lihsai0 642c713
feat: add single flight and apply to QueryRegionsProvider
lihsai0 ae57730
test: fix test cases
lihsai0 cd942fc
fix: python2 compatible and flake8 style
lihsai0 b8e29a7
docs: update version and changelogs
lihsai0 e33c0e1
styles: fix flake8 styles
lihsai0 b6fd793
fix: file lock on windows
lihsai0 166be8f
fix: windows locker
lihsai0 dcc0da4
feat: no raise error if regions cache shrink failed
lihsai0 132c9d2
fix: error messages format
eirture 322adec
fix: permissions of the region provider cache file
eirture 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
feat: add single flight and apply to QueryRegionsProvider
- Loading branch information
commit 642c71397a29ed7ccf107ee013ba6e0420628c0a
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,50 @@ | ||
import threading | ||
|
||
|
||
class _FlightLock: | ||
""" | ||
Do not use dataclass which caused the event created only once | ||
""" | ||
def __init__(self): | ||
self.event = threading.Event() | ||
self.result = None | ||
self.error = None | ||
|
||
|
||
class SingleFlight: | ||
def __init__(self): | ||
self._locks = {} | ||
self._lock = threading.Lock() | ||
|
||
def do(self, key, fn, *args, **kwargs): | ||
# here does not use `with` statement | ||
# because need to wait by another object if it exists, | ||
# and reduce the `acquire` times if it not exists | ||
self._lock.acquire() | ||
if key in self._locks: | ||
flight_lock = self._locks[key] | ||
|
||
self._lock.release() | ||
flight_lock.event.wait() | ||
|
||
if flight_lock.error: | ||
raise flight_lock.error | ||
return flight_lock.result | ||
|
||
flight_lock = _FlightLock() | ||
self._locks[key] = flight_lock | ||
self._lock.release() | ||
|
||
try: | ||
flight_lock.result = fn(*args, **kwargs) | ||
except Exception as e: | ||
flight_lock.error = e | ||
finally: | ||
flight_lock.event.set() | ||
|
||
with self._lock: | ||
del self._locks[key] | ||
|
||
if flight_lock.error: | ||
raise flight_lock.error | ||
return flight_lock.result |
This file contains hidden or 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,59 @@ | ||
import pytest | ||
import time | ||
from multiprocessing.pool import ThreadPool | ||
|
||
from qiniu.http.single_flight import SingleFlight | ||
|
||
class TestSingleFlight: | ||
def test_single_flight_success(self): | ||
sf = SingleFlight() | ||
|
||
def fn(): | ||
return "result" | ||
|
||
result = sf.do("key1", fn) | ||
assert result == "result" | ||
|
||
def test_single_flight_exception(self): | ||
sf = SingleFlight() | ||
|
||
def fn(): | ||
raise ValueError("error") | ||
|
||
with pytest.raises(ValueError, match="error"): | ||
sf.do("key2", fn) | ||
|
||
def test_single_flight_concurrent(self): | ||
sf = SingleFlight() | ||
share_state = [] | ||
results = [] | ||
|
||
def fn(): | ||
time.sleep(1) | ||
share_state.append('share_state') | ||
return "result" | ||
|
||
def worker(_n): | ||
result = sf.do("key3", fn) | ||
results.append(result) | ||
|
||
ThreadPool(2).map(worker, range(5)) | ||
|
||
assert len(share_state) == 3 | ||
assert all(result == "result" for result in results) | ||
|
||
def test_single_flight_different_keys(self): | ||
sf = SingleFlight() | ||
results = [] | ||
|
||
def fn(): | ||
time.sleep(1) | ||
return "result" | ||
|
||
def worker(n): | ||
result = sf.do("key{}".format(n), fn) | ||
eirture marked this conversation as resolved.
Show resolved
Hide resolved
|
||
results.append(result) | ||
|
||
ThreadPool(2).map(worker, range(2)) | ||
assert len(results) == 2 | ||
assert all(result == "result" for result in results) |
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.
Uh oh!
There was an error while loading. Please reload this page.