1- // Copyright 2019-2023 Google LLC
1+ // Copyright 2019-2024 Google LLC
22//
33// Licensed under the Apache License, Version 2.0 (the "License");
44// you may not use this file except in compliance with the License.
3939
4040// Imports the Google Cloud client library. v1 is for the lower level
4141// proto access.
42- const { v1 } = require ( '@google-cloud/pubsub' ) ;
42+ const { PubSub } = require ( '@google-cloud/pubsub' ) ;
4343
44- // Creates a publisher client.
45- const publisherClient = new v1 . PublisherClient ( {
46- // optional auth parameters
47- } ) ;
48- async function publishWithRetrySettings ( projectId , topicNameOrId , data ) {
49- const formattedTopic = publisherClient . projectTopicPath (
50- projectId ,
51- topicNameOrId
52- ) ;
53-
54- // Publishes the message as a string, e.g. "Hello, world!" or JSON.stringify(someObject)
55- const dataBuffer = Buffer . from ( data ) ;
56- const messagesElement = {
57- data : dataBuffer ,
58- } ;
59- const messages = [ messagesElement ] ;
60-
61- // Build the request
62- const request = {
63- topic : formattedTopic ,
64- messages : messages ,
65- } ;
44+ async function publishWithRetrySettings ( topicNameOrId , data ) {
45+ const pubsubClient = new PubSub ( ) ;
6646
6747 // Retry settings control how the publisher handles retryable failures. Default values are shown.
6848 // The `retryCodes` array determines which grpc errors will trigger an automatic retry.
6949 // The `backoffSettings` object lets you specify the behaviour of retries over time.
50+ //
51+ // Reference this document to see the current defaults for publishing:
52+ // https://github.com/googleapis/nodejs-pubsub/blob/6e2c28a9298a49dc1b194ce747ff5258c8df6deb/src/v1/publisher_client_config.json#L59
53+ //
54+ // Please note that _all_ items must be included when passing these settings to topic().
55+ // Otherwise, unpredictable (incorrect) defaults may be assumed.
7056 const retrySettings = {
7157 retryCodes : [
7258 10 , // 'ABORTED'
@@ -83,36 +69,42 @@ async function publishWithRetrySettings(projectId, topicNameOrId, data) {
8369 initialRetryDelayMillis : 100 ,
8470 // The multiplier by which to increase the delay time between the completion
8571 // of failed requests, and the initiation of the subsequent retrying request.
86- retryDelayMultiplier : 1.3 ,
72+ retryDelayMultiplier : 4 ,
8773 // The maximum delay time, in milliseconds, between requests.
8874 // When this value is reached, retryDelayMultiplier will no longer be used to increase delay time.
8975 maxRetryDelayMillis : 60000 ,
9076 // The initial timeout parameter to the request.
91- initialRpcTimeoutMillis : 5000 ,
77+ initialRpcTimeoutMillis : 60000 ,
9278 // The multiplier by which to increase the timeout parameter between failed requests.
9379 rpcTimeoutMultiplier : 1.0 ,
9480 // The maximum timeout parameter, in milliseconds, for a request. When this value is reached,
9581 // rpcTimeoutMultiplier will no longer be used to increase the timeout.
96- maxRpcTimeoutMillis : 600000 ,
82+ maxRpcTimeoutMillis : 60000 ,
9783 // The total time, in milliseconds, starting from when the initial request is sent,
9884 // after which an error will be returned, regardless of the retrying attempts made meanwhile.
9985 totalTimeoutMillis : 600000 ,
10086 } ,
10187 } ;
10288
103- const [ response ] = await publisherClient . publish ( request , {
104- retry : retrySettings ,
89+ // Cache topic objects (publishers) and reuse them.
90+ const topic = pubsubClient . topic ( topicNameOrId , {
91+ gaxOpts : {
92+ retry : retrySettings ,
93+ } ,
10594 } ) ;
106- console . log ( `Message ${ response . messageIds } published.` ) ;
95+
96+ // Publishes the message as a string, e.g. "Hello, world!" or JSON.stringify(someObject)
97+ const dataBuffer = Buffer . from ( data ) ;
98+ const messageId = await topic . publishMessage ( { data : dataBuffer } ) ;
99+ console . log ( `Message ${ messageId } published.` ) ;
107100}
108101// [END pubsub_publisher_retry_settings]
109102
110103function main (
111- projectId = 'YOUR_PROJECT_ID' ,
112104 topicNameOrId = 'YOUR_TOPIC_NAME_OR_ID' ,
113105 data = JSON . stringify ( { foo : 'bar' } )
114106) {
115- publishWithRetrySettings ( projectId , topicNameOrId , data ) . catch ( err => {
107+ publishWithRetrySettings ( topicNameOrId , data ) . catch ( err => {
116108 console . error ( err . message ) ;
117109 process . exitCode = 1 ;
118110 } ) ;
0 commit comments