简体   繁体   中英

Check availability of resource used by another user

Building a web application.

User have access trough their browser to shared resources host on a server, however if UserA is already using Resource1, Resource1 should not be available to UserB until UserA release Resource1 or until a given amount of time.

For this part : I chose to use a MySQL table with a list of tuples (resource,currentuser) and run a cron task to delete expired tuples.

Now I want to be able to notify UserA that UserB wants to access Resource1 and if get not answer from UserA, then UserA lost his lock on Resource1 and then the Resource is then available to UserB.

For this part, I guess I have to use AJAX. I have thought about the following solution :

User's browser make periodic AJAX call (let's say each minute) to prove he is still alive and upon a call, if another User has requested the same resource, he has to challenge a server request in a given amount of time(for example a captcha). If the challenge fails, it means the user is not here anymore (maybe he left his browser opened or the webpage unfocused).

The tricky part is : "he has to challenge a server request in a given amount of time (for example a captcha)". How to do that?

Am I following the best path ?

Yes, what you've outlined is fine. Using ajax is also completely fine, especially if you're simply polling every minute.

For example, let's say you have the following:

setInterval(function() {
 $.get('/resource/status', function(response) {
    if (response.data.newRequest) {
       //This would signal a new request to the resource
    }
 })
}, 60000)

When handling the new request to access the resource, you could use something like reCaptcha and display that however appropriate (overlay or inline). When you do this, you could also start a timer to determine if it's exceeded the amount of time allocated or not. If it has, then you can do another ajax request and revoke this person's access to the resource, or however you want to handle that.

i would use web sockets to control all the users that need to get the resource. this way you will know who is connected and using the resource and when he finish using it you can let the next user the resource and so on , (this way can tell each user an estimation of how much time it will take him to get the resource and do some progress bar)

I think there're two problems here.

  1. How to notify users that resource becomes available? Periodic AJAX requests might be okay, but you can also consider long-polling or websockets to get close to notifying waiting users in real time.

  2. How to find out that resource is still used by user? If you want to catch the moment when human user is not doing anything on page, you can track mouse movement/clicking or keyboard button pressing. If nothing is done for last n minutes, the page might be considered as not active. If you want to make sure that page is not exploited by automated software, you can ask to fill in captcha once in n minutes when resource is being used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM