⚡ perf(webdav): 偏移写入分片并取消整文件落盘合并
This commit is contained in:
1 parent
6d825e5fbc
commit
32298bfdeb
12 files changed
+690
-53
No files matched your search
@@ -0,0 +1,77 @@
|
||||
"""Serialize each upload's writes/finalization/cleanup in single-worker deployments."""
|
||||
import asyncio
|
||||
from contextlib import asynccontextmanager
|
||||
from functools import wraps
|
||||
from weakref import WeakValueDictionary
|
||||
|
||||
_locks = WeakValueDictionary()
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def upload_lock(upload_id):
|
||||
lock = _locks.get(upload_id)
|
||||
if lock is None:
|
||||
lock = asyncio.Lock()
|
||||
_locks[upload_id] = lock
|
||||
async with lock:
|
||||
yield
|
||||
|
||||
|
||||
def serialize_upload(function):
|
||||
@wraps(function)
|
||||
async def wrapped(*args, **kwargs):
|
||||
upload_id = kwargs.get('upload_id') if 'upload_id' in kwargs else args[0]
|
||||
async with upload_lock(upload_id):
|
||||
return await function(*args, **kwargs)
|
||||
return wrapped
|
||||
|
||||
|
||||
_completion_tasks = {}
|
||||
_cancelling = set()
|
||||
|
||||
|
||||
def is_cancelling(upload_id):
|
||||
return upload_id in _cancelling
|
||||
|
||||
|
||||
def begin_cancel(upload_id):
|
||||
_cancelling.add(upload_id)
|
||||
|
||||
|
||||
def end_cancel(upload_id):
|
||||
_cancelling.discard(upload_id)
|
||||
|
||||
|
||||
def completion_task(upload_id):
|
||||
return _completion_tasks.get(upload_id)
|
||||
|
||||
|
||||
def start_completion(upload_id, coroutine):
|
||||
task = asyncio.create_task(coroutine, name=f'upload-completion-{upload_id}')
|
||||
_completion_tasks[upload_id] = task
|
||||
def completed(done):
|
||||
if not done.cancelled():
|
||||
done.exception() # Observe failures even if the browser disconnects.
|
||||
def expire():
|
||||
if _completion_tasks.get(upload_id) is done:
|
||||
_completion_tasks.pop(upload_id, None)
|
||||
asyncio.get_running_loop().call_later(300, expire)
|
||||
task.add_done_callback(completed)
|
||||
return task
|
||||
|
||||
|
||||
def take_completion(upload_id):
|
||||
return _completion_tasks.pop(upload_id)
|
||||
|
||||
|
||||
async def cancel_completion(upload_id):
|
||||
task = _completion_tasks.pop(upload_id, None)
|
||||
if task is not None:
|
||||
if not task.done():
|
||||
task.cancel()
|
||||
await asyncio.gather(task, return_exceptions=True)
|
||||
|
||||
|
||||
async def shutdown_completions():
|
||||
for upload_id in list(_completion_tasks):
|
||||
await cancel_completion(upload_id)
|
||||
Reference in new issue
Block a user