Commit d4303ecfda4c98bdfab375fb0921eb617bee4774
1 parent
56056fae
locking mechanismen hinuzgefuegt...so gut wies eben geht
Showing
6 changed files
with
111 additions
and
148 deletions
@@ -17,3 +17,4 @@ | @@ -17,3 +17,4 @@ | ||
17 | http://www.php-faq.de/search.php?q=Locking&l=20 | 17 | http://www.php-faq.de/search.php?q=Locking&l=20 |
18 | web developer | 18 | web developer |
19 | http://de.wikipedia.org/wiki/Semaphor_%28Informatik%29 -djikstra PV- | 19 | http://de.wikipedia.org/wiki/Semaphor_%28Informatik%29 -djikstra PV- |
20 | + http://www.kreissl.info/diggs/bs_04.php |
1 | <?php | 1 | <?php |
2 | 2 | ||
3 | -// $english = mysql_escape_string($_REQUEST['translate']); | ||
4 | -// Der Service ist zur Zeit leider deaktiviert.... | ||
5 | -// $trans = new SoapClient( | ||
6 | -// "http://www.xmethods.net/sd/2001/BabelFishService.wsdl"); | ||
7 | - | ||
8 | -/* | ||
9 | - try | ||
10 | - { | ||
11 | - $german = $trans->BabelFish("en_de",$english); | ||
12 | - $french = $trans->BabelFish("en_fr",$english); | ||
13 | - } | ||
14 | - catch(SoapFault $e) | ||
15 | - { | ||
16 | - $english = "not found"; | ||
17 | - $german = "not found"; | ||
18 | - $french = "not found"; | ||
19 | - } | ||
20 | -*/ | ||
21 | - class dummy | 3 | +class dummy |
4 | +{ | ||
5 | + // nur public member werden via json verschickt. | ||
6 | + public $german; | ||
7 | + public $english; | ||
8 | + public $french; | ||
9 | + | ||
10 | + function __construct ($english, $german, $french) | ||
22 | { | 11 | { |
23 | - // nur public member werden via json verschickt. | ||
24 | - public $german; | ||
25 | - public $english; | ||
26 | - public $french; | ||
27 | - | ||
28 | - function __construct ($english, $german, $french) | ||
29 | - { | ||
30 | - $this->german = $german; | ||
31 | - $this->english = $english; | ||
32 | - $this->french["val1"] = $french; | ||
33 | - $this->french["val2"] = "jokus"; | ||
34 | - } | ||
35 | - } | ||
36 | - | ||
37 | - $encoding = FALSE; | ||
38 | - if (isset ($_SERVER['HTTP_ACCEPT_ENCODING']) && | ||
39 | - strpos ($_SERVER['HTTP_ACCEPT_ENCODING'], 'x-gzip') !== FALSE) | ||
40 | - $encoding = "x-gzip"; | ||
41 | - if (isset ($_SERVER['HTTP_ACCEPT_ENCODING']) && | ||
42 | - strpos ($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE) | ||
43 | - $encoding = "gzip"; | ||
44 | - | ||
45 | - header ('Content-type: text/plain'); | ||
46 | - | ||
47 | - $result = FALSE; | ||
48 | - if ($encoding !== FALSE) | ||
49 | - $result = gzcompress (json_encode (new dummy ( | ||
50 | - 'doing gzip', 'gezipedte Daten', 'la zippo'))); | ||
51 | - | ||
52 | - if ($result !== FALSE) | ||
53 | - { | ||
54 | - header ('Content-Encoding: ' . $encoding); | ||
55 | - | ||
56 | - print ("\x1f\x8b\x08\x00\x00\x00\x00\x00"); | ||
57 | - print ($result); | ||
58 | - } | ||
59 | - else | ||
60 | - { | ||
61 | - $result = json_encode (new dummy ( | ||
62 | - 'doing no gzip', 'nicht gezipedte Daten', 'no la zippo')); | ||
63 | - | ||
64 | - print ($result); | 12 | + $this->german = $german; |
13 | + $this->english = $english; | ||
14 | + $this->french["val1"] = $french; | ||
15 | + $this->french["val2"] = "jokus"; | ||
65 | } | 16 | } |
17 | +} | ||
18 | + | ||
19 | +$encoding = FALSE; | ||
20 | +if (isset ($_SERVER['HTTP_ACCEPT_ENCODING']) && | ||
21 | + strpos ($_SERVER['HTTP_ACCEPT_ENCODING'], 'x-gzip') !== FALSE) | ||
22 | +$encoding = "x-gzip"; | ||
23 | +if (isset ($_SERVER['HTTP_ACCEPT_ENCODING']) && | ||
24 | + strpos ($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE) | ||
25 | +$encoding = "gzip"; | ||
26 | + | ||
27 | +header ('Content-type: text/plain'); | ||
28 | + | ||
29 | +$result = FALSE; | ||
30 | +if ($encoding !== FALSE) | ||
31 | + $result = gzcompress (json_encode (new dummy ( | ||
32 | + 'doing gzip', 'gezipedte Daten', 'la zippo'))); | ||
33 | + | ||
34 | +if ($result !== FALSE) | ||
35 | +{ | ||
36 | + header ('Content-Encoding: ' . $encoding); | ||
37 | + | ||
38 | + print ("\x1f\x8b\x08\x00\x00\x00\x00\x00"); | ||
39 | + print ($result); | ||
40 | +} | ||
41 | +else | ||
42 | +{ | ||
43 | + $result = json_encode (new dummy ( | ||
44 | + 'doing no gzip', 'nicht gezipedte Daten', 'no la zippo')); | ||
45 | + | ||
46 | + print ($result); | ||
47 | +} | ||
66 | 48 | ||
67 | ?> | 49 | ?> |
@@ -67,7 +67,7 @@ | @@ -67,7 +67,7 @@ | ||
67 | 67 | ||
68 | // anfrage erstellen (GET, url ist localhost, | 68 | // anfrage erstellen (GET, url ist localhost, |
69 | // request ist asynchron | 69 | // request ist asynchron |
70 | - var url = 'http://muedv031/~georg/bilder/ajax+json/ajax.php' + | 70 | + var url = 'http://localhost/~georg/bilder/ajax+json_home/ajax.php' + |
71 | '?translate=' + document.getElementById ('myword').value; | 71 | '?translate=' + document.getElementById ('myword').value; |
72 | 72 | ||
73 | req.open("GET", url, true); | 73 | req.open("GET", url, true); |
@@ -49,7 +49,7 @@ function setErrExceptionMapping () | @@ -49,7 +49,7 @@ function setErrExceptionMapping () | ||
49 | { | 49 | { |
50 | global $oldErrHandler; | 50 | global $oldErrHandler; |
51 | 51 | ||
52 | - if ($oldErrHandler == NULL) | 52 | + if ($oldErrHandler === NULL) |
53 | $oldErrHandler = set_error_handler ('mapErrToExc'); | 53 | $oldErrHandler = set_error_handler ('mapErrToExc'); |
54 | } | 54 | } |
55 | 55 | ||
@@ -57,7 +57,7 @@ function resetErrExceptionMapping () | @@ -57,7 +57,7 @@ function resetErrExceptionMapping () | ||
57 | { | 57 | { |
58 | global $oldErrHandler; | 58 | global $oldErrHandler; |
59 | 59 | ||
60 | - if ($oldErrHandler != NULL) | 60 | + if ($oldErrHandler !== NULL) |
61 | { | 61 | { |
62 | set_error_handler ($oldErrHandler); | 62 | set_error_handler ($oldErrHandler); |
63 | $oldErrHandler = NULL; | 63 | $oldErrHandler = NULL; |
libs/mutualExclusion.php
0 → 100644
1 | +<?php | ||
2 | + | ||
3 | +require_once dirname(__FILE__) . '/../config.php'; | ||
4 | +require_once LIBDIR . 'errException.php'; | ||
5 | + | ||
6 | + | ||
7 | +function acquireLock ($lockFile, $csId) | ||
8 | +{ | ||
9 | + $fName = $lockFile . $csId . '.lck'; | ||
10 | + $lock = NULL; | ||
11 | + | ||
12 | + setErrExceptionMapping (); | ||
13 | + | ||
14 | + $lockHandle = fopen ($fName, 'w'); | ||
15 | + | ||
16 | + // if available use semaphores for mutual exclusion because flock | ||
17 | + // doesn't work reliably in threaded environments. | ||
18 | + if (function_exists ('sem_get')) | ||
19 | + { | ||
20 | + $lock = sem_get (ftok ($fName, $csId), 1, 0644, TRUE); | ||
21 | + | ||
22 | + $state = sem_acquire ($lock); | ||
23 | + while ($state === FALSE) | ||
24 | + $state = sem_acquire ($lock); | ||
25 | + } | ||
26 | + else | ||
27 | + { | ||
28 | + $state = flock ($lock, LOCK_EX); | ||
29 | + while ($state === FALSE) | ||
30 | + $state = flock ($lock, LOCK_EX); | ||
31 | + } | ||
32 | + | ||
33 | + // Here one could write informations in the lockfile...time, pid, etc. | ||
34 | + fwrite ($lockHandle, session_id () . '::' . time ()); | ||
35 | + fflush ($lockHandle); | ||
36 | + | ||
37 | + resetErrExceptionMapping (); | ||
38 | + | ||
39 | + $userAbort = ignore_user_abort(TRUE); | ||
40 | + return array ($lockHandle, $lock, $userAbort); | ||
41 | +} | ||
42 | + | ||
43 | +function releaseLock ($lock) | ||
44 | +{ | ||
45 | + setErrExceptionMapping (); | ||
46 | + | ||
47 | + if ($lock[1] !== NULL) | ||
48 | + sem_release ($lock[1]); | ||
49 | + | ||
50 | + ftruncate ($lock[0], 0); | ||
51 | + fclose ($lock[0]); | ||
52 | + | ||
53 | + resetErrExceptionMapping (); | ||
54 | + | ||
55 | + ignore_user_abort($lock[2]); | ||
56 | +} | ||
57 | + | ||
58 | +?> |
1 | <?php | 1 | <?php |
2 | 2 | ||
3 | -/* | ||
4 | - * Zunächst mal den Gedanken skizziert | ||
5 | - * Da es kein wirklich zuverlässiges Locking fuer kritische Bereiche | ||
6 | - * in php gibt. Semaphore existiren nur unter Unix und flock arbeitet nicht | ||
7 | - * wenn der webserver threaded ist und keine separaten prozesse pro | ||
8 | - * request erzeugt habe ich folgenden Gedankengang...nicht 100% Atomar, aber | ||
9 | - * möglicherweise das beste was man bekommen kann. | ||
10 | - * | ||
11 | - * ACQUIRE | ||
12 | - * ======= | ||
13 | - * | | ||
14 | - * - leere Datei erzeugen (tmp) | ||
15 | - * - link (tmp) nach (lock) | ||
16 | - * | | ||
17 | - * |----------------------------- | ||
18 | - * OK | | Fehler | | ||
19 | - * | | | | ||
20 | - * | | - lese (lock): sollte blockieren solange diese leer ist | ||
21 | - * | | - unlink (lock) | ||
22 | - * | | - link (tmp) nach (lock) | ||
23 | - * | Fehler | | | ||
24 | - * | ---------------| | ||
25 | - * | | OK | ||
26 | - * | | | ||
27 | - * |----------------------------- | ||
28 | - * | | ||
29 | - * - (tmp) loeschen | ||
30 | - * | | ||
31 | - * (ENDE) | ||
32 | - * | ||
33 | - * RELEASE | ||
34 | - * ======= | ||
35 | - * | | ||
36 | - * - schreibe nach (lock) | ||
37 | - * | | ||
38 | - * (ENDE) | ||
39 | - * | ||
40 | - * Da link angeblick atomar ist muesste das zumindest zuverlaessig Sperren... | ||
41 | - * Zusaettzlich sollte man vielleicht noch ein Priorisierung irgendwo | ||
42 | - * festhalten, um zu verhindern das ein Prozess ewig wartet, weil er immer | ||
43 | - * Pech bei der zuweisung von CPU Zeit hat. | ||
44 | - * | ||
45 | - * So ein Pech, read blockiert natuerlich nicht bei regulären Dateien, da | ||
46 | - * ja EOF erreich wird....da muss ich mir was anderes ausdenken.... | ||
47 | - * OK, unter UNIX kann ich exec mkfifo machen um eine named pipe zu generieren. | ||
48 | - * Fuer windows muss was anderes oder busy waiting (iiieeh) rein.... | ||
49 | - * | ||
50 | - * !!! Zur info, schon der read open auf eine FIFO blocked wenn kein Writer | ||
51 | - * existiert. Das war auch ein aspekt den man berücksichtigen muß... !!! | ||
52 | - */ | 3 | +require_once dirname(__FILE__) . '/config.php'; |
4 | +require_once LIBDIR . 'mutualExclusion.php'; | ||
53 | 5 | ||
54 | -function acquire ($lock, $tmp) | ||
55 | -{ | ||
56 | - if (! file_exists ($tmp)) | ||
57 | - exec ('mkfifo ' . $tmp); | ||
58 | - // hier koennte irgendwas bzgl. der Priorisierung in nem else folgen... | ||
59 | 6 | ||
60 | - if (link ($tmp, $lock) === FALSE) | ||
61 | - { | ||
62 | - while (true) | ||
63 | - { | ||
64 | - $handle = fopen ($lock, 'w'); | ||
65 | - fwrite ($handel, '1'); | ||
66 | - fclose ($handle); | ||
67 | - if (unlink ($lock) === FALSE) continue; | ||
68 | - if (link ($tmp, $lock) === FALSE) continue; | ||
69 | - break; | ||
70 | - } | ||
71 | - } | ||
72 | - | ||
73 | - unlink ($tmp); | ||
74 | -} | ||
75 | - | ||
76 | -function release ($lock) | ||
77 | -{ | ||
78 | - $handle = fopen ($lock, 'r'); | ||
79 | - stream_set_blocking ($handle, 0); | ||
80 | - if (fread ($handle, 1) === 0) | ||
81 | - unlink ($lock); | ||
82 | - fclose ($handle); | ||
83 | -} | ||
84 | - | ||
85 | - | ||
86 | -acquire ('lock', 'tmp'); | 7 | +//session_start (); |
8 | +$lock = acquireLock ('lock/lock', '1'); | ||
87 | 9 | ||
88 | echo "Lock bekommen\n"; | 10 | echo "Lock bekommen\n"; |
89 | -sleep (30); | 11 | +sleep (2); |
90 | 12 | ||
91 | -release ('lock'); | 13 | +releaseLock ($lock); |
92 | 14 | ||
93 | ?> | 15 | ?> |
Please
register
or
login
to post a comment