Queue with priority-PHP implementation

Source: Internet
Author: User

A feature that was written a long time ago, when a priority queue was needed, a simple demo was written with the newly-learned swoole, only to meet the needs of the time.

function Description :

      Full Reference HTTPSQS Add priority parameter level

Cases:
Http://192.168.8.18:5555/?name=t ... a=testdata1&level=1
Http://192.168.8.18:5555/?name=t ... a=testdata2&level=1
Http://192.168.8.18:5555/?name=t ... a=testdata3&level=3
At this point, the loop calls the Get method and gets the data testdata3,testdata1,testdata2
Note: Get the first priority to pop up data, the same priority data, according to put the successively popped;

Tools to use:

php php swoole extensions

Ideas:

      use Swoole to extend the listening port, receive the parameters passed by the user, store all queue names with global variables $info, state information for each queue (all priority, number of read data in each priority, number of unread data), set timer, write info to file periodically The actual data is stored in a file, one row per data, and the fgets is read by line; global variable $fp saves all open file handles, reducing the number of open files;

Advantage:
PHP Development using the best language in the world!

Performance test:
1: Write Performance:

Ab-c 100-n 10000 http://192.168.8.18:5555/?name=test\&opt=put\&data=testdata\&level=3

2: Read performance

Ab-c 100-n 10000 Http://192.168.8.18:5555/?name=test\&opt=get

Attached: Base code

<?php//HTTP Queue service with optimized level error_reporting (E_error | e_warning | E_parse);d ate_default_timezone_set (' PRC '); $dirname = "./data/queue_"; File storage location and filename $infodirname = "./data/info"; File storing basic Information $maxfilesize = 1024 * 1024 * 1024;//file Max 1g$fp = array (); Open File Handle $info = Array (); Store the basic information of each queue, Get/put's location $infoflag = 0; Flag info has been modified $tmp = file_get_contents ($infodirname);//Initialize, load basic information from File!empty ($tmp) && $info = Unserialize ($  TMP); $http = new Swoole_http_server ("0.0.0.0", 5555), $http->set (Array (' worker_num ' = 1,//number of worker processes), $http->on (' Workerstart ', function ($http) {$http->addtimer (10000);//Add Timer}), $http->on (' Timer ', ' cronwriteinfo ');  Periodically writes basic information to the file $http->on (' request ', ' mycallback '); $http->start ();    function Cronwriteinfo () {//timed write file Global $info, $infoflag, $infodirname;    if ($infoflag) {//info is modified obsolete before it is written to the file File_put_contents ($infodirname, serialize ($info));    }}function Mycallback ($request, $response) {global $info, $infoflag; if ($request-server[' Request_uri '] = = "/favicon.ico") {//filter out the browser's icon request $response->end ("\ n");    Return ';    } $infoflag = 1;    $param = $request->get;    if (Empty ($param [' name '])) {//queue name cannot be empty $ret = "Queue_error_queuename_empty";                } else {switch ($param [' opt ']) {case ' get ': $ret = Get ($param [' name ']);            Break Case ' put ': if (($data = $param [' data ']) &&!empty ($param [' Data '])) {Empty ($para                    m[' level ') && $param [' level '] = 0;                $ret = put ($param [' name '], $param [' Data '], $param [' Level '];                } else {$ret = "queue_put_error";            } break;                Case ' status ': $ret = Json_encode ($info);            Break                Default: $ret = "Queue_error_param";        Break }} $response->end ($ret. " \ n ");} function Get ($queuename) {GLObal $info; if ($info [$queuename] [' unread '] > 0) {$maxlevel = $info [$queuename] [' curmaxlevel '];//current maximum optimization level $pos = $inf o[$queuename [' Leveldata '] [$maxlevel] [' GetPos ']; File Offset $data = Getdatafromfile ($queuename, $maxlevel, $pos); Read Data $ret = Pack ("h*", $data);        Data is compressed and requires decompression//modification of global Variables $info [$queuename] [' Get ']++;        $info [$queuename] [' Unread ']--;        $info [$queuename] [' leveldata '] [$maxlevel] [' Unread ']--;             if ($ret = = "Queue_end") {//Read incomplete data, Reason: The data file size exceeds the maximum value setting; $data = Getdatafromfile ($queuename, $maxlevel, 0); Read again from the beginning $ret = Pack ("h*", $data); Data is compressed and needs to be decompressed $info [$queuename] [' leveldata '] [$maxlevel] [' get '] = 1;//read position is set to 1 $info [$queuename]        Leveldata '] [$maxlevel] [' getpos '] = strlen ($data) + 1;            } else {$info [$queuename] [' leveldata '] [$maxlevel] [' Get ']++;        $info [$queuename] [' leveldata '] [$maxlevel] [' getpos '] + = strlen ($data) + 1; } if ($info[$queuename] [' Leveldata '] [$maxlevel] [' get '] = = $info [$queuename] [' leveldata '] [$maxlevel] [' put ']) {//data read, recalculate current highest priority data $info [$queuename] [' Curma        Xlevel '] = Getcurmaxlevel ($info [$queuename] [' leveldata ']);    } return $ret;    } else {return ' queue_empty ';    }}function put ($queuename, $userdata, $level) {global $info; if ($info [$queuename] [' leveldata '] [$level] [' put ']+1 = = $info [$queuename] [' leveldata '] [$level] [' Get ']} {//queue full retur    n "Queue_full";    } $info [$queuename] [' Put ']++;    $info [$queuename] [' Unread ']++;    $newpos = Setdatatofile ($queuename, $level, $userdata);    $info [$queuename] [' leveldata '] [$level] ' put ']++;    $info [$queuename] [' leveldata '] [$level] [' Unread ']++;    $info [$queuename] [' leveldata '] [$level] [' putpos '] = $newpos;    $info [$queuename] [' curmaxlevel '] = Getcurmaxlevel ($info [$queuename] [' leveldata ']);  return "QUEUE_PUT_OK";}    Gets the highest-priority data function Getcurmaxlevel ($leveldata) {Krsort ($leveldata) currently required to execute; foreach$leveldata as $level = + $info) {if ($info [' unread '] > 0) {return $level;  }} return 0;}    Write data to a file, if the file is full, loop-write, function setdatatofile ($queuename, $level, $userdata) {Global $dirname, $maxfilesize, $fp;    $file = $dirname. $queuename. $level;    $packdata = Unpack ("h*", $userdata);    $ $packdata [' 1 '] = $userdata; $data = $packdata [' 1 ']. "    \ n ";    $size = FileSize ($file);    if (! $fp [$queuename] [$level]) {$fp = fopen ($file, "ab+");    } fseek ($fp, $size);    Fwrite ($fp, $data);    Clearstatcache (); if ($size + strlen ($data) + 1 > $maxfilesize) {//exceeds the maximum file size, enters the next round fseek ($fp, $size);//Returns the pointer fwrite ($FP, "QUE Ue_end ");        Write marker symbol fseek ($fp, 0);//write Fwrite ($fp, $data);    return strlen ($data);    } else {return $size + strlen ($data);    }}//reading data from a file function Getdatafromfile ($queuename, $level, $pos) {global $dirname, $fp; if (!isset ($fp [$queuename] [$level]) {$file = $dirname. $queuename. $lEvel;    $fp = fopen ($file, "ab+");    } fseek ($FP, Intval ($pos)); Return Trim (fgets ($FP));}

Queue with priority-PHP implementation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.