embed_links: Take a lock on the message object while editing.

We leave the fetching of links outside of the lock, as they could take
seconds, which is an unreasonable amount of time to hold a lock on the
message row.  This may result in unnecessary work, in the case that
the message was since edited, but the unnecessary work is preferable
to blocking other work on the message row for the duration.
This commit is contained in:
Alex Vandiver 2022-04-18 14:51:16 -07:00 committed by Tim Abbott
parent 62642b899c
commit de63000db6
1 changed files with 10 additions and 6 deletions

View File

@ -869,12 +869,16 @@ class FetchLinksEmbedData(QueueProcessingWorker):
"Time spent on get_link_embed_data for %s: %s", url, time.time() - start_time "Time spent on get_link_embed_data for %s: %s", url, time.time() - start_time
) )
message = Message.objects.get(id=event["message_id"]) with transaction.atomic():
# If the message changed, we will run this task after updating the message message = Message.objects.select_for_update().get(id=event["message_id"])
# in zerver.actions.message_edit.check_update_message # If the message changed, we will run this task after updating the message
if message.content != event["message_content"]: # in zerver.actions.message_edit.check_update_message
return if message.content != event["message_content"]:
if message.content is not None: return
if message.content is None:
return
query = UserMessage.objects.filter( query = UserMessage.objects.filter(
message=message.id, message=message.id,
) )