Nicholas Yost
Nicholas Yost

Reputation: 266

Limit Page to one Simultanious Request

Using PHP or Apache (I'd also consider moving to Nginx if necessary), how can I make a page only be ran once at a time? If three requests come in at the same time, one would complete entirely, then the next, and then the next. At no time should the page be accessed by more than one request at a time.

Think of it like transactions! How can I achieve this? It should be one page per server, no user or IP.

Upvotes: 0

Views: 491

Answers (2)

Michael Tabolsky
Michael Tabolsky

Reputation: 3597

What do you want to do with the "busy" state on server? return an error right away or keep requests in waiting until the previous finishes?

If you just want the server to refuse content to the client, you can do it on both nginx and apache:

The "tricky" part in your request is not to limit by an IP as people usually want, but globally per URI. I know it should be possible with mod_security bud I didn't do that myself but I have this configuration working for nginx:

http {
    #create a zone by $package id (my internal variable, similar to a vhost)
    limit_req_zone $packageid zone=heavy-stuff:10m rate=1r/s;

}

then later:

server {
  set $packageid some-id;
  location = /some-heavy-stuff {
                limit_req zone=heavy-stuff burst=1 nodelay;
  }
}

what it does for me is creating N limit zones, one for each of my servers. The zone is then used to count requests and allow only 1 per second.

Hope it helps

Upvotes: 1

Sundar
Sundar

Reputation: 4650

If the same user gives the request from the same page then Use session_start() this will block the other requests until finish the first request

Example:

http://codingexplained.com/coding/php/solving-concurrent-request-blocking-in-php

If you want to block the request from different browser/client keep the entries in database and process it one by one.

Upvotes: 0

Related Questions