Proper lock management

This commit is contained in:
Daniil Gentili 2018-02-18 14:59:39 +00:00
parent 6d26bf2a95
commit f352a1d285
4 changed files with 22 additions and 14 deletions

View File

@ -30,6 +30,7 @@ class API extends APIFactory
clearstatcache();
}
$lock = fopen($lock, 'r');
\danog\MadelineProto\Logger::log(['Waiting for shared lock of serialization lockfile...']);
flock($lock, LOCK_SH);
$unserialized = file_get_contents($params);
flock($lock, LOCK_UN);

View File

@ -81,7 +81,7 @@ class Logger
];
public static $storage = [];
public static $mode = null;
public static $mode = 1;
public static $optional = null;
public static $constructed = false;
public static $prefix = '';
@ -165,7 +165,7 @@ class Logger
return false;
}
if (!self::$constructed) {
throw new Exception(\danog\MadelineProto\Lang::$current_lang['constructor_function_uncalled']);
// throw new Exception(\danog\MadelineProto\Lang::$current_lang['constructor_function_uncalled']);
}
$prefix = self::$prefix;
if (\danog\MadelineProto\Logger::$has_thread && is_object(\Thread::getCurrentThread())) {

View File

@ -240,11 +240,15 @@ trait Files
$file = realpath($file);
$message_media = $this->get_download_info($message_media);
$stream = fopen($file, 'r+b');
\danog\MadelineProto\Logger::log(['Waiting for lock of file to download...']);
flock($stream, LOCK_EX);
$this->download_to_stream($message_media, $stream, $cb, filesize($file), -1);
flock($stream, LOCK_UN);
fclose($stream);
clearstatcache();
try {
$this->download_to_stream($message_media, $stream, $cb, filesize($file), -1);
} finally {
flock($stream, LOCK_UN);
fclose($stream);
clearstatcache();
}
return $file;
}
@ -265,7 +269,7 @@ trait Files
$end = $message_media['size'];
}
$size = $end - $offset;
$part_size = 128 * 1024;
$part_size = 1024 * 1024;
$percent = 0;
$datacenter = isset($message_media['InputFileLocation']['dc_id']) ? $message_media['InputFileLocation']['dc_id'] : $this->datacenter->curdc;
if (isset($message_media['key'])) {
@ -290,9 +294,6 @@ trait Files
$res = $cdn ? $this->method_call('upload.getCdnFile', ['file_token' => $message_media['file_token'], 'offset' => $offset, 'limit' => $part_size], ['heavy' => true, 'datacenter' => $datacenter]) : $this->method_call('upload.getFile', ['location' => $message_media['InputFileLocation'], 'offset' => $offset, 'limit' => $part_size], ['heavy' => true, 'datacenter' => &$datacenter]);
} catch (\danog\MadelineProto\RPCErrorException $e) {
switch ($e->rpc) {
case 'OFFSET_INVALID':
//\Rollbar\Rollbar::log(\Rollbar\Payload\Level::error(), $e->rpc, ['info' => $message_media, 'offset' => $offset]);
break;
case 'FILE_TOKEN_INVALID':
$cdn = false;
continue 2;

View File

@ -52,11 +52,15 @@ class Serialization
clearstatcache();
}
$lock = fopen($lock, 'w');
\danog\MadelineProto\Logger::log(['Waiting for exclusive lock of serialization lockfile...']);
flock($lock, LOCK_EX);
$wrote = file_put_contents($filename.'.temp.session', serialize($instance));
rename($filename.'.temp.session', $filename);
flock($lock, LOCK_UN);
fclose($lock);
try {
$wrote = file_put_contents($filename.'.temp.session', serialize($instance));
rename($filename.'.temp.session', $filename);
} finally {
flock($lock, LOCK_UN);
fclose($lock);
}
return $wrote;
}
@ -78,6 +82,8 @@ class Serialization
clearstatcache();
}
$lock = fopen($lock, 'r');
\danog\MadelineProto\Logger::log(['Waiting for shared lock of serialization lockfile...']);
flock($lock, LOCK_SH);
$unserialized = file_get_contents($filename);
flock($lock, LOCK_UN);