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);
}
/**
* 初始化 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
* @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
* @return array|mixed|null
+20 -26
View File
@@ -66,14 +66,13 @@ class Context extends BaseContext
}
if (!empty($key)) {
if (!is_array(Coroutine::getContext()[$id])) {
Coroutine::getContext()[$id] = [$key => $context];
return Coroutine::getContext()[$id] = [$key => $context];
} else {
Coroutine::getContext()[$id][$key] = $context;
return Coroutine::getContext()[$id][$key] = $context;
}
} 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)) {
return false;
}
$context = Coroutine::getContext()[$id];
if (!isset($context[$key])) {
if (!isset(Coroutine::getContext()[$id][$key])) {
return false;
}
return Coroutine::getContext()[$id][$key] -= 1;
@@ -120,14 +118,18 @@ class Context extends BaseContext
return null;
}
if (static::inCoroutine()) {
$array = Coroutine::getContext()[$id];
if ($key === null) {
return Coroutine::getContext()[$id];
} else {
$array = static::$_requests[$id];
return Coroutine::getContext()[$id][$key] ?? null;
}
} else {
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)
{
if (static::inCoroutine()) {
$context = Coroutine::getContext();
if (!isset($context[$id])) {
if (!static::inCoroutine()) {
return false;
}
$data = $context[$id];
} else {
if (!isset(static::$_requests[$id])) {
if (!isset(Coroutine::getContext()[$id])) {
return false;
}
$data = static::$_requests[$id];
}
if (empty($data)) {
return false;
}
if (empty($key)) {
if (Coroutine::getContext()[$id] !== null) {
if ($key === null) {
return true;
} else if (!is_array($data)) {
return false;
}
return isset($data[$key]);
return isset(Coroutine::getContext()[$id][$key]);
}
return false;
}
+1 -2
View File
@@ -321,8 +321,7 @@ class Request extends Application
*/
public function getMethod()
{
$head = $this->headers->getHeader('request_method');
return strtolower($head);
return strtolower($this->headers->getHeader('request_method'));
}
/**
+1
View File
@@ -189,6 +189,7 @@ class Response extends Application
}
$this->response->end($sendData);
$this->response = null;
unset($this->response);
return $sendData;
}
+7 -5
View File
@@ -54,9 +54,11 @@ class Config extends Component
*/
public static function get($key, $try = FALSE, $default = null)
{
$explode = explode('.', $key);
$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)) {
continue;
}
@@ -66,10 +68,10 @@ class Config extends Component
}
return $default;
}
$instance = $instance[$value];
if (!is_array($instance) && $index + 1 < count($explode)) {
throw new ConfigException(sprintf(self::ERROR_MESSAGE, $key));
if (!is_array($instance[$value]) ) {
return $instance[$value];
}
$instance = $instance[$value];
}
return empty($instance) ? $default : $instance;
}
+1 -1
View File
@@ -58,7 +58,7 @@ abstract class Pool extends Component
*/
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';
/**
* Application constructor.
* @param array $config
*/
public function __construct(array $config = [])
{
// instance_load();
parent::__construct($config);
}
/**
* @throws ConfigException
* @throws NotFindClassException
+2 -11
View File
@@ -366,24 +366,15 @@ SCRIPT;
*/
public function get_config(): array
{
$config = Config::get('cache.redis', false, [
return Config::get('cache.redis', false, [
'host' => '127.0.0.1',
'port' => '6379',
'prefix' => Config::get('id'),
'auth' => '',
'databases' => '0',
'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'],
];
}
}
-3
View File
@@ -131,14 +131,11 @@ class Container extends BaseObject
if (empty($config)) {
return $reflect->newInstanceArgs($dependencies ?? []);
}
if (!empty($dependencies) && $reflect->implementsInterface('Snowflake\Abstracts\Configure')) {
$dependencies[count($dependencies) - 1] = $config;
return $reflect->newInstanceArgs($dependencies);
}
if (!empty($config)) {
$this->_param[$class] = $config;
}
$object = $reflect->newInstanceArgs($dependencies ?? []);
foreach ($config as $key => $val) {
$object->{$key} = $val;
+1 -1
View File
@@ -97,7 +97,7 @@ class Redis extends Pool
$config['read_timeout'] = 10;
}
$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']);
return $redis;
}