Skip to content

Commit e46b179

Browse files
authored
Merge pull request mookofe#15 from kierate/heartbeat
Allow configuring additional connection options
2 parents c4943ca + 4d20d66 commit e46b179

File tree

3 files changed

+110
-30
lines changed

3 files changed

+110
-30
lines changed

config/tail.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,20 @@
2424
'connections' => array(
2525

2626
'default_connection' => array(
27-
'host' => 'localhost',
28-
'port' => 5672,
29-
'username' => 'guest',
30-
'password' => 'guest',
31-
'vhost' => '/',
32-
'exchange' => 'amq.direct',
33-
'consumer_tag' => 'consumer',
34-
'exchange_type' => 'direct',
35-
'content_type' => 'text/plain'
27+
'host' => 'localhost',
28+
'port' => 5672,
29+
'username' => 'guest',
30+
'password' => 'guest',
31+
'vhost' => '/',
32+
'ssl_context_options' => null,
33+
'connection_timeout' => 3.0,
34+
'read_write_timeout' => 3.0,
35+
'keepalive' => false,
36+
'heartbeat' => 0,
37+
'exchange' => 'amq.direct',
38+
'consumer_tag' => 'consumer',
39+
'exchange_type' => 'direct',
40+
'content_type' => 'text/plain',
3641
),
3742
),
3843
);

readme.md

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -109,26 +109,40 @@ return array(
109109
'connections' => array(
110110

111111
'default_connection' => array(
112-
'host' => 'localhost',
113-
'port' => 5672,
114-
'username' => 'guest',
115-
'password' => 'guest',
116-
'vhost' => '/',
117-
'exchange' => 'default_exchange_name',
118-
'consumer_tag' => 'consumer',
119-
'exchange_type'=> 'direct',
120-
'content_type' => 'text/plain'
112+
'host' => 'localhost',
113+
'port' => 5672,
114+
'username' => 'guest',
115+
'password' => 'guest',
116+
'vhost' => '/',
117+
'ssl_context_options' => null,
118+
'connection_timeout' => 3,
119+
'read_write_timeout' => 50, //should be at least 2x heartbeat (if using heartbeat)
120+
'keepalive' => true, //requires php-amqplib v2.4.1+
121+
'heartbeat' => 25, //requires php-amqplib v2.4.1+
122+
'exchange' => 'default_exchange_name',
123+
'consumer_tag' => 'consumer',
124+
'exchange_type' => 'direct',
125+
'content_type' => 'text/plain'
121126
),
122127
'other_server' => array(
123-
'host' => '192.168.0.10',
124-
'port' => 5672,
125-
'username' => 'guest',
126-
'password' => 'guest',
127-
'vhost' => '/',
128-
'exchange' => 'default_exchange_name',
129-
'consumer_tag' => 'consumer',
130-
'exchange_type'=> 'fanout',
131-
'content_type' => 'application/json'
128+
'host' => '192.168.0.10',
129+
'port' => 5672,
130+
'username' => 'guest',
131+
'password' => 'guest',
132+
'vhost' => '/',
133+
'ssl_context_options' => array(
134+
'capath' => '/etc/ssl/certs',
135+
'cafile' => './startssl_ca.pem',
136+
'verify_peer' => true,
137+
),
138+
'connection_timeout' => 3.0,
139+
'read_write_timeout' => 3.0,
140+
'keepalive' => false,
141+
'heartbeat' => 0,
142+
'exchange' => 'default_exchange_name',
143+
'consumer_tag' => 'consumer',
144+
'exchange_type' => 'fanout',
145+
'content_type' => 'application/json'
132146
),
133147
),
134148
);

src/Connection.php

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use Exception;
44
use Mookofe\Tail\BaseOptions;
5-
use PhpAmqpLib\Connection\AMQPConnection;
5+
use PhpAmqpLib\Connection\AMQPSSLConnection;
66

77
/**
88
* Connection class, used to manage connection to the RabbitMQ Server
@@ -46,6 +46,41 @@ class Connection extends BaseOptions{
4646
*/
4747
public $consumer_tag;
4848

49+
/**
50+
* RabbitMQ connection SSL context options
51+
*
52+
* @var array
53+
*/
54+
public $ssl_context_options;
55+
56+
/**
57+
* RabbitMQ connection timeout in seconds
58+
*
59+
* @var float
60+
*/
61+
public $connection_timeout;
62+
63+
/**
64+
* RabbitMQ connection read/write timeout in seconds
65+
*
66+
* @var float
67+
*/
68+
public $read_write_timeout;
69+
70+
/**
71+
* RabbitMQ connection keepalive flag
72+
*
73+
* @var bool
74+
*/
75+
public $keepalive;
76+
77+
/**
78+
* RabbitMQ connection heartbeat in seconds
79+
*
80+
* @var int
81+
*/
82+
public $heartbeat;
83+
4984
/**
5085
* RabbitMQ AMQP Connection
5186
*
@@ -69,7 +104,19 @@ class Connection extends BaseOptions{
69104
*/
70105
public function __construct(array $options = null)
71106
{
72-
$this->allowedOptions = array_merge($this->allowedOptions, array('host', 'port', 'username', 'password', 'consumer_tag'));
107+
$this->allowedOptions = array_merge($this->allowedOptions, array(
108+
'host',
109+
'port',
110+
'username',
111+
'password',
112+
'consumer_tag',
113+
'ssl_context_options',
114+
'connection_timeout',
115+
'read_write_timeout',
116+
'keepalive',
117+
'heartbeat'
118+
)
119+
);
73120

74121
if (!$options)
75122
$options = $this->buildConnectionOptions();
@@ -86,7 +133,21 @@ public function open()
86133
{
87134
try
88135
{
89-
$this->AMQPConnection = new AMQPConnection($this->host, $this->port, $this->username, $this->password, $this->vhost);
136+
$additionalConnectionOptions = array();
137+
foreach (array('connection_timeout', 'read_write_timeout', 'keepalive', 'heartbeat') as $option) {
138+
if (isset($this->$option)) {
139+
$additionalConnectionOptions[$option] = $this->$option;
140+
}
141+
}
142+
$this->AMQPConnection = new AMQPSSLConnection(
143+
$this->host,
144+
$this->port,
145+
$this->username,
146+
$this->password,
147+
$this->vhost,
148+
$this->ssl_context_options,
149+
$additionalConnectionOptions
150+
);
90151
$this->channel = $this->AMQPConnection->channel();
91152
$this->channel->queue_declare($this->queue_name, false, false, false, false);
92153
$this->channel->exchange_declare($this->exchange, $this->exchange_type, false, true, false);

0 commit comments

Comments
 (0)