Skip to content

Commit 17331f1

Browse files
committed
Merge branch 'feature' of https://github.com/swoft-cloud/swoft into dev2
2 parents 8085701 + b7a08c6 commit 17331f1

25 files changed

+678
-137
lines changed

app/Common/RpcProvider.php

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php declare(strict_types=1);
2+
3+
4+
namespace App\Common;
5+
6+
7+
use ReflectionException;
8+
use Swoft\Bean\Annotation\Mapping\Bean;
9+
use Swoft\Bean\Annotation\Mapping\Inject;
10+
use Swoft\Bean\Exception\ContainerException;
11+
use Swoft\Consul\Agent;
12+
use Swoft\Consul\Exception\ClientException;
13+
use Swoft\Consul\Exception\ServerException;
14+
use Swoft\Rpc\Client\Client;
15+
use Swoft\Rpc\Client\Contract\ProviderInterface;
16+
17+
/**
18+
* Class RpcProvider
19+
*
20+
* @since 2.0
21+
*
22+
* @Bean()
23+
*/
24+
class RpcProvider implements ProviderInterface
25+
{
26+
/**
27+
* @Inject()
28+
*
29+
* @var Agent
30+
*/
31+
private $agent;
32+
33+
/**
34+
* @param Client $client
35+
*
36+
* @return array
37+
* @throws ReflectionException
38+
* @throws ContainerException
39+
* @throws ClientException
40+
* @throws ServerException
41+
* @example
42+
* [
43+
* 'host:port',
44+
* 'host:port',
45+
* 'host:port',
46+
* ]
47+
*/
48+
public function getList(Client $client): array
49+
{
50+
// Get health service from consul
51+
$services = $this->agent->services();
52+
53+
$services = [
54+
55+
];
56+
57+
return $services;
58+
}
59+
}

app/Console/Command/TestCommand.php

+22-9
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,18 @@ public function ab()
6262
private function uris(): array
6363
{
6464
return [
65-
'redis' => [
65+
'redis' => [
6666
'/redis/str',
6767
'/redis/et',
6868
'/redis/ep',
6969
'/redis/release',
7070
'/redis/poolSet',
7171
'/redis/set',
7272
],
73-
'log' => [
73+
'log' => [
7474
'/log/test'
7575
],
76-
'db' => [
76+
'db' => [
7777
'/dbTransaction/ts',
7878
'/dbTransaction/cm',
7979
'/dbTransaction/rl',
@@ -95,23 +95,36 @@ private function uris(): array
9595
'/selectDb/select',
9696
'/builder/schema'
9797
],
98-
'task' => [
98+
'task' => [
9999
'/task/getListByCo',
100100
'/task/deleteByCo',
101101
'/task/getListByAsync',
102102
'/task/deleteByAsync',
103+
'/task/returnNull',
104+
'/task/returnVoid',
103105
],
104-
'rpc' => [
106+
'rpc' => [
105107
'/rpc/getList',
106108
'/rpc/returnBool',
107109
'/rpc/bigString',
108-
'/rpc/sendBigString'
110+
'/rpc/sendBigString',
111+
'/rpc/returnNull'
109112
],
110-
'co' => [
113+
'co' => [
111114
'/co/multi'
112115
],
113-
'bean' => [
116+
'bean' => [
114117
'/bean/request'
118+
],
119+
'breaker' => [
120+
'/breaker/unbreak',
121+
'/breaker/breaked',
122+
'/breaker/loopBreaker'
123+
],
124+
'limiter' => [
125+
'/limiter/requestLimiter',
126+
'/limiter/requestLimiter2',
127+
'/limiter/paramLimiter?id=12',
115128
]
116129
];
117130
}
@@ -156,7 +169,7 @@ public function error(): void
156169
*/
157170
public function tcp(Input $input, Output $output): void
158171
{
159-
$cli = new Client(\SWOOLE_SOCK_TCP);
172+
$cli = new Client(\SWOOLE_SOCK_TCP);
160173
$host = $input->getSameOpt(['host', 'H'], '127.0.0.1');
161174
$port = $input->getSameOpt(['port', 'p'], 18309);
162175

app/Exception/Handler/HttpExceptionHandler.php

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Swoft\Error\Annotation\Mapping\ExceptionHandler;
1111
use Swoft\Http\Message\Response;
1212
use Swoft\Http\Server\Exception\Handler\AbstractHttpErrorHandler;
13+
use Swoft\Log\Helper\CLog;
1314
use Throwable;
1415

1516
/**
@@ -29,6 +30,9 @@ class HttpExceptionHandler extends AbstractHttpErrorHandler
2930
*/
3031
public function handle(Throwable $e, Response $response): Response
3132
{
33+
// Log
34+
CLog::error($e->getMessage());
35+
3236
// Debug is false
3337
if (!APP_DEBUG) {
3438
return $response->withStatus(500)->withContent(
+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php declare(strict_types=1);
2+
3+
4+
namespace App\Http\Controller;
5+
6+
use App\Model\Logic\BreakerLogic;
7+
use Exception;
8+
use Swoft\Bean\Annotation\Mapping\Inject;
9+
use Swoft\Http\Server\Annotation\Mapping\Controller;
10+
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
11+
12+
/**
13+
* Class BreakerController
14+
*
15+
* @since 2.0
16+
*
17+
* @Controller(prefix="breaker")
18+
*/
19+
class BreakerController
20+
{
21+
/**
22+
* @Inject()
23+
*
24+
* @var BreakerLogic
25+
*/
26+
private $logic;
27+
28+
/**
29+
* @RequestMapping()
30+
*
31+
* @return string
32+
* @throws Exception
33+
*/
34+
public function breaked(): string
35+
{
36+
return $this->logic->func();
37+
}
38+
39+
/**
40+
* @RequestMapping()
41+
*
42+
* @return array
43+
* @throws Exception
44+
*/
45+
public function unbreak(): array
46+
{
47+
return [$this->logic->func2()];
48+
}
49+
50+
/**
51+
* @RequestMapping()
52+
*
53+
* @return string
54+
* @throws Exception
55+
*/
56+
public function loopBraker(): string
57+
{
58+
return $this->logic->loop();
59+
}
60+
61+
/**
62+
* @RequestMapping()
63+
*
64+
* @return string
65+
* @throws Exception
66+
*/
67+
public function unFallback(): string
68+
{
69+
return $this->logic->unFallback();
70+
}
71+
}

app/Http/Controller/DbModelController.php

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace App\Http\Controller;
55

6+
use App\Model\Entity\Count;
67
use App\Model\Entity\User;
78
use Exception;
89
use Swoft\Http\Message\Response;
@@ -51,6 +52,10 @@ public function save(): array
5152

5253
$user->save();
5354

55+
$count = Count::new();
56+
$count->setUserId($user->getId());
57+
$count->save();
58+
5459
return $user->toArray();
5560
}
5661

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php declare(strict_types=1);
2+
3+
4+
namespace App\Http\Controller;
5+
6+
use Swoft\Http\Message\Request;
7+
use Swoft\Http\Server\Annotation\Mapping\Controller;
8+
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
9+
use Swoft\Limiter\Annotation\Mapping\RateLimiter;
10+
11+
/**
12+
* Class LimiterController
13+
*
14+
* @since 2.0
15+
*
16+
* @Controller(prefix="limiter")
17+
*/
18+
class LimiterController
19+
{
20+
/**
21+
* @RequestMapping()
22+
* @RateLimiter(key="request.getUriPath()")
23+
*
24+
* @param Request $request
25+
*
26+
* @return array
27+
*/
28+
public function requestLimiter(Request $request): array
29+
{
30+
$uri = $request->getUriPath();
31+
return ['requestLimiter', $uri];
32+
}
33+
34+
/**
35+
* @RequestMapping()
36+
* @RateLimiter(rate=20, fallback="limiterFallback")
37+
*
38+
* @param Request $request
39+
*
40+
* @return array
41+
*/
42+
public function requestLimiter2(Request $request): array
43+
{
44+
$uri = $request->getUriPath();
45+
return ['requestLimiter2', $uri];
46+
}
47+
48+
/**
49+
* @RequestMapping()
50+
* @RateLimiter(key="request.getUriPath()~':'~request.query('id')")
51+
*
52+
* @param Request $request
53+
*
54+
* @return array
55+
*/
56+
public function paramLimiter(Request $request): array
57+
{
58+
$id = $request->query('id');
59+
return ['paramLimiter', $id];
60+
}
61+
62+
/**
63+
* @param Request $request
64+
*
65+
* @return array
66+
*/
67+
public function limiterFallback(Request $request): array
68+
{
69+
$uri = $request->getUriPath();
70+
return ['limiterFallback', $uri];
71+
}
72+
}

app/Http/Controller/RpcController.php

+13
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use App\Rpc\Lib\UserInterface;
77
use Exception;
88
use Swoft\Co;
9+
use Swoft\Exception\SwoftException;
910
use Swoft\Http\Server\Annotation\Mapping\Controller;
1011
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
1112
use Swoft\Rpc\Client\Annotation\Mapping\Reference;
@@ -78,6 +79,7 @@ public function bigString(): array
7879
* @RequestMapping()
7980
*
8081
* @return array
82+
* @throws SwoftException
8183
*/
8284
public function sendBigString(): array
8385
{
@@ -88,6 +90,17 @@ public function sendBigString(): array
8890
return [$len, $result];
8991
}
9092

93+
/**
94+
* @RequestMapping()
95+
*
96+
* @return array
97+
*/
98+
public function returnNull(): array
99+
{
100+
$this->userService->returnNull();
101+
return [null];
102+
}
103+
91104
/**
92105
* @RequestMapping()
93106
*

app/Http/Controller/TaskController.php

+24
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,28 @@ public function deleteByAsync(): array
7171

7272
return [$data];
7373
}
74+
75+
/**
76+
* @RequestMapping()
77+
*
78+
* @return array
79+
* @throws TaskException
80+
*/
81+
public function returnNull(): array
82+
{
83+
$result = Task::co('testTask', 'returnNull', ['name']);
84+
return [$result];
85+
}
86+
87+
/**
88+
* @RequestMapping()
89+
*
90+
* @return array
91+
* @throws TaskException
92+
*/
93+
public function returnVoid(): array
94+
{
95+
$result = Task::co('testTask', 'returnVoid', ['name']);
96+
return [$result];
97+
}
7498
}

0 commit comments

Comments
 (0)