This commit is contained in:
2020-09-17 14:12:14 +08:00
parent 28669399b5
commit 57db5fe495
4 changed files with 370 additions and 375 deletions
+6 -1
View File
@@ -10,6 +10,7 @@ namespace HttpServer\Http;
use Exception; use Exception;
use HttpServer\Exception\RequestException; use HttpServer\Exception\RequestException;
use Snowflake\Core\Help;
use Snowflake\Snowflake; use Snowflake\Snowflake;
/** /**
@@ -39,6 +40,10 @@ class HttpParams
$this->body = $body; $this->body = $body;
$this->gets = $get ?? []; $this->gets = $get ?? [];
$this->files = $files ?? []; $this->files = $files ?? [];
if (!is_array($this->body)) {
$this->body = Help::toArray($this->body);
}
} }
/** /**
@@ -53,7 +58,7 @@ class HttpParams
* @param array $data * @param array $data
* 批量添加数据 * 批量添加数据
*/ */
public function setPosts($data) public function setPosts(array $data)
{ {
if (!is_array($data)) { if (!is_array($data)) {
return; return;
+1 -1
View File
@@ -424,7 +424,7 @@ class Request extends Application
$sRequest->startTime = microtime(true); $sRequest->startTime = microtime(true);
$sRequest->uri = $request->server['request_uri'] ?? $request->header['request_uri']; $sRequest->uri = $request->server['request_uri'] ?? $request->header['request_uri'];
$sRequest->params = Snowflake::createObject(HttpParams::class,[Help::toArray($request->rawContent()), $request->get, $request->files]); $sRequest->params = new HttpParams($request->rawContent(), $request->get, $request->files);
if (!empty($request->post)) { if (!empty($request->post)) {
$sRequest->params->setPosts($request->post ?? []); $sRequest->params->setPosts($request->post ?? []);
} }
+9 -9
View File
@@ -105,7 +105,7 @@ class Connection extends Pool
*/ */
public function inTransaction($cds) public function inTransaction($cds)
{ {
[$coroutineId, $coroutineName] = $this->getIndex($cds, true); $coroutineName = $this->name($cds, true);
if (!Context::hasContext('begin_' . $coroutineName)) { if (!Context::hasContext('begin_' . $coroutineName)) {
return false; return false;
} }
@@ -117,7 +117,7 @@ class Connection extends Pool
*/ */
public function beginTransaction($coroutineName) public function beginTransaction($coroutineName)
{ {
[$coroutineId, $coroutineName] = $this->getIndex($coroutineName, true); $coroutineName = $this->name($coroutineName, true);
if (!Context::hasContext('begin_' . $coroutineName)) { if (!Context::hasContext('begin_' . $coroutineName)) {
Context::setContext('begin_' . $coroutineName, 0); Context::setContext('begin_' . $coroutineName, 0);
} }
@@ -136,7 +136,7 @@ class Connection extends Pool
*/ */
public function commit($coroutineName) public function commit($coroutineName)
{ {
[$coroutineId, $coroutineName] = $this->getIndex($coroutineName, true); $coroutineName = $this->name($coroutineName, true);
if (!Context::hasContext('begin_' . $coroutineName)) { if (!Context::hasContext('begin_' . $coroutineName)) {
return; return;
} }
@@ -170,7 +170,7 @@ class Connection extends Pool
*/ */
public function rollback($coroutineName) public function rollback($coroutineName)
{ {
[$coroutineId, $coroutineName] = $this->getIndex($coroutineName, true); $coroutineName = $this->name($coroutineName, true);
if (!Context::hasContext('begin_' . $coroutineName)) { if (!Context::hasContext('begin_' . $coroutineName)) {
return; return;
} }
@@ -200,7 +200,7 @@ class Connection extends Pool
if ($this->creates === 0) { if ($this->creates === 0) {
$this->creates = Timer::tick(10000, [$this, 'Heartbeat_detection']); $this->creates = Timer::tick(10000, [$this, 'Heartbeat_detection']);
} }
[$coroutineId, $coroutineName] = $this->getIndex($config['cds'], $isMaster); $coroutineName = $this->name($config['cds'], $isMaster);
if (!isset($this->hasCreate[$coroutineName])) { if (!isset($this->hasCreate[$coroutineName])) {
$this->hasCreate[$coroutineName] = 0; $this->hasCreate[$coroutineName] = 0;
} }
@@ -210,9 +210,9 @@ class Connection extends Pool
if ($this->size($coroutineName) < 1 && $this->hasCreate[$coroutineName] < $this->max) { if ($this->size($coroutineName) < 1 && $this->hasCreate[$coroutineName] < $this->max) {
return $this->saveClient($coroutineName, $this->nowClient($coroutineName, $config)); return $this->saveClient($coroutineName, $this->nowClient($coroutineName, $config));
} }
[$timeout, $connection] = $client = $this->get($coroutineName); $connections = $client = $this->get($coroutineName);
if ($connection instanceof PDO) { if ($connections[1] instanceof PDO) {
return $this->saveClient($coroutineName, $connection); return $this->saveClient($coroutineName, $connections[1]);
} }
return $this->saveClient($coroutineName, $this->nowClient($coroutineName, $config)); return $this->saveClient($coroutineName, $this->nowClient($coroutineName, $config));
} }
@@ -252,7 +252,7 @@ class Connection extends Pool
*/ */
public function release($coroutineName, $isMaster) public function release($coroutineName, $isMaster)
{ {
[$coroutineId, $coroutineName] = $this->getIndex($coroutineName, $isMaster); $coroutineName = $this->name($coroutineName, $isMaster);
if (!$this->hasClient($coroutineName)) { if (!$this->hasClient($coroutineName)) {
return; return;
} }
+6 -16
View File
@@ -36,7 +36,7 @@ class Redis extends Pool
public function getConnection(array $config, $isMaster = false) public function getConnection(array $config, $isMaster = false)
{ {
$name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases']; $name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases'];
[$coroutineId, $coroutineName] = $this->getIndex('redis:' . $name, $isMaster); $coroutineName = $this->name('redis:' . $name, $isMaster);
if (Context::hasContext($coroutineName)) { if (Context::hasContext($coroutineName)) {
return Context::getContext($coroutineName); return Context::getContext($coroutineName);
} else if (!$this->hasItem($coroutineName)) { } else if (!$this->hasItem($coroutineName)) {
@@ -59,11 +59,11 @@ class Redis extends Pool
$this->success('create redis client -> ' . $config['host'] . ':' . $this->size($coroutineName)); $this->success('create redis client -> ' . $config['host'] . ':' . $this->size($coroutineName));
return $this->saveClient($coroutineName, $this->createConnect($config)); return $this->saveClient($coroutineName, $this->createConnect($config));
} }
[$time, $client] = $this->get($coroutineName); $clients = $this->get($coroutineName);
if ($client === null) { if ($clients[1] === null) {
return $this->getByChannel($coroutineName, $config); return $this->getByChannel($coroutineName, $config);
} }
return $this->saveClient($coroutineName, $client); return $this->saveClient($coroutineName, $clients[1]);
} }
@@ -109,7 +109,7 @@ class Redis extends Pool
public function release(array $config, $isMaster = false) public function release(array $config, $isMaster = false)
{ {
$name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases']; $name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases'];
[$coroutineId, $coroutineName] = $this->getIndex('redis:' . $name, $isMaster); $coroutineName = $this->name('redis:' . $name, $isMaster);
if (!Context::hasContext($coroutineName)) { if (!Context::hasContext($coroutineName)) {
return; return;
} }
@@ -127,7 +127,7 @@ class Redis extends Pool
public function destroy(array $config, $isMaster = false) public function destroy(array $config, $isMaster = false)
{ {
$name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases']; $name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases'];
[$coroutineId, $coroutineName] = $this->getIndex('redis:' . $name, $isMaster); $coroutineName = $this->name('redis:' . $name, $isMaster);
if (!Context::hasContext($coroutineName)) { if (!Context::hasContext($coroutineName)) {
return; return;
} }
@@ -177,15 +177,5 @@ class Redis extends Pool
// TODO: Implement desc() method. // TODO: Implement desc() method.
} }
/**
* @param $name
* @param false $isMaster
* @return array
*/
private function getIndex($name, $isMaster = false)
{
return [Coroutine::getCid(), $this->name($name, $isMaster)];
}
} }