diff options
author | anandv96 <60289989+anandv96@users.noreply.github.com> | 2020-08-20 12:57:43 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-20 08:27:43 +0100 |
commit | f5edfb9659f97b564b490cbbc1118b380d6244cd (patch) | |
tree | 55aa802537566ea4d261d9dbd9ef4de5a7ce3932 | |
parent | b24747b305a0770fdd746655e702aa1c1c049765 (diff) |
#903: Client API: mutex on (user_id, room_id) (#1286)
* Client API: mutex on (user_id, room_id)
* Client API: mutex on (user_id, room_id)
Changed variable name used for the mutexes map
Changed the place where the mutex is locked
Changed unlock to a defered call instead of manually calling it
-rw-r--r-- | clientapi/routing/sendevent.go | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/clientapi/routing/sendevent.go b/clientapi/routing/sendevent.go index e0cd7eb5..9cf517cf 100644 --- a/clientapi/routing/sendevent.go +++ b/clientapi/routing/sendevent.go @@ -16,6 +16,7 @@ package routing import ( "net/http" + "sync" "github.com/matrix-org/dendrite/clientapi/httputil" "github.com/matrix-org/dendrite/clientapi/jsonerror" @@ -35,6 +36,10 @@ type sendEventResponse struct { EventID string `json:"event_id"` } +var ( + userRoomSendMutexes sync.Map // (roomID+userID) -> mutex. mutexes to ensure correct ordering of sendEvents +) + // SendEvent implements: // /rooms/{roomID}/send/{eventType} // /rooms/{roomID}/send/{eventType}/{txnID} @@ -63,6 +68,13 @@ func SendEvent( } } + // create a mutex for the specific user in the specific room + // this avoids a situation where events that are received in quick succession are sent to the roomserver in a jumbled order + userID := device.UserID + mutex, _ := userRoomSendMutexes.LoadOrStore(roomID+userID, &sync.Mutex{}) + mutex.(*sync.Mutex).Lock() + defer mutex.(*sync.Mutex).Unlock() + e, resErr := generateSendEvent(req, device, roomID, eventType, stateKey, cfg, rsAPI) if resErr != nil { return *resErr |