Implement semaphore retry. Make not getting the semaphore an error

This commit is contained in:
Isaac Connor 2021-12-17 09:41:45 -05:00
parent 1bc0e1820e
commit edbe726e86
1 changed files with 15 additions and 6 deletions

View File

@ -1,5 +1,5 @@
<?php <?php
error_reporting(0); error_reporting(0);
$start_time = time(); $start_time = time();
@ -7,7 +7,7 @@ define('MSG_TIMEOUT', ZM_WEB_AJAX_TIMEOUT/2);
define('MSG_DATA_SIZE', 4+256); define('MSG_DATA_SIZE', 4+256);
if ( !($_REQUEST['connkey'] && $_REQUEST['command']) ) { if ( !($_REQUEST['connkey'] && $_REQUEST['command']) ) {
ajaxError("Unexpected received message type '$type'"); ajaxError('No connkey or no command in stream ajax');
} }
mkdir(ZM_PATH_SOCKS); mkdir(ZM_PATH_SOCKS);
@ -15,7 +15,17 @@ 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 # 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'); $key = ftok(ZM_PATH_SOCKS.'/zms-'.sprintf('%06d',$_REQUEST['connkey']).'s.sock', 'Z');
$semaphore = sem_get($key,1); $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)) ) { if ( !($socket = @socket_create(AF_UNIX, SOCK_DGRAM, 0)) ) {
ajaxError('socket_create() failed: '.socket_strerror(socket_last_error())); ajaxError('socket_create() failed: '.socket_strerror(socket_last_error()));
} }
@ -161,12 +171,11 @@ if ( sem_acquire($semaphore,1) !== false ) {
ajaxResponse(array('status'=>$data)); ajaxResponse(array('status'=>$data));
break; break;
default : default :
ajaxError("Unexpected received message type '$type'"); ajaxError('Unexpected received message type '.$data['type']);
} }
sem_release($semaphore); sem_release($semaphore);
} else { } else {
ZM\Debug('Couldn\'t get semaphore'); ajaxError("Unable to get semaphore.");
ajaxResponse(array());
} }
ajaxError('Unrecognised action or insufficient permissions in ajax/stream'); ajaxError('Unrecognised action or insufficient permissions in ajax/stream');