开发者

Handling HTTP request

If there is a HTTP request coming to a web server from many clients the requests will be handled in the order.

For all the http request i want to use a token bucket system. So when there is a first Request i write a number to a file and increment the number for the next request and so on..

I dont want to do it in DB since the DB size开发者_StackOverflow社区 increases..

Is this the right way to do this.Please suggest

Edit:So if a user posts a comment the comment should be stored in the a file instead of the DB.So to keep track of it there is a variable that is incremented for every request.this number will be used in writing the file name and refer it for future reference.so if there are many requests is this the right way to do it..

Thanks..


Why not lock ( http://php.net/manual/en/function.flock.php ) files in a folder ?

First call locks 01,
Second call locks 02,
3rd call locks 03,
01 gets unlocked,
4th call locks 01

Basically each php script tries to lock the first file it can and when it's done it unlocks/erases the file.

I use this in a system with 250+ child processes spawned by a "process manager". Tried to use a database but it slowed down everything.

If you want to keep incrementing the file number for some content i would suggest using mktime() or time() and using


$now=time();
$suffix=0;
while(is_file($dir.$now.'_'.$suffix)) {
  $suffix++;
} 

But again, depending on how you want to read the data or use it, there are many options. Could you provide more details?

-----EDIT 1-----

  1. Each request has a "lock-file", and stores the lock id (number) is in $lock.
  2. three visitors post at the same time with the lock-id 01, 02, 03 (the last step in the described situation)

$now=time();
$suffix=0;
$post_id=30;
$dir='posts/'.$post_id.'/';
if(!is_dir($dir)) { mkdir($dir,0777,true); }
while(is_file($dir.$mktime.'_'.$lock.'_'.$suffix.'.txt')) {
  $suffix++;
}

The while should not be neede but i usually keep it anyway just in case :). That should create a txt file 30/69848968695_01_0.txt and ..02_0.txt and ..03_0.txt.

When you want to show the comments you just sort them by filename....


The database size need not increase. All you need is a single row. In concept the logic goes:

 Read row, taking lock, getting the current count
 Write row with count incremented, releasing lock

Note that you're using the database locks to deal with the possibilities that multiple requests are being processed at the same time.

So I'm suggesting to use the database as the place to manage your count. You can still write your other data to files if you wish. However you'll still need housekeeping for the files. Is that much harder with a database?


I agree with some of the other commenters that, regardless of whichever problem you are trying to solve, you may be making it more difficult than it needs to be.

Your example is mentioning putting comments in a file and keeping them outside the database.

What is the purpose of your count, exactly? You want to count the number of comments a user has made? Or total number of comment requests exactly?

If you don't have to update the count anywhere in real time, you could write a simple script that reads your server access logs and adds up the total.

Also, Matthew points out above, if you want requests to be handled in a particular order you will be rapidly heading for strange concurrency bugs and performance issues.

If you update your post to include details more explicitly, we should be able to help you further.

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜