1
0
mirror of https://github.com/Yubico/yubikey-val.git synced 2024-11-29 00:24:13 +01:00

Merge branch 'jeamland-be-more-parallel'

This commit is contained in:
Jean Paul Galea 2019-11-11 10:33:33 +01:00
commit 4d8b422808
No known key found for this signature in database
GPG Key ID: FC31B42E20461D82
2 changed files with 48 additions and 28 deletions

View File

@ -54,6 +54,9 @@ if ($sl->getNumberOfServers() === 0 && $sl->getQueueLength() === 0)
# Loop forever and resync
do {
$start = time();
$sl->reSync($baseParams['__YKVAL_SYNC_OLD_LIMIT__'],
$baseParams['__YKVAL_SYNC_RESYNC_TIMEOUT__']);
} while(sleep($baseParams['__YKVAL_SYNC_INTERVAL__'])==0);
$duration = time() - $start;
$sleep_time = max($baseParams['__YKVAL_SYNC_INTERVAL__'] - $duration, 0);
} while(sleep($sleep_time)==0);

View File

@ -32,6 +32,10 @@ require_once 'ykval-common.php';
require_once 'ykval-db.php';
require_once 'ykval-log.php';
function _get($arr, $key) {
return array_key_exists($key, $arr) ? $arr[$key] : "?";
}
class SyncLib
{
public $syncServers = array();
@ -153,13 +157,13 @@ class SyncLib
public function log($priority, $msg, $params=NULL)
{
if ($params)
$msg .= ' modified=' . $params['modified'] .
' nonce=' . $params['nonce'] .
' yk_publicname=' . $params['yk_publicname'] .
' yk_counter=' . $params['yk_counter'] .
' yk_use=' . $params['yk_use'] .
' yk_high=' . $params['yk_high'] .
' yk_low=' . $params['yk_low'];
$msg .= ' modified=' . _get($params, 'modified') .
' nonce=' . _get($params, 'nonce') .
' yk_publicname=' . _get($params, 'yk_publicname') .
' yk_counter=' . _get($params, 'yk_counter') .
' yk_use=' . _get($params, 'yk_use') .
' yk_high=' . _get($params, 'yk_high') .
' yk_low=' . _get($params, 'yk_low');
if ($this->myLog)
$this->myLog->log($priority, $msg);
@ -284,6 +288,8 @@ class SyncLib
$ch = array();
$entries = array();
$handles = 0;
$num_per_server = 4;
$curlopts = $this->curlopts;
while ($my_server = $this->db->fetchArray($server_res))
{
@ -299,28 +305,37 @@ class SyncLib
$list[] = $entry;
}
$server_list[$server] = $list;
$handle = curl_init();
$ch[$server] = $handle;
$this->db->closeCursor($res);
}
$this->db->closeCursor($server_res);
/* add one entry for each server we're going to sync */
/* add up to n entries for each server we're going to sync */
foreach ($server_list as $server) {
$entry = array_shift($server);
if(count($server) == 0) {
unset($server_list[$entry['server']]);
}
$handle = $ch[$entry['server']];
$items = array_slice($server, 0, $num_per_server);
$counter = 0;
foreach ($items as $entry) {
$label = "{$entry['server']}:$counter";
$handle = curl_init();
$ch[$label] = $handle;
$counter++;
$this->log(LOG_INFO, "server=" . $entry['server'] . ", server_nonce=" . $entry['server_nonce'] . ", info=" . $entry['info']);
$url = $this->buildSyncUrl($entry);
curl_settings($this, 'YK-VAL resync', $handle, $url, $timeout, $this->curlopts);
$entries[$entry['server']] = $entry;
$curlopts[CURLOPT_PRIVATE] = $label;
curl_settings($this, 'YK-VAL resync', $handle, $url, $timeout, $curlopts);
$entries[$label] = $entry;
curl_multi_add_handle($mh, $handle);
$handles++;
}
$empty = array();
array_splice($server, 0, $num_per_server, $empty);
if(count($server) == 0) {
unset($server_list[$entry['server']]);
}
}
while($handles > 0) {
while (curl_multi_exec($mh, $active) == CURLM_CALL_MULTI_PERFORM);
@ -328,7 +343,8 @@ class SyncLib
while ($info = curl_multi_info_read($mh)) {
$handle = $info['handle'];
$server = strtok(curl_getinfo($handle, CURLINFO_EFFECTIVE_URL), "?");
$entry = $entries[$server];
$label = curl_getinfo($handle, CURLINFO_PRIVATE);
$entry = $entries[$label];
$this->log(LOG_DEBUG, "handle indicated to be for $server.");
curl_multi_remove_handle($mh, $handle);
$handles--;
@ -420,7 +436,7 @@ class SyncLib
$this->log(LOG_ERR, 'Remote server refused our sync request. Check remote server logs.');
}
if($server_list[$server]) {
if (array_key_exists($server, $server_list)) {
$entry = array_shift($server_list[$server]);
if(count($server_list[$server]) == 0) {
$this->log(LOG_DEBUG, "All entries for $server synced.");
@ -430,8 +446,9 @@ class SyncLib
$url = $this->buildSyncUrl($entry);
curl_settings($this, 'YK-VAL resync', $handle, $url, $timeout, $this->curlopts);
$entries[$server] = $entry;
$curlopts[CURLOPT_PRIVATE] = $label;
curl_settings($this, 'YK-VAL resync', $handle, $url, $timeout, $curlopts);
$entries[$label] = $entry;
curl_multi_add_handle($mh, $handle);
$handles++;
}
@ -443,7 +460,7 @@ class SyncLib
}
foreach ($ch as $handle) {
curl_close($ch);
curl_close($handle);
}
curl_multi_close($mh);