1
0
mirror of https://github.com/Yubico/yubikey-val.git synced 2025-02-18 06:54:19 +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 # Loop forever and resync
do { do {
$sl->reSync($baseParams['__YKVAL_SYNC_OLD_LIMIT__'], $start = time();
$baseParams['__YKVAL_SYNC_RESYNC_TIMEOUT__']); $sl->reSync($baseParams['__YKVAL_SYNC_OLD_LIMIT__'],
} while(sleep($baseParams['__YKVAL_SYNC_INTERVAL__'])==0); $baseParams['__YKVAL_SYNC_RESYNC_TIMEOUT__']);
$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-db.php';
require_once 'ykval-log.php'; require_once 'ykval-log.php';
function _get($arr, $key) {
return array_key_exists($key, $arr) ? $arr[$key] : "?";
}
class SyncLib class SyncLib
{ {
public $syncServers = array(); public $syncServers = array();
@ -153,13 +157,13 @@ class SyncLib
public function log($priority, $msg, $params=NULL) public function log($priority, $msg, $params=NULL)
{ {
if ($params) if ($params)
$msg .= ' modified=' . $params['modified'] . $msg .= ' modified=' . _get($params, 'modified') .
' nonce=' . $params['nonce'] . ' nonce=' . _get($params, 'nonce') .
' yk_publicname=' . $params['yk_publicname'] . ' yk_publicname=' . _get($params, 'yk_publicname') .
' yk_counter=' . $params['yk_counter'] . ' yk_counter=' . _get($params, 'yk_counter') .
' yk_use=' . $params['yk_use'] . ' yk_use=' . _get($params, 'yk_use') .
' yk_high=' . $params['yk_high'] . ' yk_high=' . _get($params, 'yk_high') .
' yk_low=' . $params['yk_low']; ' yk_low=' . _get($params, 'yk_low');
if ($this->myLog) if ($this->myLog)
$this->myLog->log($priority, $msg); $this->myLog->log($priority, $msg);
@ -284,6 +288,8 @@ class SyncLib
$ch = array(); $ch = array();
$entries = array(); $entries = array();
$handles = 0; $handles = 0;
$num_per_server = 4;
$curlopts = $this->curlopts;
while ($my_server = $this->db->fetchArray($server_res)) while ($my_server = $this->db->fetchArray($server_res))
{ {
@ -299,27 +305,36 @@ class SyncLib
$list[] = $entry; $list[] = $entry;
} }
$server_list[$server] = $list; $server_list[$server] = $list;
$handle = curl_init();
$ch[$server] = $handle;
$this->db->closeCursor($res); $this->db->closeCursor($res);
} }
$this->db->closeCursor($server_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) { foreach ($server_list as $server) {
$entry = array_shift($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);
$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) { if(count($server) == 0) {
unset($server_list[$entry['server']]); unset($server_list[$entry['server']]);
} }
$handle = $ch[$entry['server']];
$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;
curl_multi_add_handle($mh, $handle);
$handles++;
} }
while($handles > 0) { while($handles > 0) {
@ -328,7 +343,8 @@ class SyncLib
while ($info = curl_multi_info_read($mh)) { while ($info = curl_multi_info_read($mh)) {
$handle = $info['handle']; $handle = $info['handle'];
$server = strtok(curl_getinfo($handle, CURLINFO_EFFECTIVE_URL), "?"); $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."); $this->log(LOG_DEBUG, "handle indicated to be for $server.");
curl_multi_remove_handle($mh, $handle); curl_multi_remove_handle($mh, $handle);
$handles--; $handles--;
@ -420,7 +436,7 @@ class SyncLib
$this->log(LOG_ERR, 'Remote server refused our sync request. Check remote server logs.'); $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]); $entry = array_shift($server_list[$server]);
if(count($server_list[$server]) == 0) { if(count($server_list[$server]) == 0) {
$this->log(LOG_DEBUG, "All entries for $server synced."); $this->log(LOG_DEBUG, "All entries for $server synced.");
@ -430,8 +446,9 @@ class SyncLib
$url = $this->buildSyncUrl($entry); $url = $this->buildSyncUrl($entry);
curl_settings($this, 'YK-VAL resync', $handle, $url, $timeout, $this->curlopts); $curlopts[CURLOPT_PRIVATE] = $label;
$entries[$server] = $entry; curl_settings($this, 'YK-VAL resync', $handle, $url, $timeout, $curlopts);
$entries[$label] = $entry;
curl_multi_add_handle($mh, $handle); curl_multi_add_handle($mh, $handle);
$handles++; $handles++;
} }
@ -443,7 +460,7 @@ class SyncLib
} }
foreach ($ch as $handle) { foreach ($ch as $handle) {
curl_close($ch); curl_close($handle);
} }
curl_multi_close($mh); curl_multi_close($mh);