Google-cloud-dotnet: PubSub Connection Lifetime

Created on 16 Sep 2019  路  6Comments  路  Source: googleapis/google-cloud-dotnet

I am using the PubSub to publish/subscribe messages to a specific topic/subscription and everything is working as expected. 馃憤

My question is about the connection life time. As I can read in the documentation it says to use PublisherClient and SubscriberClient since it's more performant and simple comparing to PublisherServiceApiClient and SubscriberServiceApiClient.

Should, I create a new publisher per message or can I reuse the same publisher to send messages?

// new publisher per message
var publisher = await PublisherClient.CreateAsync(topicName);
var messageId = await publisher.PublishAsync("My message");
await publisher.ShutdownAsync(TimeSpan.FromSeconds(15));

I assume that for subscribers, I MUST reuse the same subscriber! Also, if some connectivity problem occurred, do I need to handle them or the subscriber can reconnect automatically?

question

All 6 comments

Assigned Chris to comment as he knows far more about PubSub than I do, but if you're publishing multiple messages to the same topic, I'd definitely expect it to be best to create a single publisher.
Chris can answer the latter parts...

@Talento90 Reuse the publisher and subscribers as much as possible.
Both will recover from transient errors without any action required from your code.
Unrecoverable errors will throw relevant exceptions, which you will need to handle. An example of an unrecoverable exception would be incorrect auth; or trying to use a non-existent topic/subscription.

Regarding authentication, are there any way to authenticate PubSub without using GOOGLE_APPLICATION_CREDENTIALS? I don't have access to the file system... If I load the Json service account it will work? Or it only supports file path?

You can pass in a PublisherClient.ClientCreationSettings when you create the PublisherClient. Those client creation settings can specify the ChannelCredentials. You can create these with code like this:

using Grpc.Auth; // Used for extension methods
using Google.Apis.Auth.OAuth2; // Used for GoogleCredential
...

string json = ...; // Wherever you get the JSON from 
GoogleCredential credential = GoogleCredential.FromJson(json);
ChannelCredentials channelCredentials = credential.ToChannelCredentials();

This works like a charm! 馃憤

var credential = GoogleCredential.FromJson(json);
var channelCredentials = credential.ToChannelCredentials();
var settings = new PublisherClient.ClientCreationSettings(credentials: channelCredentials);
var channel = new Channel(
   PublisherServiceApiClient.DefaultEndpoint.Host,
   PublisherServiceApiClient.DefaultEndpoint.Port,
   channelCredentials
);

var topicName = new TopicName("project-id", topicId);
var publisherService = PublisherServiceApiClient.Create(channel);

try 
{
   await publisherService.CreateTopicAsync(topicName);
}
catch (RpcException e) when (e.Status.StatusCode == StatusCode.AlreadyExists){}

var publisherClient = await PublisherClient.CreateAsync(topicName, settings);

I am going to close this topic! Thank you both! 馃檱

Was this page helpful?
0 / 5 - 0 ratings