Skip to content

Commit

Permalink
Implement semaphore retry. Make not getting the semaphore an error
Browse files Browse the repository at this point in the history
  • Loading branch information
Isaac Connor committed May 31, 2022
1 parent da0cf59 commit 6479a1e
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions web/ajax/stream.php
Expand Up @@ -7,15 +7,25 @@
define('MSG_DATA_SIZE', 4+256);

if ( !($_REQUEST['connkey'] && $_REQUEST['command']) ) {
ajaxError("Unexpected received message type '$type'");
ajaxError('No connkey or no command in stream ajax');
}

mkdir(ZM_PATH_SOCKS);

# The file that we point ftok to has to exist, and only exist if zms is running, so we are pointing it at the .sock
$key = ftok(ZM_PATH_SOCKS.'/zms-'.sprintf('%06d', $_REQUEST['connkey']).'s.sock', 'Z');
$semaphore = sem_get($key, 1);
if (sem_acquire($semaphore, 1) !== false) {
$semaphore_tries = 10;
$have_semaphore = false;

while ($semaphore_tries) {
$have_semaphore = sem_acquire($semaphore, 1);
if ($have_semaphore !== false) break;
ZM\Debug('Failed to get semaphore, trying again');
usleep(100000);
$semaphore_tries -= 1;
}
if ($have_semaphore) {
if ( !($socket = @socket_create(AF_UNIX, SOCK_DGRAM, 0)) ) {
ajaxError('socket_create() failed: '.socket_strerror(socket_last_error()));
}
Expand Down Expand Up @@ -148,12 +158,11 @@
ajaxResponse(array('status'=>$data));
break;
default :
ajaxError("Unexpected received message type '$type'");
ajaxError('Unexpected received message type '.$data['type']);
}
sem_release($semaphore);
} else {
ZM\Debug('Couldn\'t get semaphore');
ajaxResponse(array());
ajaxError("Unable to get semaphore.");
}

ajaxError('Unrecognised action or insufficient permissions in ajax/stream');
Expand Down

0 comments on commit 6479a1e

Please sign in to comment.