Skip to content

Commit

Permalink
Allow to define custom config for Google\Cloud\PubSub\PubSubClient (#11)
Browse files Browse the repository at this point in the history
Co-authored-by: Ronald Marfoldi <ronald.marfoldi@petitpress.sk>
  • Loading branch information
marforon and Ronald Marfoldi authored Feb 11, 2022
1 parent b201463 commit 3bf9a84
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 10 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ framework:
gps_transport:
dsn: 'gps://default'
options:
client_config: # optional (default: [])
apiEndpoint: 'https://europe-west3-pubsub.googleapis.com'
max_messages_pull: 10 # optional (default: 10)
topic: # optional (default name: messages)
name: 'messages'
Expand All @@ -64,10 +66,10 @@ framework:
messenger:
transports:
gps_transport:
dsn: 'gps://default/messages?max_messages_pull=10'
dsn: 'gps://default/messages?client_config[apiEndpoint]=https://europe-west3-pubsub.googleapis.com&max_messages_pull=10'
```
### Step 4: Use available stamps if needed
* `OrderingKeyStamp`: use for keeping messages of the same context in order.
For more information, read an [official documentation](https://cloud.google.com/pubsub/docs/publisher#using_ordering_keys).
For more information, read an [official documentation](https://cloud.google.com/pubsub/docs/publisher#using_ordering_keys).
17 changes: 14 additions & 3 deletions Transport/GpsConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,18 @@ final class GpsConfiguration implements GpsConfigurationInterface
private string $queueName;
private string $subscriptionName;
private int $maxMessagesPull;
private array $clientConfig;

public function __construct(string $queueName, string $subscriptionName, int $maxMessagesPull)
{
public function __construct(
string $queueName,
string $subscriptionName,
int $maxMessagesPull,
array $clientConfig
) {
$this->queueName = $queueName;
$this->subscriptionName = $subscriptionName;
$this->maxMessagesPull = $maxMessagesPull;
$this->clientConfig = $clientConfig;
}

public function getQueueName(): string
Expand All @@ -34,4 +40,9 @@ public function getMaxMessagesPull(): int
{
return $this->maxMessagesPull;
}
}

public function getClientConfig(): array
{
return $this->clientConfig;
}
}
9 changes: 8 additions & 1 deletion Transport/GpsConfigurationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace PetitPress\GpsMessengerBundle\Transport;

use Google\Cloud\PubSub\PubSubClient;

/**
* @author Ronald Marfoldi <ronald.marfoldi@petitpress.sk>
*/
Expand All @@ -14,4 +16,9 @@ public function getQueueName(): string;
public function getSubscriptionName(): string;

public function getMaxMessagesPull(): int;
}

/**
* @see PubSubClient constructor options
*/
public function getClientConfig(): array;
}
5 changes: 4 additions & 1 deletion Transport/GpsConfigurationResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function resolve(string $dsn, array $options): GpsConfigurationInterface

$optionsResolver = new OptionsResolver();
$optionsResolver
->setDefault('client_config', [])
->setDefault('max_messages_pull', self::DEFAULT_MAX_MESSAGES_PULL)
->setDefault('topic', static function (OptionsResolver $topicResolver): void {
$topicResolver
Expand All @@ -39,6 +40,7 @@ public function resolve(string $dsn, array $options): GpsConfigurationInterface
return ((int) filter_var($value, FILTER_SANITIZE_NUMBER_INT)) ?: null;
})
->setAllowedTypes('max_messages_pull', ['int', 'string'])
->setAllowedTypes('client_config', 'array')
;

$dnsOptions = [];
Expand All @@ -60,6 +62,7 @@ public function resolve(string $dsn, array $options): GpsConfigurationInterface
$resolvedOptions['topic']['name'],
$resolvedOptions['queue']['name'],
$resolvedOptions['max_messages_pull'],
$resolvedOptions['client_config'],
);
}
}
}
8 changes: 5 additions & 3 deletions Transport/GpsTransportFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ public function __construct(GpsConfigurationResolverInterface $gpsConfigurationR
*/
public function createTransport(string $dsn, array $options, SerializerInterface $serializer): TransportInterface
{
$options = $this->gpsConfigurationResolver->resolve($dsn, $options);

return new GpsTransport(
new PubSubClient(),
$this->gpsConfigurationResolver->resolve($dsn, $options),
new PubSubClient($options->getClientConfig()),
$options,
$serializer
);
}
Expand All @@ -40,4 +42,4 @@ public function supports(string $dsn, array $options): bool
{
return str_starts_with($dsn, 'gps://');
}
}
}

0 comments on commit 3bf9a84

Please sign in to comment.