Skip to content

Commit 53c32c0

Browse files
authored
Merge pull request #1 from Reasno/0.1.1
0.1.1
2 parents 371ed8d + 30ac19c commit 53c32c0

File tree

9 files changed

+72
-50
lines changed

9 files changed

+72
-50
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"hyperf/config": "^1.1",
3030
"hyperf/di": "^1.1",
3131
"hyperf/testing": "1.1.*",
32-
"phpstan/phpstan": "^0.10.5",
32+
"phpstan/phpstan": "^0.12",
3333
"swoft/swoole-ide-helper": "dev-master"
3434
},
3535
"config": {

example/Server.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"github.com/reasno/gotask/pkg/gotask"
88
"io/ioutil"
9+
"log"
910
)
1011

1112
// App sample
@@ -49,5 +50,7 @@ func (a *App) HelloError(name interface{}, r *interface{}) error {
4950

5051
func main() {
5152
gotask.Register(new(App))
52-
gotask.Run()
53+
if err := gotask.Run(); err != nil {
54+
log.Fatal(err)
55+
}
5356
}

pkg/gotask/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ func GetAddress() string {
5656

5757
func Run() error {
5858
var g run.Group
59-
{
59+
if *address == "" {
6060
relay := goridge.NewPipeRelay(os.Stdin, os.Stdout)
6161
codec := goridge.NewCodecWithRelay(relay)
6262
g.Add(func() error {
6363
rpc.ServeCodec(codec)
64-
return nil
64+
return fmt.Errorf("pipe is closed")
6565
}, func(err error) {
6666
_ = os.Stdin.Close()
6767
_ = os.Stdout.Close()

publish/gotask.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
}),
2121
'pool' => [
2222
'min_connections' => 1,
23-
'max_connections' => 100,
23+
'max_connections' => 30,
2424
'connect_timeout' => 10.0,
25-
'wait_timeout' => 3.0,
25+
'wait_timeout' => 30.0,
2626
'heartbeat' => -1,
2727
'max_idle_time' => (float) env('GOTASK_MAX_IDLE_TIME', 60),
2828
],

src/LocalGoTask.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ public function call(string $method, $payload, int $flags = 0){
3434
}
3535
$returnChannel = new Channel(1);
3636
$this->taskChannel->push([$method, $payload, $flags, $returnChannel]);
37-
return $returnChannel->pop();
37+
$result = $returnChannel->pop();
38+
if ($result instanceof \Throwable){
39+
throw $result;
40+
}
41+
return $result;
3842
}
3943

4044
private function start()
@@ -47,8 +51,12 @@ private function start()
4751
);
4852
while (true) {
4953
[$method, $payload, $flag, $returnChannel] = $this->taskChannel->pop();
50-
$result = $task->call($method, $payload, $flag);
51-
$returnChannel->push($result);
54+
try{
55+
$result = $task->call($method, $payload, $flag);
56+
$returnChannel->push($result);
57+
} catch (\Throwable $e){
58+
$returnChannel->push($e);
59+
}
5260
}
5361
}
5462
}

src/Relay/CoroutineSocketRelay.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
use Spiral\Goridge\Exceptions\GoridgeException;
1616
use Spiral\Goridge\Exceptions\InvalidArgumentException;
17+
use Spiral\Goridge\Exceptions\RelayException;
1718
use Swoole\Coroutine\Socket;
1819

1920
/**
@@ -87,6 +88,15 @@ public function __construct(string $address, int $port = null, int $type = self:
8788
$this->type = $type;
8889
}
8990

91+
public function __toString(): string
92+
{
93+
if ($this->type == self::SOCK_TCP) {
94+
return "tcp://{$this->address}:{$this->port}";
95+
}
96+
97+
return "unix://{$this->address}";
98+
}
99+
90100
/**
91101
* @return string
92102
*/
@@ -111,6 +121,33 @@ public function getType(): int
111121
return $this->type;
112122
}
113123

124+
125+
/**
126+
* Ensure socket connection. Returns true if socket successfully connected
127+
* or have already been connected.
128+
*
129+
* @throws RelayException
130+
* @throws \Error when sockets are used in unsupported environment
131+
*/
132+
public function connect(): bool
133+
{
134+
if ($this->isConnected()) {
135+
return true;
136+
}
137+
138+
$this->socket = $this->createSocket();
139+
try {
140+
// Port type needs to be int, so we convert null to 0
141+
if ($this->socket->connect($this->address, $this->port ?? 0) === false) {
142+
throw new RelayException(sprintf('%s (%s)', $this->socket->errMsg, $this->socket->errCode));
143+
}
144+
} catch (\Exception $e) {
145+
throw new RelayException("unable to establish connection {$this}", 0, $e);
146+
}
147+
148+
return true;
149+
}
150+
114151
/**
115152
* @throws GoridgeException
116153
* @return Socket

src/Relay/SocketTransporter.php

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,6 @@ public function __destruct()
2020
}
2121
}
2222

23-
public function __toString(): string
24-
{
25-
if ($this->type == self::SOCK_TCP) {
26-
return "tcp://{$this->address}:{$this->port}";
27-
}
28-
29-
return "unix://{$this->address}";
30-
}
31-
3223
/**
3324
* {@inheritdoc}
3425
*/
@@ -82,32 +73,6 @@ public function isConnected(): bool
8273
return $this->socket != null;
8374
}
8475

85-
/**
86-
* Ensure socket connection. Returns true if socket successfully connected
87-
* or have already been connected.
88-
*
89-
* @throws RelayException
90-
* @throws \Error when sockets are used in unsupported environment
91-
*/
92-
public function connect(): bool
93-
{
94-
if ($this->isConnected()) {
95-
return true;
96-
}
97-
98-
$this->socket = $this->createSocket();
99-
try {
100-
// Port type needs to be int, so we convert null to 0
101-
if ($this->socket->connect($this->address, $this->port ?? 0) === false) {
102-
throw new RelayException(sprintf('%s (%s)', $this->socket->errMsg, $this->socket->errCode));
103-
}
104-
} catch (\Exception $e) {
105-
throw new RelayException("unable to establish connection {$this}", 0, $e);
106-
}
107-
108-
return true;
109-
}
110-
11176
/**
11277
* Close connection.
11378
*

tests/Cases/CoroutineSocketTest.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,28 @@
3636
class CoroutineSocketTest extends AbstractTestCase
3737
{
3838
const UNIX_SOCKET = '/tmp/test.sock';
39-
4039
/**
41-
* @var RPC
40+
* @var Process
4241
*/
43-
private $task;
44-
42+
private $p;
4543

4644
public function setUp()
4745
{
4846
! defined('BASE_PATH') && define('BASE_PATH', dirname(__DIR__, 1));
4947
@unlink(self::UNIX_SOCKET);
50-
$p = new Process(function (Process $process) {
48+
$this->p = new Process(function (Process $process) {
5149
$process->exec(__DIR__ . '/../../app', ['-address', self::UNIX_SOCKET]);
5250
});
53-
$p->start();
51+
$this->p->start();
5452
sleep(1);
5553
}
5654

55+
public function tearDown()
56+
{
57+
Process::kill($this->p->pid);
58+
}
59+
60+
5761
public function testOnCoroutine()
5862
{
5963
\Swoole\Coroutine\run(function () {

tests/Cases/IPCRelayTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ public function setUp()
2929
sleep(1);
3030
}
3131

32+
public function tearDown()
33+
{
34+
Process::kill($this->p->pid);
35+
}
36+
3237
public function testOnCoroutine()
3338
{
3439
\Swoole\Coroutine\run(function () {

0 commit comments

Comments
 (0)