Skip to content
This repository was archived by the owner on Jul 19, 2024. It is now read-only.

Commit 21e2fc1

Browse files
Merge pull request #1 from Superbalist/implement-unique-client-identifier
add support for unique client identifier
2 parents 8648ea7 + 5de96fe commit 21e2fc1

File tree

4 files changed

+62
-11
lines changed

4 files changed

+62
-11
lines changed

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ $client = new \Google\Cloud\PubSub\PubSubClient([
2727
$adapter = new \Superbalist\PubSub\GoogleCloud\GoogleCloudPubSubAdapter($client);
2828

2929

30+
// disable auto topic & subscription creation
31+
$adapter->setAutoCreateTopics(false); // this is true by default
32+
$adapter->setAutoCreateSubscriptions(false); // this is true by default
33+
34+
// set a unique client identifier for the subscriber
35+
$adapter->setClientIdentifier('search_service');
36+
3037
// consume messages
3138
// note: this is a blocking call
3239
$adapter->subscribe('my_channel', function ($message) {
@@ -38,10 +45,6 @@ $adapter->publish('my_channel', 'HELLO WORLD');
3845
$adapter->publish('my_channel', json_encode(['hello' => 'world']));
3946
$adapter->publish('my_channel', 1);
4047
$adapter->publish('my_channel', false);
41-
42-
// disable auto topic & subscription creation
43-
$adapter->setAutoCreateTopics(false); // this is true by default
44-
$adapter->setAutoCreateSubscriptions(false); // this is true by default
4548
```
4649

4750
## Examples

changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 2.0.0 - 2016-10-05
4+
5+
* Add new `$clientIdentifier` & `setClientIdentifier()` functionality to allow subscribers to use the same, or unique identifiers.
6+
37
## 1.0.2 - 2016-09-26
48

59
* Allow for google/cloud ^0.8.0 and ^0.9.0

src/GoogleCloudPubSubAdapter.php

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ class GoogleCloudPubSubAdapter implements PubSubAdapterInterface
1313
*/
1414
protected $client;
1515

16+
/**
17+
* @var string
18+
*/
19+
protected $clientIdentifier;
20+
1621
/**
1722
* @var bool
1823
*/
@@ -25,12 +30,14 @@ class GoogleCloudPubSubAdapter implements PubSubAdapterInterface
2530

2631
/**
2732
* @param PubSubClient $client
33+
* @param string $clientIdentifier
2834
* @param bool $autoCreateTopics
2935
* @param bool $autoCreateSubscriptions
3036
*/
31-
public function __construct(PubSubClient $client, $autoCreateTopics = true, $autoCreateSubscriptions = true)
37+
public function __construct(PubSubClient $client, $clientIdentifier = null, $autoCreateTopics = true, $autoCreateSubscriptions = true)
3238
{
3339
$this->client = $client;
40+
$this->clientIdentifier = $clientIdentifier;
3441
$this->autoCreateTopics = $autoCreateTopics;
3542
$this->autoCreateSubscriptions = $autoCreateSubscriptions;
3643
}
@@ -45,6 +52,32 @@ public function getClient()
4552
return $this->client;
4653
}
4754

55+
/**
56+
* Set the unique client identifier.
57+
*
58+
* The client identifier is used when creating a subscription to a topic.
59+
*
60+
* A topic can have multiple subscribers connected.
61+
* If all subscribers use the same client identifier, the messages will load-balance across them.
62+
* If all subscribers have different client identifiers, the messages will be dispatched all of them.
63+
*
64+
* @param string $clientIdentifier
65+
*/
66+
public function setClientIdentifier($clientIdentifier)
67+
{
68+
$this->clientIdentifier = $clientIdentifier;
69+
}
70+
71+
/**
72+
* Return the unique client identifier.
73+
*
74+
* @return string
75+
*/
76+
public function getClientIdentifier()
77+
{
78+
return $this->clientIdentifier;
79+
}
80+
4881
/**
4982
* Set whether or not topics will be auto created.
5083
*
@@ -156,7 +189,8 @@ protected function getTopicForChannel($channel)
156189
protected function getSubscriptionForChannel($channel)
157190
{
158191
$topic = $this->getTopicForChannel($channel);
159-
$subscription = $topic->subscription($channel);
192+
$clientIdentifier = $this->clientIdentifier ? $this->clientIdentifier : 'default';
193+
$subscription = $topic->subscription($clientIdentifier);
160194
if ($this->autoCreateSubscriptions && !$subscription->exists()) {
161195
$subscription->create();
162196
}

tests/GoogleCloudPubSubAdapterTest.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ public function testGetClient()
1818
$this->assertSame($client, $adapter->getClient());
1919
}
2020

21+
public function testGetSetClientIdentifier()
22+
{
23+
$client = Mockery::mock(PubSubClient::class);
24+
$adapter = new GoogleCloudPubSubAdapter($client);
25+
$this->assertNull($adapter->getClientIdentifier());
26+
27+
$adapter->setClientIdentifier('my_identifier');
28+
$this->assertEquals('my_identifier', $adapter->getClientIdentifier());
29+
}
30+
2131
public function testGetSetAutoCreateTopics()
2232
{
2333
$client = Mockery::mock(PubSubClient::class);
@@ -104,7 +114,7 @@ public function testPublishWhenAutoTopicCreationIsDisabled()
104114
->once()
105115
->andReturn($topic);
106116

107-
$adapter = new GoogleCloudPubSubAdapter($client, false);
117+
$adapter = new GoogleCloudPubSubAdapter($client, null, false);
108118

109119
$adapter->publish('channel_name', ['hello' => 'world']);
110120
}
@@ -162,7 +172,7 @@ public function testSubscribeWhenSubscriptionMustBeCreated()
162172
->andReturn(true);
163173
$topic->shouldNotHaveReceived('create');
164174
$topic->shouldReceive('subscription')
165-
->with('channel_name')
175+
->with('default')
166176
->once()
167177
->andReturn($subscription);
168178

@@ -237,7 +247,7 @@ public function testSubscribeWhenSubscriptionExists()
237247
->andReturn(true);
238248
$topic->shouldNotHaveReceived('create');
239249
$topic->shouldReceive('subscription')
240-
->with('channel_name')
250+
->with('default')
241251
->once()
242252
->andReturn($subscription);
243253

@@ -310,7 +320,7 @@ public function testSubscribeWhenAutoTopicCreationIsDisabled()
310320
->andReturn(true);
311321
$topic->shouldNotHaveReceived('create');
312322
$topic->shouldReceive('subscription')
313-
->with('channel_name')
323+
->with('default')
314324
->once()
315325
->andReturn($subscription);
316326

@@ -320,7 +330,7 @@ public function testSubscribeWhenAutoTopicCreationIsDisabled()
320330
->once()
321331
->andReturn($topic);
322332

323-
$adapter = new GoogleCloudPubSubAdapter($client, true, false);
333+
$adapter = new GoogleCloudPubSubAdapter($client, null, true, false);
324334

325335
$handler1 = Mockery::mock(\stdClass::class);
326336
$handler1->shouldReceive('handle')

0 commit comments

Comments
 (0)