Skip to content

Commit

Permalink
[DOP-20958] Take a lock before updating addresses list
Browse files Browse the repository at this point in the history
  • Loading branch information
dolfinus committed Nov 2, 2024
1 parent 3588222 commit 65108ea
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions data_rentgen/db/repositories/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,22 @@ async def _create(self, location: LocationDTO) -> Location:
async def _update_addresses(self, existing: Location, new: LocationDTO) -> Location:
existing_urls = {address.url for address in existing.addresses}
new_urls = new.addresses - existing_urls
if new_urls:
addresses = [Address(url=url, location_id=existing.id) for url in new_urls]
existing.addresses.extend(addresses)
await self._session.flush([existing])
if not new_urls:
return existing

# take a lock, to avoid race conditions, and then
# get fresh state of the object, because it already could be updated by another worker
await self._lock(existing.type, existing.name)
await self._session.refresh(existing, ["addresses"])

# already has all required addresses - nothing to update
existing_urls = {address.url for address in existing.addresses}
new_urls = new.addresses - existing_urls
if not new_urls:
return existing

# add new addresses while holding the lock
addresses = [Address(url=url, location_id=existing.id) for url in new_urls]
existing.addresses.extend(addresses)
await self._session.flush([existing])
return existing

0 comments on commit 65108ea

Please sign in to comment.