Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Monitoring User Sessions to Prevent Editing Conflict

I'm working on something similar to a pastebin (yeah, it's that generic) but allowing for multiple user editing. The obvious problem is that of multiple users attempting to edit the same file. I'm thinking along the lines of locking down the file when one user is working on it (it's not the best solution, but I don't need anything too complex), but to prevent/warn the user I'd obviously need a system for monitoring each user's edit sessions. Working with database and ajax, I'm thinking of two solutions.

The first would be to have the edit page ping the server at a arbitrary interval, say a minute, and it would update the edit session entry in the db. Then the next time a script request to edit, it checks for the most recent ping, and if the most recent was another arbitrary time ago, say five minute, then we assume that the previous user had quited and the file can be edited again. Of course, the problem with this method is that the assumption that the previous user had quited is simply an assumption. He could be having flaky wi-fi connection and simply dropped out for ten minutes, all the time with the window still open.

Of course, to deal with this problem, we'd have to have the server respond to new request from previously closed sessions with an error, telling the client side to point out to the user that his session has ended, and then deal with it by, say, saving it as another file on the server and asking the user to manually merge it, etc. It goes without saying that this is rather horrible for the end user.

So I've came around to think of another solution. It may also be possible to get a unload event to fire when the user's session ends, but I cannot be sure whether this will work reliably.

Does anybody has any other, more elegant solution to this problem?

like image 269
Yi Jiang Avatar asked Aug 20 '10 10:08

Yi Jiang


2 Answers

If you expect the number of concurrent edits to the file to be minor, you could just store a version number for the file in the db, and when the user downloads the file into their browser they also get the version number. They are only allowed to upload their changes if the version number matches. First one to upload wins. When a conflict is detected you should send back the latest file and the user's changes so that the user can manually merge in the changes. The advantage is that this works even if it's the same user making two simultaneous edits. If this feature ends up being frequently used you could add client-side merging similar to what a diff tool uses (but you might need to keep the old revisions in that case).

like image 163
Mr. Shiny and New 安宇 Avatar answered Sep 24 '22 16:09

Mr. Shiny and New 安宇


You're probably better off going for a "merge" solution. Using this approach you only need to check for changes when the user posts their document to the server.

The basic approach would be: 1. User A gets the document for editing, document is at version 1 2. User B gets the document for editing, document is at version 1 3. User B posts some changes, including the base version number of 1 4. Server updates document, document now at version 2 5. User B posts some changes, including the base version number of 1 6. Server responds saying document has changed since the user starts editing, and sends user the new document, and their version - user will then need to perform any merging of their changes into document version 2, and post back to the server. User is essentially now editing document version 2 7. User A posts some changes, including the version number of 2 8. Server updates the document, which is now at version 3

You can still do a "ping" every minute, to get the current version number - you already know what version they're editing, so if a new version is available you can let them know and let them download the latest version to make their changes into.

The main benefit of this approach is that users never lock files, so you don't need any arbitrary "time-outs".

like image 42
David_001 Avatar answered Sep 21 '22 16:09

David_001