41 lines
1.5 KiB
PHP
41 lines
1.5 KiB
PHP
<?php
|
|
namespace app\event;
|
|
use think\facade\Cache;
|
|
class WebUpstreamSocket
|
|
{
|
|
public function handle($frame = '')
|
|
{
|
|
if ($frame) {
|
|
$json = json_decode($frame->data, true);
|
|
if (isset($json['type']) && $json['type'] == 'upstream') {
|
|
$upstream_addres = $json['data']['upstream_addres'];
|
|
$weight = $json['data']['weight'] ?? config('swoole.upstream.weight', 10);
|
|
$url = config('domain.url') . '/dynamic?upstream=zone_for_backends';
|
|
if (strpos(http($url), $upstream_addres) == false) {
|
|
$this->addUpstream($upstream_addres, $weight);
|
|
}
|
|
$data = Cache::get('upstream_addres');
|
|
if ($data) {
|
|
$data[$upstream_addres] = ['fd' => $frame->fd, 'host' => $upstream_addres, 'tiemr' => time()];
|
|
Cache::tag('upstream_addres_data')->set('upstream_addres', $data);
|
|
} else {
|
|
$data[$upstream_addres] = ['fd' => $frame->fd, 'host' => $upstream_addres, 'tiemr' => time()];
|
|
Cache::tag('upstream_addres_data')->set('upstream_addres', $data);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/***
|
|
* 添加到负载
|
|
* @param $upstream
|
|
* @return void
|
|
* @throws \Exception
|
|
*/
|
|
public function addUpstream($upstream, $weight = 10)
|
|
{
|
|
$url = config('domain.url');
|
|
$urls = "{$url}/dynamic?upstream=zone_for_backends&add=&server={$upstream}&weight={$weight}";
|
|
http($urls);
|
|
}
|
|
} |