@@ -209,13 +209,43 @@ internal void Validate()
209
209
/// high network bandwidth (e.g. Google Compute Engine instances). If running with more limited network bandwidth, some
210
210
/// settings may need changing; especially
211
211
/// <see cref="ClientCreationSettings.PublisherServiceApiSettings"/>.<see cref="PublisherServiceApiSettings.PublishSettings"/>.<see cref="CallSettings.Retry"/>.
212
+ /// By default this method generates a gRPC channel per CPU core; if using a high-core-count machine and using many
213
+ /// clients concurrently then this may need reducing; use the setting <see cref="ClientCreationSettings.ClientCount"/>.
212
214
/// </summary>
213
215
/// <param name="topicName">The <see cref="TopicName"/> to publish messages to.</param>
214
216
/// <param name="clientCreationSettings">Optional. <see cref="ClientCreationSettings"/> specifying how to create
215
217
/// <see cref="PublisherServiceApiClient"/>s.</param>
216
218
/// <param name="settings">Optional. <see cref="Settings"/> for creating a <see cref="PublisherClient"/>.</param>
217
219
/// <returns>A <see cref="PublisherClient"/> instance associated with the specified <see cref="TopicName"/>.</returns>
218
- public static async Task < PublisherClient > CreateAsync ( TopicName topicName , ClientCreationSettings clientCreationSettings = null , Settings settings = null )
220
+ public static PublisherClient Create ( TopicName topicName , ClientCreationSettings clientCreationSettings = null , Settings settings = null ) =>
221
+ // With isAsync set to false, the returned task will already be completed (either successfully or faulted),
222
+ // so .ResultWithUnwrappedExceptions() will always return immediately.
223
+ CreateMaybeAsync ( topicName , clientCreationSettings , settings , isAsync : false ) . ResultWithUnwrappedExceptions ( ) ;
224
+
225
+ /// <summary>
226
+ /// Create a <see cref="PublisherClient"/> instance associated with the specified <see cref="TopicName"/>.
227
+ /// The default <paramref name="settings"/> and <paramref name="clientCreationSettings"/> are suitable for machines with
228
+ /// high network bandwidth (e.g. Google Compute Engine instances). If running with more limited network bandwidth, some
229
+ /// settings may need changing; especially
230
+ /// <see cref="ClientCreationSettings.PublisherServiceApiSettings"/>.<see cref="PublisherServiceApiSettings.PublishSettings"/>.<see cref="CallSettings.Retry"/>.
231
+ /// By default this method generates a gRPC channel per CPU core; if using a high-core-count machine and using many
232
+ /// clients concurrently then this may need reducing; use the setting <see cref="ClientCreationSettings.ClientCount"/>.
233
+ /// </summary>
234
+ /// <param name="topicName">The <see cref="TopicName"/> to publish messages to.</param>
235
+ /// <param name="clientCreationSettings">Optional. <see cref="ClientCreationSettings"/> specifying how to create
236
+ /// <see cref="PublisherServiceApiClient"/>s.</param>
237
+ /// <param name="settings">Optional. <see cref="Settings"/> for creating a <see cref="PublisherClient"/>.</param>
238
+ /// <returns>A <see cref="PublisherClient"/> instance associated with the specified <see cref="TopicName"/>.</returns>
239
+ public static Task < PublisherClient > CreateAsync ( TopicName topicName , ClientCreationSettings clientCreationSettings = null , Settings settings = null ) =>
240
+ // With isAsync set to true, the returned task will complete asynchronously (if required) as expected.
241
+ CreateMaybeAsync ( topicName , clientCreationSettings , settings , isAsync : true ) ;
242
+
243
+ /// <summary>
244
+ /// Creates a <see cref="PublisherClient"/>.
245
+ /// <paramref name="isAsync"/> controls whether the returned task will complete synchronously or asynchronously, allowing this
246
+ /// method to be used by both <see cref="Create"/> and <see cref="CreateAsync"/>.
247
+ /// </summary>
248
+ private static async Task < PublisherClient > CreateMaybeAsync ( TopicName topicName , ClientCreationSettings clientCreationSettings , Settings settings , bool isAsync )
219
249
{
220
250
clientCreationSettings ? . Validate ( ) ;
221
251
// Clone settings, just in case user modifies them and an await happens in this method
@@ -225,7 +255,9 @@ public static async Task<PublisherClient> CreateAsync(TopicName topicName, Clien
225
255
// Use default credentials if none given.
226
256
if ( channelCredentials == null )
227
257
{
228
- var credentials = await GoogleCredential . GetApplicationDefaultAsync ( ) . ConfigureAwait ( false ) ;
258
+ var credentials = isAsync ?
259
+ ( await GoogleCredential . GetApplicationDefaultAsync ( ) . ConfigureAwait ( false ) ) :
260
+ GoogleCredential . GetApplicationDefault ( ) ;
229
261
if ( credentials . IsCreateScopedRequired )
230
262
{
231
263
credentials = credentials . CreateScoped ( PublisherServiceApiClient . DefaultScopes ) ;
@@ -252,7 +284,9 @@ public static async Task<PublisherClient> CreateAsync(TopicName topicName, Clien
252
284
Settings = clientCreationSettings ? . PublisherServiceApiSettings ,
253
285
ChannelOptions = grpcChannelOptions
254
286
} ;
255
- var channel = await builder . CreateChannelAsync ( cancellationToken : default ) . ConfigureAwait ( false ) ;
287
+ var channel = isAsync ?
288
+ await builder . CreateChannelAsync ( cancellationToken : default ) . ConfigureAwait ( false ) :
289
+ builder . CreateChannel ( ) ;
256
290
257
291
// Second builder doesn't need to do much, as we can build a call invoker from the channel.
258
292
clients [ i ] = new PublisherServiceApiClientBuilder
0 commit comments