google-cloud-pubsub 2.15.3 → 3.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/AUTHENTICATION.md +8 -26
  3. data/CHANGELOG.md +108 -0
  4. data/OVERVIEW.md +188 -144
  5. data/lib/google/cloud/pubsub/admin_clients.rb +116 -0
  6. data/lib/google/cloud/pubsub/async_publisher.rb +9 -9
  7. data/lib/google/cloud/pubsub/batch_publisher.rb +4 -4
  8. data/lib/google/cloud/pubsub/convert.rb +1 -1
  9. data/lib/google/cloud/pubsub/errors.rb +3 -3
  10. data/lib/google/cloud/pubsub/flow_controller.rb +0 -2
  11. data/lib/google/cloud/pubsub/message.rb +10 -11
  12. data/lib/google/cloud/pubsub/{subscriber → message_listener}/enumerator_queue.rb +1 -1
  13. data/lib/google/cloud/pubsub/{subscriber → message_listener}/inventory.rb +2 -4
  14. data/lib/google/cloud/pubsub/{subscriber → message_listener}/sequencer.rb +1 -1
  15. data/lib/google/cloud/pubsub/{subscriber → message_listener}/stream.rb +15 -14
  16. data/lib/google/cloud/pubsub/{subscriber → message_listener}/timed_unary_buffer.rb +1 -1
  17. data/lib/google/cloud/pubsub/message_listener.rb +413 -0
  18. data/lib/google/cloud/pubsub/project.rb +103 -523
  19. data/lib/google/cloud/pubsub/publisher.rb +373 -0
  20. data/lib/google/cloud/pubsub/received_message.rb +50 -45
  21. data/lib/google/cloud/pubsub/service.rb +40 -352
  22. data/lib/google/cloud/pubsub/subscriber.rb +442 -279
  23. data/lib/google/cloud/pubsub/version.rb +1 -1
  24. data/lib/google/cloud/pubsub.rb +24 -28
  25. data/lib/google-cloud-pubsub.rb +8 -6
  26. metadata +18 -183
  27. data/lib/google/cloud/pubsub/policy.rb +0 -188
  28. data/lib/google/cloud/pubsub/retry_policy.rb +0 -88
  29. data/lib/google/cloud/pubsub/schema/list.rb +0 -180
  30. data/lib/google/cloud/pubsub/schema.rb +0 -310
  31. data/lib/google/cloud/pubsub/snapshot/list.rb +0 -178
  32. data/lib/google/cloud/pubsub/snapshot.rb +0 -205
  33. data/lib/google/cloud/pubsub/subscription/list.rb +0 -205
  34. data/lib/google/cloud/pubsub/subscription/push_config.rb +0 -268
  35. data/lib/google/cloud/pubsub/subscription.rb +0 -1467
  36. data/lib/google/cloud/pubsub/topic/list.rb +0 -171
  37. data/lib/google/cloud/pubsub/topic.rb +0 -1100
@@ -0,0 +1,116 @@
1
+ # Copyright 2025 Google LLC
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require "google/cloud/pubsub/v1/topic_admin/client"
16
+ require "google/cloud/pubsub/v1/subscription_admin/client"
17
+
18
+ module Google
19
+ module Cloud
20
+ module PubSub
21
+ module TopicAdmin
22
+ ##
23
+ # The TopicAdmin client is used to manage topics.
24
+ #
25
+ # This client is a subclass of the auto-generated TopicAdmin client, and provides
26
+ # the same methods. However, it raises errors on data plane operations
27
+ # to prevent misuse.
28
+ #
29
+ class Client < Google::Cloud::PubSub::V1::TopicAdmin::Client
30
+ # @private
31
+ alias publish_internal publish
32
+
33
+ ##
34
+ # The `publish` method is a data plane operation.
35
+ #
36
+ # @raise [NotImplementedError] This method is not implemented on the
37
+ # admin client. Use {Google::Cloud::PubSub::Publisher} instead.
38
+ #
39
+ def publish *args, **kwargs
40
+ raise NotImplementedError,
41
+ "The `publish` method is a data plane operation. " \
42
+ "Use Google::Cloud::PubSub::Publisher instead."
43
+ end
44
+ end
45
+ end
46
+
47
+ module SubscriptionAdmin
48
+ ##
49
+ # The SubscriptionAdmin client is used to manage subscriptions.
50
+ #
51
+ # This client is a subclass of the auto-generated SubscriptionAdmin client, and
52
+ # provides the same methods. However, it raises errors on data plane
53
+ # operations to prevent misuse.
54
+ #
55
+ class Client < Google::Cloud::PubSub::V1::SubscriptionAdmin::Client
56
+ # @private
57
+ alias modify_ack_deadline_internal modify_ack_deadline
58
+ # @private
59
+ alias acknowledge_internal acknowledge
60
+ # @private
61
+ alias pull_internal pull
62
+ # @private
63
+ alias streaming_pull_internal streaming_pull
64
+
65
+ ##
66
+ # The `modify_ack_deadline` method is a data plane operation.
67
+ #
68
+ # @raise [NotImplementedError] This method is not implemented on the
69
+ # admin client. Use {Google::Cloud::PubSub::Subscriber} instead.
70
+ #
71
+ def modify_ack_deadline *args, **kwargs
72
+ raise NotImplementedError,
73
+ "The `modify_ack_deadline` method is a data plane operation. " \
74
+ "Use Google::Cloud::PubSub::Subscriber instead."
75
+ end
76
+
77
+ ##
78
+ # The `acknowledge` method is a data plane operation.
79
+ #
80
+ # @raise [NotImplementedError] This method is not implemented on the
81
+ # admin client. Use {Google::Cloud::PubSub::Subscriber} instead.
82
+ #
83
+ def acknowledge *args, **kwargs
84
+ raise NotImplementedError,
85
+ "The `acknowledge` method is a data plane operation. " \
86
+ "Use Google::Cloud::PubSub::Subscriber instead."
87
+ end
88
+
89
+ ##
90
+ # The `pull` method is a data plane operation.
91
+ #
92
+ # @raise [NotImplementedError] This method is not implemented on the
93
+ # admin client. Use {Google::Cloud::PubSub::Subscriber} instead.
94
+ #
95
+ def pull *args, **kwargs
96
+ raise NotImplementedError,
97
+ "The `pull` method is a data plane operation. " \
98
+ "Use Google::Cloud::PubSub::Subscriber instead."
99
+ end
100
+
101
+ ##
102
+ # The `streaming_pull` method is a data plane operation.
103
+ #
104
+ # @raise [NotImplementedError] This method is not implemented on the
105
+ # admin client. Use {Google::Cloud::PubSub::Subscriber} instead.
106
+ #
107
+ def streaming_pull *args, **kwargs
108
+ raise NotImplementedError,
109
+ "The `streaming_pull` method is a data plane operation. " \
110
+ "Use Google::Cloud::PubSub::Subscriber instead."
111
+ end
112
+ end
113
+ end
114
+ end
115
+ end
116
+ end
@@ -27,15 +27,15 @@ module Google
27
27
  module PubSub
28
28
  ##
29
29
  # Used to publish multiple messages in batches to a topic. See
30
- # {Google::Cloud::PubSub::Topic#async_publisher}
30
+ # {Google::Cloud::PubSub::Publisher#async_publisher}
31
31
  #
32
32
  # @example
33
33
  # require "google/cloud/pubsub"
34
34
  #
35
35
  # pubsub = Google::Cloud::PubSub.new
36
36
  #
37
- # topic = pubsub.topic "my-topic"
38
- # topic.publish_async "task completed" do |result|
37
+ # publisher = pubsub.publisher "my-topic"
38
+ # publisher.publish_async "task completed" do |result|
39
39
  # if result.succeeded?
40
40
  # log_publish_success result.data
41
41
  # else
@@ -43,7 +43,7 @@ module Google
43
43
  # end
44
44
  # end
45
45
  #
46
- # topic.async_publisher.stop!
46
+ # publisher.async_publisher.stop!
47
47
  #
48
48
  # @attr_reader [String] topic_name The name of the topic the messages are published to. The value is a
49
49
  # fully-qualified topic name in the form `projects/{project_id}/topics/{topic_id}`.
@@ -116,7 +116,7 @@ module Google
116
116
  ##
117
117
  # Add a message to the async publisher to be published to the topic.
118
118
  # Messages will be collected in batches and published together.
119
- # See {Google::Cloud::PubSub::Topic#publish_async}
119
+ # See {Google::Cloud::PubSub::Publisher#publish_async}
120
120
  #
121
121
  # @param [String, File] data The message payload. This will be converted
122
122
  # to bytes encoded as ASCII-8BIT.
@@ -262,8 +262,8 @@ module Google
262
262
  # enabled, messages published with the same `ordering_key` will be
263
263
  # delivered in the order they were published.
264
264
  #
265
- # See {#message_ordering?}. See {Topic#publish_async},
266
- # {Subscription#listen}, and {Message#ordering_key}.
265
+ # See {#message_ordering?}. See {Publisher#publish_async},
266
+ # {Subscriber#listen}, and {Message#ordering_key}.
267
267
  #
268
268
  def enable_message_ordering!
269
269
  synchronize { @ordered = true }
@@ -275,8 +275,8 @@ module Google
275
275
  # will be delivered in the order they were published. When disabled,
276
276
  # messages may be delivered in any order.
277
277
  #
278
- # See {#enable_message_ordering!}. See {Topic#publish_async},
279
- # {Subscription#listen}, and {Message#ordering_key}.
278
+ # See {#enable_message_ordering!}. See {Publisher#publish_async},
279
+ # {Subscriber#listen}, and {Message#ordering_key}.
280
280
  #
281
281
  # @return [Boolean]
282
282
  #
@@ -27,8 +27,8 @@ module Google
27
27
  #
28
28
  # pubsub = Google::Cloud::PubSub.new
29
29
  #
30
- # topic = pubsub.topic "my-topic"
31
- # msgs = topic.publish do |batch_publisher|
30
+ # publisher = pubsub.publisher "my-topic"
31
+ # msgs = publisher.publish do |batch_publisher|
32
32
  # batch_publisher.publish "task 1 completed", foo: :bar
33
33
  # batch_publisher.publish "task 2 completed", foo: :baz
34
34
  # batch_publisher.publish "task 3 completed", foo: :bif
@@ -86,8 +86,8 @@ module Google
86
86
  #
87
87
  # pubsub = Google::Cloud::PubSub.new
88
88
  #
89
- # topic = pubsub.topic "my-topic"
90
- # msgs = topic.publish do |batch_publisher|
89
+ # publisher = pubsub.publisher "my-topic"
90
+ # msgs = publisher.publish do |batch_publisher|
91
91
  # batch_publisher.publish "task 1 completed", foo: :bar
92
92
  # batch_publisher.publish "task 2 completed", foo: :baz
93
93
  # batch_publisher.publish "task 3 completed", foo: :bif
@@ -69,7 +69,7 @@ module Google
69
69
  data_bytes = String(data).dup.force_encoding(Encoding::ASCII_8BIT).freeze
70
70
 
71
71
  # Convert attributes to strings to match the protobuf definition
72
- attributes = Hash[attributes.map { |k, v| [String(k), String(v)] }]
72
+ attributes = attributes.to_h { |k, v| [String(k), String(v)] }
73
73
 
74
74
  # Ordering Key must always be a string
75
75
  ordering_key = String(ordering_key).freeze
@@ -41,7 +41,7 @@ module Google
41
41
  end
42
42
 
43
43
  ##
44
- # Indicates that the {Subscriber} for a {Subscription} with message
44
+ # Indicates that the {MessageListener} for a {Subscriber} with message
45
45
  # ordering enabled has observed that a message has been delivered out of
46
46
  # order.
47
47
  #
@@ -57,11 +57,11 @@ module Google
57
57
 
58
58
  ##
59
59
  # Indicates that messages using the {#ordering_key} are not being
60
- # published due to error. Future calls to {Topic#publish_async} with the
60
+ # published due to error. Future calls to {Publisher#publish_async} with the
61
61
  # {#ordering_key} will fail with this error.
62
62
  #
63
63
  # To allow future messages with the {#ordering_key} to be published, the
64
- # {#ordering_key} must be passed to {Topic#resume_publish}.
64
+ # {#ordering_key} must be passed to {Publisher#resume_publish}.
65
65
  #
66
66
  # If this error is retrieved from {PublishResult#error}, inspect `cause`
67
67
  # for the error raised while publishing.
@@ -84,7 +84,6 @@ module Google
84
84
 
85
85
  protected
86
86
 
87
- # rubocop:disable Style/IdenticalConditionalBranches
88
87
  # rubocop:disable Style/GuardClause
89
88
 
90
89
  def acquire_or_wait message_size
@@ -117,7 +116,6 @@ module Google
117
116
  end
118
117
  end
119
118
 
120
- # rubocop:enable Style/IdenticalConditionalBranches
121
119
  # rubocop:enable Style/GuardClause
122
120
 
123
121
  def is_new_and_others_wait? waiter
@@ -24,7 +24,7 @@ module Google
24
24
  #
25
25
  # Represents a Pub/Sub Message.
26
26
  #
27
- # Message objects are created by {Topic#publish}. {Subscription#pull}
27
+ # Message objects are created by {Publisher#publish}. {Subscriber#pull}
28
28
  # returns an array of {ReceivedMessage} objects, each of which contains a
29
29
  # Message object. Each {ReceivedMessage} object can be acknowledged and/or
30
30
  # delayed.
@@ -35,22 +35,22 @@ module Google
35
35
  # pubsub = Google::Cloud::PubSub.new
36
36
  #
37
37
  # # Publish a message
38
- # topic = pubsub.topic "my-topic"
39
- # message = topic.publish "task completed"
38
+ # publisher = pubsub.publisher "my-topic"
39
+ # message = publisher.publish "task completed"
40
40
  # message.data #=> "task completed"
41
41
  #
42
42
  # # Listen for messages
43
- # sub = pubsub.subscription "my-topic-sub"
44
- # subscriber = sub.listen do |received_message|
43
+ # subscriber = pubsub.subscriber "my-topic-sub"
44
+ # listener = subscriber.listen do |received_message|
45
45
  # # process message
46
46
  # received_message.acknowledge!
47
47
  # end
48
48
  #
49
49
  # # Start background threads that will call the block passed to listen.
50
- # subscriber.start
50
+ # listener.start
51
51
  #
52
52
  # # Shut down the subscriber when ready to stop receiving messages.
53
- # subscriber.stop!
53
+ # listener.stop!
54
54
  #
55
55
  class Message
56
56
  ##
@@ -62,7 +62,7 @@ module Google
62
62
  # This can be used to publish several messages in bulk.
63
63
  def initialize data = nil, attributes = {}
64
64
  # Convert attributes to strings to match the protobuf definition
65
- attributes = Hash[attributes.map { |k, v| [String(k), String(v)] }]
65
+ attributes = attributes.to_h { |k, v| [String(k), String(v)] }
66
66
 
67
67
  @grpc = Google::Cloud::PubSub::V1::PubsubMessage.new(
68
68
  data: String(data).dup.force_encoding(Encoding::ASCII_8BIT),
@@ -81,8 +81,7 @@ module Google
81
81
  # Optional attributes for the message.
82
82
  def attributes
83
83
  return @grpc.attributes.to_h if @grpc.attributes.respond_to? :to_h
84
- # Enumerable doesn't have to_h on Ruby 2.0, so fallback to this
85
- Hash[@grpc.attributes.to_a]
84
+ @grpc.attributes.to_a.to_h
86
85
  end
87
86
 
88
87
  ##
@@ -114,7 +113,7 @@ module Google
114
113
  # or scalability, as the service automatically distributes messages for
115
114
  # different ordering keys across subscribers.
116
115
  #
117
- # See {Topic#publish_async} and {Subscription#listen}.
116
+ # See {Publisher#publish_async} and {Subscriber#listen}.
118
117
  #
119
118
  # @return [String]
120
119
  #
@@ -16,7 +16,7 @@
16
16
  module Google
17
17
  module Cloud
18
18
  module PubSub
19
- class Subscriber
19
+ class MessageListener
20
20
  # @private
21
21
  class EnumeratorQueue
22
22
  def initialize sentinel = nil
@@ -18,7 +18,7 @@ require "monitor"
18
18
  module Google
19
19
  module Cloud
20
20
  module PubSub
21
- class Subscriber
21
+ class MessageListener
22
22
  ##
23
23
  # @private
24
24
  class Inventory
@@ -36,10 +36,9 @@ module Google
36
36
  attr_reader :extension
37
37
  attr_reader :max_duration_per_lease_extension
38
38
  attr_accessor :min_duration_per_lease_extension
39
- attr_reader :use_legacy_flow_control
40
39
 
41
40
  def initialize stream, limit:, bytesize:, extension:, max_duration_per_lease_extension:,
42
- min_duration_per_lease_extension:, use_legacy_flow_control:
41
+ min_duration_per_lease_extension:
43
42
  super()
44
43
  @stream = stream
45
44
  @limit = limit
@@ -47,7 +46,6 @@ module Google
47
46
  @extension = extension
48
47
  @max_duration_per_lease_extension = max_duration_per_lease_extension
49
48
  @min_duration_per_lease_extension = min_duration_per_lease_extension
50
- @use_legacy_flow_control = use_legacy_flow_control
51
49
  @inventory = {}
52
50
  @wait_cond = new_cond
53
51
  end
@@ -18,7 +18,7 @@ require "monitor"
18
18
  module Google
19
19
  module Cloud
20
20
  module PubSub
21
- class Subscriber
21
+ class MessageListener
22
22
  ##
23
23
  # @private The sequencer's job is simple, keep track of all the
24
24
  # streams's recieved message and deliver the messages with an
@@ -13,9 +13,9 @@
13
13
  # limitations under the License.
14
14
 
15
15
 
16
- require "google/cloud/pubsub/subscriber/sequencer"
17
- require "google/cloud/pubsub/subscriber/enumerator_queue"
18
- require "google/cloud/pubsub/subscriber/inventory"
16
+ require "google/cloud/pubsub/message_listener/sequencer"
17
+ require "google/cloud/pubsub/message_listener/enumerator_queue"
18
+ require "google/cloud/pubsub/message_listener/inventory"
19
19
  require "google/cloud/pubsub/service"
20
20
  require "google/cloud/errors"
21
21
  require "monitor"
@@ -24,7 +24,7 @@ require "concurrent"
24
24
  module Google
25
25
  module Cloud
26
26
  module PubSub
27
- class Subscriber
27
+ class MessageListener
28
28
  ##
29
29
  # @private
30
30
  class Stream
@@ -231,7 +231,8 @@ module Google
231
231
  end
232
232
 
233
233
  # Call the StreamingPull API to get the response enumerator
234
- enum = @subscriber.service.streaming_pull @request_queue.each
234
+ options = { :"metadata" => { :"x-goog-request-params" => @subscriber.subscription_name } }
235
+ enum = @subscriber.service.streaming_pull @request_queue.each, options
235
236
 
236
237
  loop do
237
238
  synchronize do
@@ -245,7 +246,7 @@ module Google
245
246
  break if synchronize { @stopped }
246
247
 
247
248
  begin
248
- # Cannot syncronize the enumerator, causes deadlock
249
+ # Cannot synchronize the enumerator, causes deadlock
249
250
  response = enum.next
250
251
  new_exactly_once_delivery_enabled = response&.subscription_properties&.exactly_once_delivery_enabled
251
252
  received_messages = response.received_messages
@@ -253,7 +254,7 @@ module Google
253
254
  # Use synchronize so changes happen atomically
254
255
  synchronize do
255
256
  update_min_duration_per_lease_extension new_exactly_once_delivery_enabled
256
- @exactly_once_delivery_enabled = new_exactly_once_delivery_enabled unless new_exactly_once_delivery_enabled.nil?
257
+ @exactly_once_delivery_enabled = new_exactly_once_delivery_enabled unless new_exactly_once_delivery_enabled.nil?
257
258
  @subscriber.exactly_once_delivery_enabled = @exactly_once_delivery_enabled
258
259
 
259
260
  # Create receipt of received messages reception
@@ -271,7 +272,7 @@ module Google
271
272
  # No need to synchronize the callback future
272
273
  register_callback rec_msg
273
274
  end if !@exactly_once_delivery_enabled # Exactly once delivery scenario is handled by callback
274
-
275
+
275
276
  synchronize { pause_streaming! }
276
277
  rescue StopIteration
277
278
  break
@@ -371,8 +372,8 @@ module Google
371
372
  end
372
373
 
373
374
  def pause_streaming?
374
- return if @stopped
375
- return if @paused
375
+ return false if @stopped
376
+ return false if @paused
376
377
 
377
378
  @inventory.full?
378
379
  end
@@ -386,8 +387,8 @@ module Google
386
387
  end
387
388
 
388
389
  def unpause_streaming?
389
- return if @stopped
390
- return if @paused.nil?
390
+ return false if @stopped
391
+ return false if @paused.nil?
391
392
 
392
393
  @inventory.count < @inventory.limit * 0.8
393
394
  end
@@ -399,8 +400,8 @@ module Google
399
400
  req.modify_deadline_ack_ids += @inventory.ack_ids
400
401
  req.modify_deadline_seconds += @inventory.ack_ids.map { @subscriber.deadline }
401
402
  req.client_id = @subscriber.service.client_id
402
- req.max_outstanding_messages = @inventory.use_legacy_flow_control ? 0 : @inventory.limit
403
- req.max_outstanding_bytes = @inventory.use_legacy_flow_control ? 0 : @inventory.bytesize
403
+ req.max_outstanding_messages = @inventory.limit
404
+ req.max_outstanding_bytes = @inventory.bytesize
404
405
  end
405
406
  end
406
407
 
@@ -22,7 +22,7 @@ require "retriable"
22
22
  module Google
23
23
  module Cloud
24
24
  module PubSub
25
- class Subscriber
25
+ class MessageListener
26
26
  ##
27
27
  # @private
28
28
  class TimedUnaryBuffer