This commit is contained in:
2020-09-17 13:56:57 +08:00
parent 06ba4acd51
commit 6f8e024fae
11 changed files with 77 additions and 64 deletions
+12
View File
@@ -101,6 +101,18 @@ class Connection extends Component
return $this->getPdo($sql); return $this->getPdo($sql);
} }
/**
* 初始化 Channel
*/
public function fill()
{
$connections = Snowflake::app()->connections;
$connections->initConnections($this->cds, true, $this->maxNumber);
$connections->initConnections($this->slaveConfig['cds'], false, $this->maxNumber);
}
/** /**
* @param $sql * @param $sql
* @return PDO * @return PDO
+28
View File
@@ -55,6 +55,34 @@ class DatabasesProviders extends Providers
} }
/**
* @throws ConfigException
* @throws Exception
*/
public function createPool()
{
$databases = Config::get('databases', false, []);
if (empty($databases)) {
return;
}
$application = Snowflake::app();
foreach ($databases as $name => $database) {
/** @var Connection $connection */
$connection = $application->set('databases.' . $name, [
'class' => Connection::class,
'id' => $database['id'],
'cds' => $database['cds'],
'username' => $database['username'],
'password' => $database['password'],
'tablePrefix' => $database['tablePrefix'],
'maxNumber' => $database['maxNumber'],
'slaveConfig' => $database['slaveConfig']
]);
$connection->fill();
}
}
/** /**
* @param $name * @param $name
* @return array|mixed|null * @return array|mixed|null
+23 -29
View File
@@ -66,14 +66,13 @@ class Context extends BaseContext
} }
if (!empty($key)) { if (!empty($key)) {
if (!is_array(Coroutine::getContext()[$id])) { if (!is_array(Coroutine::getContext()[$id])) {
Coroutine::getContext()[$id] = [$key => $context]; return Coroutine::getContext()[$id] = [$key => $context];
} else { } else {
Coroutine::getContext()[$id][$key] = $context; return Coroutine::getContext()[$id][$key] = $context;
} }
} else { } else {
Coroutine::getContext()[$id] = $context; return Coroutine::getContext()[$id] = $context;
} }
return $context;
} }
/** /**
@@ -102,8 +101,7 @@ class Context extends BaseContext
if (!static::inCoroutine() || !static::hasContext($id)) { if (!static::inCoroutine() || !static::hasContext($id)) {
return false; return false;
} }
$context = Coroutine::getContext()[$id]; if (!isset(Coroutine::getContext()[$id][$key])) {
if (!isset($context[$key])) {
return false; return false;
} }
return Coroutine::getContext()[$id][$key] -= 1; return Coroutine::getContext()[$id][$key] -= 1;
@@ -120,14 +118,18 @@ class Context extends BaseContext
return null; return null;
} }
if (static::inCoroutine()) { if (static::inCoroutine()) {
$array = Coroutine::getContext()[$id]; if ($key === null) {
return Coroutine::getContext()[$id];
} else {
return Coroutine::getContext()[$id][$key] ?? null;
}
} else { } else {
$array = static::$_requests[$id]; if ($key === null) {
return static::$_requests[$id];
} else {
return static::$_requests[$id][$key] ?? null;
}
} }
if (empty($key) || !is_array($array)) {
return $array;
}
return $array[$key];
} }
/** /**
@@ -169,27 +171,19 @@ class Context extends BaseContext
*/ */
public static function hasContext($id, $key = null) public static function hasContext($id, $key = null)
{ {
if (static::inCoroutine()) { if (!static::inCoroutine()) {
$context = Coroutine::getContext();
if (!isset($context[$id])) {
return false;
}
$data = $context[$id];
} else {
if (!isset(static::$_requests[$id])) {
return false;
}
$data = static::$_requests[$id];
}
if (empty($data)) {
return false; return false;
} }
if (empty($key)) { if (!isset(Coroutine::getContext()[$id])) {
return true;
} else if (!is_array($data)) {
return false; return false;
} }
return isset($data[$key]); if (Coroutine::getContext()[$id] !== null) {
if ($key === null) {
return true;
}
return isset(Coroutine::getContext()[$id][$key]);
}
return false;
} }
+1 -2
View File
@@ -321,8 +321,7 @@ class Request extends Application
*/ */
public function getMethod() public function getMethod()
{ {
$head = $this->headers->getHeader('request_method'); return strtolower($this->headers->getHeader('request_method'));
return strtolower($head);
} }
/** /**
+1
View File
@@ -189,6 +189,7 @@ class Response extends Application
} }
$this->response->end($sendData); $this->response->end($sendData);
$this->response = null; $this->response = null;
unset($this->response);
return $sendData; return $sendData;
} }
+7 -5
View File
@@ -54,9 +54,11 @@ class Config extends Component
*/ */
public static function get($key, $try = FALSE, $default = null) public static function get($key, $try = FALSE, $default = null)
{ {
$explode = explode('.', $key);
$instance = Snowflake::app()->config->getData(); $instance = Snowflake::app()->config->getData();
foreach ($explode as $index => $value) { if (strpos($key, '.') === false) {
return isset($instance[$key]) ? $instance[$key] : $default;
}
foreach (explode('.', $key) as $index => $value) {
if (empty($value)) { if (empty($value)) {
continue; continue;
} }
@@ -66,10 +68,10 @@ class Config extends Component
} }
return $default; return $default;
} }
$instance = $instance[$value]; if (!is_array($instance[$value]) ) {
if (!is_array($instance) && $index + 1 < count($explode)) { return $instance[$value];
throw new ConfigException(sprintf(self::ERROR_MESSAGE, $key));
} }
$instance = $instance[$value];
} }
return empty($instance) ? $default : $instance; return empty($instance) ? $default : $instance;
} }
+1 -1
View File
@@ -58,7 +58,7 @@ abstract class Pool extends Component
*/ */
public function name($cds, $isMaster = false) public function name($cds, $isMaster = false)
{ {
return hash('sha1', $cds . ($isMaster ? 'master' : 'slave')); return md5($cds . ($isMaster ? 'master' : 'slave'));
} }
-11
View File
@@ -38,17 +38,6 @@ class Application extends BaseApplication
public $id = 'uniqueId'; public $id = 'uniqueId';
/**
* Application constructor.
* @param array $config
*/
public function __construct(array $config = [])
{
// instance_load();
parent::__construct($config);
}
/** /**
* @throws ConfigException * @throws ConfigException
* @throws NotFindClassException * @throws NotFindClassException
+2 -11
View File
@@ -366,24 +366,15 @@ SCRIPT;
*/ */
public function get_config(): array public function get_config(): array
{ {
$config = Config::get('cache.redis', false, [ return Config::get('cache.redis', false, [
'host' => '127.0.0.1', 'host' => '127.0.0.1',
'port' => '6379', 'port' => '6379',
'prefix' => Config::get('id'), 'prefix' => Config::get('id'),
'auth' => '', 'auth' => '',
'databases' => '0', 'databases' => '0',
'read_timeout' => -1, 'read_timeout' => -1,
'conn_timeout' => -1, 'timeout' => -1,
]); ]);
return [
'host' => $config['host'],
'port' => $config['port'],
'auth' => $config['auth'],
'timeout' => $config['conn_timeout'],
'databases' => $config['databases'],
'read_timeout' => $config['read_timeout'],
'prefix' => $config['prefix'],
];
} }
} }
+1 -4
View File
@@ -131,14 +131,11 @@ class Container extends BaseObject
if (empty($config)) { if (empty($config)) {
return $reflect->newInstanceArgs($dependencies ?? []); return $reflect->newInstanceArgs($dependencies ?? []);
} }
if (!empty($dependencies) && $reflect->implementsInterface('Snowflake\Abstracts\Configure')) { if (!empty($dependencies) && $reflect->implementsInterface('Snowflake\Abstracts\Configure')) {
$dependencies[count($dependencies) - 1] = $config; $dependencies[count($dependencies) - 1] = $config;
return $reflect->newInstanceArgs($dependencies); return $reflect->newInstanceArgs($dependencies);
} }
if (!empty($config)) { $this->_param[$class] = $config;
$this->_param[$class] = $config;
}
$object = $reflect->newInstanceArgs($dependencies ?? []); $object = $reflect->newInstanceArgs($dependencies ?? []);
foreach ($config as $key => $val) { foreach ($config as $key => $val) {
$object->{$key} = $val; $object->{$key} = $val;
+1 -1
View File
@@ -97,7 +97,7 @@ class Redis extends Pool
$config['read_timeout'] = 10; $config['read_timeout'] = 10;
} }
$redis->select($config['databases']); $redis->select($config['databases']);
$redis->setOption(SRedis::OPT_READ_TIMEOUT, -1); $redis->setOption(SRedis::OPT_READ_TIMEOUT, $config['read_timeout']);
$redis->setOption(SRedis::OPT_PREFIX, $config['prefix']); $redis->setOption(SRedis::OPT_PREFIX, $config['prefix']);
return $redis; return $redis;
} }