Skip to content

Commit 6c40d00

Browse files
committed
ng: new sync client interface
1 parent 05c431a commit 6c40d00

17 files changed

+563
-4
lines changed

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ sudo: required
55
language: php
66

77
php:
8-
- 7.0
9-
- 7.1
108
- 7.2
9+
- 7.3
1110
- nightly
1211

1312
cache:

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2015-2018 Jakub Kulhan <[email protected]>
1+
Copyright (c) 2015-2019 Jakub Kulhan <[email protected]>
22

33
Permission is hereby granted, free of charge, to any person
44
obtaining a copy of this software and associated documentation

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
}
2626
],
2727
"require": {
28-
"php": "~7.0",
28+
"php": "~7.2",
2929
"psr/log": "~1.0",
3030
"react/event-loop": "^1.0 || ^0.5 || ^0.4",
3131
"react/promise": "~2.2"

src/Bunny/NG/ClosableInterface.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
namespace Bunny\NG;
3+
4+
/**
5+
* Resource that must be explicitly closed.
6+
*
7+
* @author Jakub Kulhan <[email protected]>
8+
*/
9+
interface ClosableInterface
10+
{
11+
12+
/**
13+
* Closes the resource.
14+
*
15+
* After the resource is closed behavior of any of its method is undefined.
16+
*
17+
* If the resource is not closed by calling this method, it will trigger an error in its destructor.
18+
*
19+
* @return void
20+
*/
21+
public function close();
22+
23+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
namespace Bunny\NG;
3+
4+
/**
5+
* Channel on the connection to an AMQP 0.9.1 broker.
6+
*
7+
* @author Jakub Kulhan <[email protected]>
8+
*/
9+
interface RabbitChannelInterface extends ClosableInterface
10+
{
11+
// TODO: channel methods
12+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
namespace Bunny\NG;
3+
4+
/**
5+
* Client manages message publications and subscriptions.
6+
*
7+
* @author Jakub Kulhan <[email protected]>
8+
*/
9+
interface RabbitClientInterface
10+
{
11+
12+
/**
13+
* Publishes the message on the client-managed connection.
14+
*
15+
* If the client is not connected, a new connection will be opened.
16+
*
17+
* If the connection gets broken, a new one is tried to be created and message will be published on it.
18+
*
19+
* All publications use the same channel on the connection.
20+
*
21+
* @param RabbitMessage $message
22+
* @return void
23+
*/
24+
public function publish(RabbitMessage $message);
25+
26+
/**
27+
* Consume from one or more queues on the client-managed connection.
28+
*
29+
* If the client is not connected, a new connection will be opened.
30+
*
31+
* If the connection gets broken, an exception will be thrown.
32+
*
33+
* Every subscription gets a new channel. However, all consumers of the subscription share that channel.
34+
*
35+
* @return RabbitSubscriptionInterface
36+
*/
37+
public function subscribe(): RabbitSubscriptionInterface;
38+
39+
/**
40+
* Send heartbeat on the client-managed connection.
41+
*
42+
* If the client is not connected, a new connection will be opened.
43+
*
44+
* This method is supposed to be called either if the application tries to verify that the broker is available,
45+
* or it received a message on a subscription and it knows that the heartbeat timeout would be triggered. Otherwise,
46+
* e.g. when a subscription waits for new messages, heartbeats get sent automatically.
47+
*
48+
* @return void
49+
*/
50+
public function ping();
51+
52+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
namespace Bunny\NG;
3+
4+
/**
5+
* @author Jakub Kulhan <[email protected]>
6+
*/
7+
interface RabbitConnectionFactoryInterface
8+
{
9+
10+
/**
11+
* Opens a new connection with the factory connection options.
12+
*
13+
* Returned connection is not managed by the instance and must be explicitly closed by its `close()` method.
14+
*
15+
* @return RabbitConnectionInterface
16+
*/
17+
public function newConnection(): RabbitConnectionInterface;
18+
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
namespace Bunny\NG;
3+
4+
/**
5+
* Connection to an AMQP 0.9.1 broker.
6+
*
7+
* @author Jakub Kulhan <[email protected]>
8+
*/
9+
interface RabbitConnectionInterface extends ClosableInterface
10+
{
11+
12+
/**
13+
* Opens a new channel on the connection.
14+
*
15+
* @return RabbitChannelInterface
16+
*/
17+
public function newChannel(): RabbitChannelInterface;
18+
19+
}
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
<?php
2+
namespace Bunny\NG;
3+
4+
use Bunny\NG\Sasl\SaslMechanismInterface;
5+
6+
/**
7+
* @author Jakub Kulhan <[email protected]>
8+
*/
9+
final class RabbitConnectionOptions
10+
{
11+
12+
/**
13+
* Hostname or IP address of the server.
14+
*
15+
* @var string
16+
*/
17+
public $host;
18+
19+
/**
20+
* Server port.
21+
*
22+
* @var int
23+
*/
24+
public $port;
25+
26+
/**
27+
* Available SASL mechanisms.
28+
*
29+
* @var SaslMechanismInterface[]
30+
*/
31+
public $mechanisms;
32+
33+
/**
34+
* Server virtual host.
35+
*
36+
* @var string
37+
*/
38+
public $virtualHost;
39+
40+
/**
41+
* Heartbeat timeout defines max time the connection will be kept idle. Every `heartbeat` seconds the client will send
42+
* heartbeat frame to the server. If client doesn't hear from the server in `heartbeat * 2` seconds, the connection
43+
* is assumed to be dead.
44+
*
45+
* @var float
46+
*/
47+
public $heartbeat;
48+
49+
/**
50+
* Timeout to establish the connection (connection parameters tuning, SASL exchange, opening virtual host).
51+
*
52+
* @var float
53+
*/
54+
public $connectTimeout;
55+
56+
/**
57+
* Parameters for TLS-secured connections.
58+
*
59+
* @var TlsOptions|null
60+
*/
61+
public $tls;
62+
63+
public static function new()
64+
{
65+
return new static();
66+
}
67+
68+
public static function fromUrl(string $url)
69+
{
70+
throw new \LogicException("TODO");
71+
}
72+
73+
/**
74+
* @param string $host
75+
* @return self
76+
*/
77+
public function setHost(string $host)
78+
{
79+
$this->host = $host;
80+
return $this;
81+
}
82+
83+
/**
84+
* @param int $port
85+
* @return self
86+
*/
87+
public function setPort(int $port)
88+
{
89+
$this->port = $port;
90+
return $this;
91+
}
92+
93+
/**
94+
* @param SaslMechanismInterface[] $mechanisms
95+
* @return self
96+
*/
97+
public function setMechanisms(array $mechanisms)
98+
{
99+
$this->mechanisms = $mechanisms;
100+
return $this;
101+
}
102+
103+
/**
104+
* @param SaslMechanismInterface $mechanism
105+
* @return self
106+
*/
107+
public function addMechanism(SaslMechanismInterface $mechanism)
108+
{
109+
if ($this->mechanisms === null) {
110+
$this->mechanisms = [];
111+
}
112+
113+
$this->mechanisms[] = $mechanism;
114+
115+
return $this;
116+
}
117+
118+
/**
119+
* @param string $virtualHost
120+
* @return self
121+
*/
122+
public function setVirtualHost(string $virtualHost)
123+
{
124+
$this->virtualHost = $virtualHost;
125+
return $this;
126+
}
127+
128+
/**
129+
* @param float $heartbeat
130+
* @return self
131+
*/
132+
public function setHeartbeat(float $heartbeat)
133+
{
134+
$this->heartbeat = $heartbeat;
135+
return $this;
136+
}
137+
138+
/**
139+
* @param float $connectTimeout
140+
* @return self
141+
*/
142+
public function setConnectTimeout(float $connectTimeout)
143+
{
144+
$this->connectTimeout = $connectTimeout;
145+
return $this;
146+
}
147+
148+
/**
149+
* @param TlsOptions|null $tls
150+
* @return self
151+
*/
152+
public function setTls(?TlsOptions $tls)
153+
{
154+
$this->tls = $tls;
155+
return $this;
156+
}
157+
158+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
namespace Bunny\NG;
3+
4+
/**
5+
* @author Jakub Kulhan <[email protected]>
6+
*/
7+
final class RabbitConsumerOptions
8+
{
9+
10+
/**
11+
* Queue name.
12+
*
13+
* @var string
14+
*/
15+
public $queue;
16+
17+
/**
18+
* Consumer tag.
19+
*
20+
* @var string
21+
*/
22+
public $consumerTag;
23+
24+
public static function new()
25+
{
26+
return new static();
27+
}
28+
29+
/**
30+
* @param string $queue
31+
* @return self
32+
*/
33+
public function setQueue(string $queue)
34+
{
35+
$this->queue = $queue;
36+
return $this;
37+
}
38+
39+
/**
40+
* @param string $consumerTag
41+
* @return self
42+
*/
43+
public function setConsumerTag(string $consumerTag)
44+
{
45+
$this->consumerTag = $consumerTag;
46+
return $this;
47+
}
48+
49+
}

0 commit comments

Comments
 (0)