Skip to content

Commit

Permalink
优化ConnectContext和Group的Redis初始化判断逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft committed Aug 22, 2018
1 parent 0b69d92 commit 2a1127b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 40 deletions.
54 changes: 34 additions & 20 deletions src/Server/ConnectContext/StoreHandler/Redis.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?php
namespace Imi\Server\ConnectContext\StoreHandler;

use Imi\Worker;
use Imi\Log\Log;
use Imi\Util\Swoole;
use Imi\ServerManage;
use Imi\RequestContext;
use Imi\Pool\PoolManager;
use Imi\Bean\Annotation\Bean;
Expand Down Expand Up @@ -66,30 +69,41 @@ public function __init()
{
return;
}
$this->useRedis(function($resource, $redis){
// 判断master进程pid
$this->masterPID = Swoole::getMasterPID();
$hasPing = $this->hasPing($redis);
$storeMasterPID = $redis->get($this->key);
if(null === $storeMasterPID)
{
// 没有存储master进程pid
$this->initRedis($redis, $storeMasterPID);
}
else if($this->masterPID != $storeMasterPID)
{
if($hasPing)
if(0 === Worker::getWorkerID())
{
$this->useRedis(function($resource, $redis){
// 判断master进程pid
$this->masterPID = Swoole::getMasterPID();
$storeMasterPID = $redis->get($this->key);
if(null === $storeMasterPID)
{
// 与master进程ID不等
throw new \RuntimeException('ConnectContextRedis repeat');
// 没有存储master进程pid
$this->initRedis($redis, $storeMasterPID);
}
else
else if($this->masterPID != $storeMasterPID)
{
$this->initRedis($redis, $storeMasterPID);
$hasPing = $this->hasPing($redis);
if($hasPing)
{
Log::warning('ConnectContextRedis key has been used, waiting...');
sleep($this->heartbeatTtl);
$hasPing = $this->hasPing($redis);
}
if($hasPing)
{
// 与master进程ID不等
Log::emergency('ConnectContextRedis key has been used');
ServerManage::getServer('main')->getSwooleServer()->shutdown();
}
else
{
$this->initRedis($redis, $storeMasterPID);
Log::info('ConnectContextRedis key init');
}
}
}
$this->startPing($redis);
});
$this->startPing($redis);
});
}
}

/**
Expand Down
54 changes: 34 additions & 20 deletions src/Server/Group/Handler/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use Imi\Pool\PoolManager;
use Imi\Bean\Annotation\Bean;
use Swoole\Coroutine\Redis as CoRedis;
use Imi\Worker;
use Imi\Log\Log;
use Imi\ServerManage;

/**
* @Bean("GroupRedis")
Expand Down Expand Up @@ -74,30 +77,41 @@ public function __init()
{
return;
}
$this->useRedis(function($resource, $redis){
// 判断master进程pid
$this->masterPID = Swoole::getMasterPID();
$hasPing = $this->hasPing($redis);
$storeMasterPID = $redis->get($this->key);
if(null === $storeMasterPID)
{
// 没有存储master进程pid
$this->initRedis($redis, $storeMasterPID);
}
else if($this->masterPID != $storeMasterPID)
{
if($hasPing)
if(0 === Worker::getWorkerID())
{
$this->useRedis(function($resource, $redis){
// 判断master进程pid
$this->masterPID = Swoole::getMasterPID();
$storeMasterPID = $redis->get($this->key);
if(null === $storeMasterPID)
{
// 与master进程ID不等
throw new \RuntimeException('Server Group Redis repeat');
// 没有存储master进程pid
$this->initRedis($redis, $storeMasterPID);
}
else
else if($this->masterPID != $storeMasterPID)
{
$this->initRedis($redis, $storeMasterPID);
$hasPing = $this->hasPing($redis);
if($hasPing)
{
Log::warning('Redis server group key has been used, waiting...');
sleep($this->heartbeatTtl);
$hasPing = $this->hasPing($redis);
}
if($hasPing)
{
// 与master进程ID不等
Log::emergency('Redis server group key has been used');
ServerManage::getServer('main')->getSwooleServer()->shutdown();
}
else
{
$this->initRedis($redis, $storeMasterPID);
Log::info('Redis server group key init');
}
}
}
$this->startPing($redis);
});
$this->startPing($redis);
});
}
}

/**
Expand Down

0 comments on commit 2a1127b

Please sign in to comment.