From 972fd6679d6c85a2742aa5a4ea8adc989497f260 Mon Sep 17 00:00:00 2001 From: Sumner Evans Date: Mon, 4 Sep 2023 18:34:02 -0600 Subject: [PATCH] media/create: don't store unused expiration time in database Signed-off-by: Sumner Evans --- synapse/media/media_repository.py | 13 +++++------ .../databases/main/media_repository.py | 7 +++--- .../80/02_add_unused_expires_at_for_media.sql | 22 ------------------- 3 files changed, 9 insertions(+), 33 deletions(-) delete mode 100644 synapse/storage/schema/main/delta/80/02_add_unused_expires_at_for_media.sql diff --git a/synapse/media/media_repository.py b/synapse/media/media_repository.py index 6e87ca9d4c6b..e4eb8d3ae70b 100644 --- a/synapse/media/media_repository.py +++ b/synapse/media/media_repository.py @@ -201,14 +201,12 @@ async def create_media_id(self, auth_user: UserID) -> Tuple[str, int]: """ media_id = random_string(24) now = self.clock.time_msec() - unused_expires_at = now + self.unused_expiration_time await self.store.store_local_media_id( media_id=media_id, time_now_ms=now, user_id=auth_user, - unused_expires_at=unused_expires_at, ) - return f"mxc://{self.server_name}/{media_id}", unused_expires_at + return f"mxc://{self.server_name}/{media_id}", now + self.unused_expiration_time @trace async def reached_pending_media_limit( @@ -262,7 +260,8 @@ async def verify_can_upload(self, media_id: str, auth_user: UserID) -> None: errcode=Codes.CANNOT_OVERWRITE_MEDIA, ) - if media.get("unused_expires_at", 0) < self.clock.time_msec(): + now = self.clock.time_msec() + if media.get("created_ts", now) < now - self.unused_expiration_time: raise NotFoundError("Media ID has expired") @trace @@ -390,12 +389,12 @@ async def get_local_media_info( return media_info # Check if the media ID has expired and still hasn't been uploaded to. - unused_expires_at = media_info.get("unused_expires_at", 0) - if 0 < unused_expires_at < self.clock.time_msec(): + now = self.clock.time_msec() + if media_info.get("created_ts", now) + self.unused_expiration_time < now: respond_404(request) return None - if self.clock.time_msec() >= wait_until: + if now >= wait_until: break await self.clock.sleep(0.5) diff --git a/synapse/storage/databases/main/media_repository.py b/synapse/storage/databases/main/media_repository.py index 71d87149131f..ad2056d6b29d 100644 --- a/synapse/storage/databases/main/media_repository.py +++ b/synapse/storage/databases/main/media_repository.py @@ -117,6 +117,8 @@ def __init__( self._drop_media_index_without_method, ) + self.unused_expiration_time = hs.config.media.unused_expiration_time + async def _drop_media_index_without_method( self, progress: JsonDict, batch_size: int ) -> int: @@ -170,7 +172,6 @@ async def get_local_media(self, media_id: str) -> Optional[Dict[str, Any]]: "url_cache", "safe_from_quarantine", "user_id", - "unused_expires_at", ), allow_none=True, desc="get_local_media", @@ -339,7 +340,6 @@ async def store_local_media_id( media_id: str, time_now_ms: int, user_id: UserID, - unused_expires_at: int, ) -> None: await self.db_pool.simple_insert( "local_media_repository", @@ -347,7 +347,6 @@ async def store_local_media_id( "media_id": media_id, "created_ts": time_now_ms, "user_id": user_id.to_string(), - "unused_expires_at": unused_expires_at, }, desc="store_local_media_id", ) @@ -436,7 +435,7 @@ def get_pending_media_txn(txn: LoggingTransaction) -> Tuple[int, int]: row = txn.fetchone() if not row: raise StoreError(404, "Failed to count pending media for user") - return row[0], row[1] or 0 + return row[0], (row[1] + self.unused_expiration_time if row[1] else 0) return await self.db_pool.runInteraction("get_url_cache", get_pending_media_txn) diff --git a/synapse/storage/schema/main/delta/80/02_add_unused_expires_at_for_media.sql b/synapse/storage/schema/main/delta/80/02_add_unused_expires_at_for_media.sql deleted file mode 100644 index ed464820cece..000000000000 --- a/synapse/storage/schema/main/delta/80/02_add_unused_expires_at_for_media.sql +++ /dev/null @@ -1,22 +0,0 @@ -/* Copyright 2023 Beeper Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - --- Add new colums to the `local_media_repository` to keep track of when the --- media ID must be used by. This is to support async uploads (see MSC2246). - -ALTER TABLE local_media_repository - ADD COLUMN unused_expires_at BIGINT DEFAULT NULL; - -CREATE INDEX CONCURRENTLY ON local_media_repository (unused_expires_at);