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(); clearstatcache();
} }
$lock = fopen($lock, 'r'); $lock = fopen($lock, 'r');
\danog\MadelineProto\Logger::log(['Waiting for shared lock of serialization lockfile...']);
flock($lock, LOCK_SH); flock($lock, LOCK_SH);
$unserialized = file_get_contents($params); $unserialized = file_get_contents($params);
flock($lock, LOCK_UN); flock($lock, LOCK_UN);

View File

@ -81,7 +81,7 @@ class Logger
]; ];
public static $storage = []; public static $storage = [];
public static $mode = null; public static $mode = 1;
public static $optional = null; public static $optional = null;
public static $constructed = false; public static $constructed = false;
public static $prefix = ''; public static $prefix = '';
@ -165,7 +165,7 @@ class Logger
return false; return false;
} }
if (!self::$constructed) { 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; $prefix = self::$prefix;
if (\danog\MadelineProto\Logger::$has_thread && is_object(\Thread::getCurrentThread())) { if (\danog\MadelineProto\Logger::$has_thread && is_object(\Thread::getCurrentThread())) {

View File

@ -240,11 +240,15 @@ trait Files
$file = realpath($file); $file = realpath($file);
$message_media = $this->get_download_info($message_media); $message_media = $this->get_download_info($message_media);
$stream = fopen($file, 'r+b'); $stream = fopen($file, 'r+b');
\danog\MadelineProto\Logger::log(['Waiting for lock of file to download...']);
flock($stream, LOCK_EX); flock($stream, LOCK_EX);
$this->download_to_stream($message_media, $stream, $cb, filesize($file), -1); try {
flock($stream, LOCK_UN); $this->download_to_stream($message_media, $stream, $cb, filesize($file), -1);
fclose($stream); } finally {
clearstatcache(); flock($stream, LOCK_UN);
fclose($stream);
clearstatcache();
}
return $file; return $file;
} }
@ -265,7 +269,7 @@ trait Files
$end = $message_media['size']; $end = $message_media['size'];
} }
$size = $end - $offset; $size = $end - $offset;
$part_size = 128 * 1024; $part_size = 1024 * 1024;
$percent = 0; $percent = 0;
$datacenter = isset($message_media['InputFileLocation']['dc_id']) ? $message_media['InputFileLocation']['dc_id'] : $this->datacenter->curdc; $datacenter = isset($message_media['InputFileLocation']['dc_id']) ? $message_media['InputFileLocation']['dc_id'] : $this->datacenter->curdc;
if (isset($message_media['key'])) { 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]); $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) { } catch (\danog\MadelineProto\RPCErrorException $e) {
switch ($e->rpc) { 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': case 'FILE_TOKEN_INVALID':
$cdn = false; $cdn = false;
continue 2; continue 2;

View File

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