Merge branch 'master' of https://gitee.com/dreamwithouttrace/snowflake into v1.0
This commit is contained in:
@@ -9,6 +9,7 @@ use HttpServer\Abstracts\Callback;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Exception\ConfigException;
|
||||
use Snowflake\Snowflake;
|
||||
use Swoole\Coroutine;
|
||||
use Swoole\Coroutine\System;
|
||||
use Swoole\Server;
|
||||
|
||||
@@ -32,10 +33,7 @@ class OnWorkerStart extends Callback
|
||||
putenv('state=start');
|
||||
putenv('worker=' . $worker_id);
|
||||
|
||||
$content = System::readFile(storage('runtime.php'));
|
||||
|
||||
$annotation = Snowflake::app()->getAnnotation();
|
||||
$annotation->setLoader(unserialize($content));
|
||||
$annotation = $this->settings();
|
||||
if ($worker_id < $server->setting['worker_num']) {
|
||||
$this->onWorker($server, $annotation);
|
||||
} else {
|
||||
@@ -44,6 +42,21 @@ class OnWorkerStart extends Callback
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return \Annotation\Annotation
|
||||
* @throws \Exception
|
||||
*/
|
||||
private function settings(): Annotation
|
||||
{
|
||||
$content = System::readFile(storage('runtime.php'));
|
||||
|
||||
$annotation = Snowflake::app()->getAnnotation();
|
||||
$annotation->setLoader(unserialize($content));
|
||||
|
||||
return $annotation;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $worker_id
|
||||
|
||||
+206
-212
@@ -13,239 +13,233 @@ use Swoole\Coroutine;
|
||||
class Context extends BaseContext
|
||||
{
|
||||
|
||||
protected static array $_contents = [];
|
||||
protected static array $_contents = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @param $context
|
||||
* @param null $key
|
||||
* @return mixed
|
||||
*/
|
||||
public static function setContext($id, $context, $key = null): mixed
|
||||
{
|
||||
if (!static::inCoroutine()) {
|
||||
return static::setStatic($id, $context, $key);
|
||||
}
|
||||
return self::setCoroutine($id, $context, $key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @param $context
|
||||
* @param null $key
|
||||
* @return mixed
|
||||
*/
|
||||
public static function setContext($id, $context, $key = null): mixed
|
||||
{
|
||||
if (static::inCoroutine()) {
|
||||
return self::setCoroutine($id, $context, $key);
|
||||
} else {
|
||||
return self::setStatic($id, $context, $key);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @param $id
|
||||
* @param $context
|
||||
* @param null $key
|
||||
* @return array
|
||||
*/
|
||||
private static function setStatic($id, $context, $key = null): mixed
|
||||
{
|
||||
if (empty($key)) {
|
||||
return static::$_contents[$id] = $context;
|
||||
}
|
||||
if (!is_array(static::$_contents[$id])) {
|
||||
static::$_contents[$id] = [$key => $context];
|
||||
} else {
|
||||
static::$_contents[$id][$key] = $context;
|
||||
}
|
||||
return $context;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @param $context
|
||||
* @param null $key
|
||||
* @return array
|
||||
*/
|
||||
private static function setStatic($id, $context, $key = null): mixed
|
||||
{
|
||||
if (empty($key)) {
|
||||
return static::$_contents[$id] = $context;
|
||||
}
|
||||
if (!is_array(static::$_contents[$id])) {
|
||||
static::$_contents[$id] = [$key => $context];
|
||||
} else {
|
||||
static::$_contents[$id][$key] = $context;
|
||||
}
|
||||
return $context;
|
||||
}
|
||||
/**
|
||||
* @param $id
|
||||
* @param $context
|
||||
* @param null $key
|
||||
* @return mixed
|
||||
*/
|
||||
private static function setCoroutine($id, $context, $key = null): mixed
|
||||
{
|
||||
if (empty($key)) {
|
||||
return Coroutine::getContext()[$id] = $context;
|
||||
}
|
||||
if (!is_array(Coroutine::getContext()[$id])) {
|
||||
Coroutine::getContext()[$id] = [$key => $context];
|
||||
} else {
|
||||
Coroutine::getContext()[$id][$key] = $context;
|
||||
}
|
||||
return $context;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @param $context
|
||||
* @param null $key
|
||||
* @return mixed
|
||||
*/
|
||||
private static function setCoroutine($id, $context, $key = null): mixed
|
||||
{
|
||||
if (empty($key)) {
|
||||
return Coroutine::getContext()[$id] = $context;
|
||||
}
|
||||
if (!is_array(Coroutine::getContext()[$id])) {
|
||||
Coroutine::getContext()[$id] = [$key => $context];
|
||||
} else {
|
||||
Coroutine::getContext()[$id][$key] = $context;
|
||||
}
|
||||
return $context;
|
||||
}
|
||||
/**
|
||||
* @param $id
|
||||
* @param null $key
|
||||
* @param int $value
|
||||
* @return bool|int
|
||||
*/
|
||||
public static function increment($id, $key = null, $value = 1): bool|int
|
||||
{
|
||||
if (!isset(Coroutine::getContext()[$id][$key])) {
|
||||
return false;
|
||||
}
|
||||
return Coroutine::getContext()[$id][$key] += $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @param null $key
|
||||
* @param int $value
|
||||
* @return bool|int
|
||||
*/
|
||||
public static function increment($id, $key = null, $value = 1): bool|int
|
||||
{
|
||||
if (!static::inCoroutine()) {
|
||||
return false;
|
||||
}
|
||||
if (!isset(Coroutine::getContext()[$id][$key])) {
|
||||
return false;
|
||||
}
|
||||
return Coroutine::getContext()[$id][$key] += $value;
|
||||
}
|
||||
/**
|
||||
* @param $id
|
||||
* @param null $key
|
||||
* @param int $value
|
||||
* @return bool|int
|
||||
*/
|
||||
public static function decrement($id, $key = null, $value = 1): bool|int
|
||||
{
|
||||
if (!static::hasContext($id)) {
|
||||
return false;
|
||||
}
|
||||
if (!isset(Coroutine::getContext()[$id][$key])) {
|
||||
return false;
|
||||
}
|
||||
return Coroutine::getContext()[$id][$key] -= $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @param null $key
|
||||
* @param int $value
|
||||
* @return bool|int
|
||||
*/
|
||||
public static function decrement($id, $key = null, $value = 1): bool|int
|
||||
{
|
||||
if (!static::inCoroutine() || !static::hasContext($id)) {
|
||||
return false;
|
||||
}
|
||||
if (!isset(Coroutine::getContext()[$id][$key])) {
|
||||
return false;
|
||||
}
|
||||
return Coroutine::getContext()[$id][$key] -= $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @param null $key
|
||||
* @return mixed
|
||||
*/
|
||||
public static function getContext($id, $key = null): mixed
|
||||
{
|
||||
if (!static::hasContext($id)) {
|
||||
return null;
|
||||
}
|
||||
if (static::inCoroutine()) {
|
||||
return static::loadByContext($id, $key);
|
||||
} else {
|
||||
return static::loadByStatic($id, $key);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @param $id
|
||||
* @param null $key
|
||||
* @return mixed
|
||||
*/
|
||||
public static function getContext($id, $key = null): mixed
|
||||
{
|
||||
if (!static::inCoroutine()) {
|
||||
return static::loadByStatic($id, $key);
|
||||
}
|
||||
return static::loadByContext($id, $key);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @param null $key
|
||||
* @return mixed
|
||||
*/
|
||||
private static function loadByContext($id, $key = null): mixed
|
||||
{
|
||||
$data = Coroutine::getContext()[$id] ?? null;
|
||||
if ($data === null) {
|
||||
return null;
|
||||
}
|
||||
if ($key !== null) {
|
||||
return $data[$key] ?? null;
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
/**
|
||||
* @param $id
|
||||
* @param null $key
|
||||
* @return mixed
|
||||
*/
|
||||
private static function loadByContext($id, $key = null): mixed
|
||||
{
|
||||
$data = Coroutine::getContext()[$id] ?? null;
|
||||
if ($data === null) {
|
||||
return null;
|
||||
}
|
||||
if ($key !== null) {
|
||||
return $data[$key] ?? null;
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @param null $key
|
||||
* @return mixed
|
||||
*/
|
||||
private static function loadByStatic($id, $key = null): mixed
|
||||
{
|
||||
$data = static::$_contents[$id] ?? null;
|
||||
if ($data === null) {
|
||||
return null;
|
||||
}
|
||||
if ($key !== null) {
|
||||
return $data[$key] ?? null;
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
/**
|
||||
* @param $id
|
||||
* @param null $key
|
||||
* @return mixed
|
||||
*/
|
||||
private static function loadByStatic($id, $key = null): mixed
|
||||
{
|
||||
$data = static::$_contents[$id] ?? null;
|
||||
if ($data === null) {
|
||||
return null;
|
||||
}
|
||||
if ($key !== null) {
|
||||
return $data[$key] ?? null;
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public static function getAllContext(): mixed
|
||||
{
|
||||
if (static::inCoroutine()) {
|
||||
return Coroutine::getContext() ?? [];
|
||||
} else {
|
||||
return static::$_contents ?? [];
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public static function getAllContext(): mixed
|
||||
{
|
||||
if (static::inCoroutine()) {
|
||||
return Coroutine::getContext() ?? [];
|
||||
} else {
|
||||
return static::$_contents ?? [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $id
|
||||
* @param string|null $key
|
||||
*/
|
||||
public static function remove(string $id, string $key = null)
|
||||
{
|
||||
if (!static::hasContext($id, $key)) {
|
||||
return;
|
||||
}
|
||||
if (static::inCoroutine()) {
|
||||
unset(static::$_contents[$id]);
|
||||
return;
|
||||
}
|
||||
if (!empty($key)) {
|
||||
unset(Coroutine::getContext()[$id][$key]);
|
||||
} else {
|
||||
unset(Coroutine::getContext()[$id]);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @param string $id
|
||||
* @param string|null $key
|
||||
*/
|
||||
public static function remove(string $id, string $key = null)
|
||||
{
|
||||
if (!static::hasContext($id, $key)) {
|
||||
return;
|
||||
}
|
||||
if (!static::inCoroutine()) {
|
||||
if (!empty($key)) {
|
||||
unset(static::$_contents[$id][$key]);
|
||||
} else {
|
||||
unset(static::$_contents[$id]);
|
||||
}
|
||||
} else {
|
||||
if (!empty($key)) {
|
||||
unset(Coroutine::getContext()[$id][$key]);
|
||||
} else {
|
||||
unset(Coroutine::getContext()[$id]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @param null $key
|
||||
* @return bool
|
||||
*/
|
||||
public static function hasContext($id, $key = null): bool
|
||||
{
|
||||
if (!static::inCoroutine()) {
|
||||
return static::searchByStatic($id, $key);
|
||||
} else {
|
||||
return static::searchByCoroutine($id, $key);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @param $id
|
||||
* @param null $key
|
||||
* @return bool
|
||||
*/
|
||||
public static function hasContext($id, $key = null): bool
|
||||
{
|
||||
if (!static::inCoroutine()) {
|
||||
return static::searchByStatic($id, $key);
|
||||
}
|
||||
return static::searchByCoroutine($id, $key);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @param null $key
|
||||
* @return bool
|
||||
*/
|
||||
private static function searchByStatic($id, $key = null): bool
|
||||
{
|
||||
if (!isset(static::$_contents[$id])) {
|
||||
return false;
|
||||
}
|
||||
if (!empty($key) && !isset(static::$_contents[$id][$key])) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* @param $id
|
||||
* @param null $key
|
||||
* @return bool
|
||||
*/
|
||||
private static function searchByStatic($id, $key = null): bool
|
||||
{
|
||||
if (!isset(static::$_contents[$id])) {
|
||||
return false;
|
||||
}
|
||||
if (!empty($key) && !isset(static::$_contents[$id][$key])) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @param null $key
|
||||
* @return bool
|
||||
*/
|
||||
private static function searchByCoroutine($id, $key = null): bool
|
||||
{
|
||||
if (!isset(Coroutine::getContext()[$id])) {
|
||||
return false;
|
||||
}
|
||||
if ($key !== null) {
|
||||
return isset((Coroutine::getContext()[$id] ?? [])[$key]);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* @param $id
|
||||
* @param null $key
|
||||
* @return bool
|
||||
*/
|
||||
private static function searchByCoroutine($id, $key = null): bool
|
||||
{
|
||||
if (!isset(Coroutine::getContext()[$id])) {
|
||||
return false;
|
||||
}
|
||||
if ($key !== null) {
|
||||
return isset((Coroutine::getContext()[$id] ?? [])[$key]);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public static function inCoroutine(): bool
|
||||
{
|
||||
return Coroutine::getCid() > 0;
|
||||
}
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public static function inCoroutine(): bool
|
||||
{
|
||||
return Coroutine::getCid() !== -1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -115,7 +115,11 @@ class Server extends HttpService
|
||||
|
||||
Runtime::enableCoroutine(true, SWOOLE_HOOK_ALL ^ SWOOLE_HOOK_BLOCKING_FUNCTION);
|
||||
|
||||
Coroutine::set(['enable_deadlock_check' => false]);
|
||||
$settings['enable_deadlock_check'] = false;
|
||||
$settings['exit_condition'] = function () {
|
||||
return Coroutine::stats()['coroutine_num'] === 0;
|
||||
};
|
||||
Coroutine::set($settings);
|
||||
|
||||
return $this->execute($baseServer);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user