From 1ae924558f0ba397efb4e4b2c01b4fb80e901aa7 Mon Sep 17 00:00:00 2001 From: fr33domlover Date: Tue, 18 Jun 2019 10:47:01 +0000 Subject: [PATCH] Use updateGet for atomic access to projectNextTicket `updateGet` isn't atomic by default. In PostgreSQL the default isolation level if committed read, and an `update` followed by a `get` doesn't guarantee you get the same value you sent. However I'm making a patch for `persistent` to make `updateGet` atomic for PostgreSQL. --- src/Vervis/Federation/Ticket.hs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/Vervis/Federation/Ticket.hs b/src/Vervis/Federation/Ticket.hs index 3fb9711..24ec549 100644 --- a/src/Vervis/Federation/Ticket.hs +++ b/src/Vervis/Federation/Ticket.hs @@ -179,10 +179,9 @@ projectOfferTicketF findRelevantCollections hLocal $ activityAudience $ actbActivity body mremotesHttp <- runDBExcept $ do - (sid, jid, ibid, fsid, next, tids) <- - getProjectAndDeps deps + (sid, jid, ibid, fsid, tids) <- getProjectAndDeps deps lift $ join <$> do - mractid <- insertTicket jid ibid next tids + mractid <- insertTicket jid ibid tids for mractid $ \ ractid -> for msig $ \ sig -> do remoteRecips <- deliverLocal ractid colls sid fsid (sig,) <$> deliverRemoteDB (actbBL body) ractid jid sig remoteRecips @@ -234,11 +233,8 @@ projectOfferTicketF tids <- for deps $ \ dep -> do mtid <- lift $ getKeyBy $ UniqueTicket jid dep fromMaybeE mtid "Local dep: No such ticket number in DB" - return - ( sid, jid, projectInbox j, projectFollowers j, projectNextTicket j - , tids - ) - insertTicket jid ibid next deps = do + return (sid, jid, projectInbox j, projectFollowers j, tids) + insertTicket jid ibid deps = do let iidAuthor = remoteAuthorInstance author raidAuthor = remoteAuthorId author ractid <- either entityKey id <$> insertBy' RemoteActivity @@ -254,7 +250,9 @@ projectOfferTicketF delete ibiid return Nothing Just _ibirid -> do - update jid [ProjectNextTicket +=. 1] + next <- + ((subtract 1) . projectNextTicket) <$> + updateGet jid [ProjectNextTicket +=. 1] did <- insert Discussion fsid <- insert FollowerSet tid <- insert Ticket