google-cloud-pubsub 0.33.2 → 0.34.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/AUTHENTICATION.md +4 -4
  3. data/CHANGELOG.md +45 -0
  4. data/EMULATOR.md +2 -2
  5. data/OVERVIEW.md +81 -43
  6. data/lib/google-cloud-pubsub.rb +10 -7
  7. data/lib/google/cloud/pubsub.rb +38 -21
  8. data/lib/google/cloud/pubsub/async_publisher.rb +8 -6
  9. data/lib/google/cloud/pubsub/batch_publisher.rb +7 -5
  10. data/lib/google/cloud/pubsub/convert.rb +5 -3
  11. data/lib/google/cloud/pubsub/credentials.rb +6 -4
  12. data/lib/google/cloud/pubsub/message.rb +9 -6
  13. data/lib/google/cloud/pubsub/policy.rb +12 -10
  14. data/lib/google/cloud/pubsub/project.rb +30 -28
  15. data/lib/google/cloud/pubsub/publish_result.rb +3 -1
  16. data/lib/google/cloud/pubsub/received_message.rb +11 -10
  17. data/lib/google/cloud/pubsub/service.rb +47 -37
  18. data/lib/google/cloud/pubsub/snapshot.rb +11 -9
  19. data/lib/google/cloud/pubsub/snapshot/list.rb +10 -8
  20. data/lib/google/cloud/pubsub/subscriber.rb +32 -6
  21. data/lib/google/cloud/pubsub/subscriber/enumerator_queue.rb +3 -1
  22. data/lib/google/cloud/pubsub/subscriber/inventory.rb +4 -2
  23. data/lib/google/cloud/pubsub/subscriber/stream.rb +23 -43
  24. data/lib/google/cloud/pubsub/subscriber/timed_unary_buffer.rb +221 -0
  25. data/lib/google/cloud/pubsub/subscription.rb +157 -80
  26. data/lib/google/cloud/pubsub/subscription/list.rb +12 -10
  27. data/lib/google/cloud/pubsub/topic.rb +79 -51
  28. data/lib/google/cloud/pubsub/topic/list.rb +10 -8
  29. data/lib/google/cloud/pubsub/v1/credentials.rb +4 -2
  30. data/lib/google/cloud/pubsub/v1/doc/google/iam/v1/iam_policy.rb +1 -43
  31. data/lib/google/cloud/pubsub/v1/doc/google/iam/v1/policy.rb +1 -108
  32. data/lib/google/cloud/pubsub/v1/doc/google/protobuf/duration.rb +1 -1
  33. data/lib/google/cloud/pubsub/v1/doc/google/protobuf/empty.rb +1 -1
  34. data/lib/google/cloud/pubsub/v1/doc/google/protobuf/field_mask.rb +1 -1
  35. data/lib/google/cloud/pubsub/v1/doc/google/protobuf/timestamp.rb +1 -1
  36. data/lib/google/cloud/pubsub/v1/doc/google/pubsub/v1/pubsub.rb +107 -61
  37. data/lib/google/cloud/pubsub/v1/publisher_client.rb +58 -55
  38. data/lib/google/cloud/pubsub/v1/publisher_client_config.json +0 -4
  39. data/lib/google/cloud/pubsub/v1/subscriber_client.rb +177 -128
  40. data/lib/google/cloud/pubsub/v1/subscriber_client_config.json +0 -4
  41. data/lib/google/cloud/pubsub/version.rb +4 -2
  42. data/lib/google/pubsub/v1/pubsub_pb.rb +48 -40
  43. data/lib/google/pubsub/v1/pubsub_services_pb.rb +201 -161
  44. metadata +6 -7
  45. data/lib/google/cloud/pubsub/subscriber/async_stream_pusher.rb +0 -223
  46. data/lib/google/cloud/pubsub/subscriber/async_unary_pusher.rb +0 -271
@@ -18,7 +18,7 @@ require "google/cloud/pubsub/snapshot/list"
18
18
 
19
19
  module Google
20
20
  module Cloud
21
- module Pubsub
21
+ module PubSub
22
22
  ##
23
23
  # # Snapshot
24
24
  #
@@ -35,7 +35,7 @@ module Google
35
35
  # @example
36
36
  # require "google/cloud/pubsub"
37
37
  #
38
- # pubsub = Google::Cloud::Pubsub.new
38
+ # pubsub = Google::Cloud::PubSub.new
39
39
  # sub = pubsub.subscription "my-sub"
40
40
  #
41
41
  # snapshot = sub.create_snapshot "my-snapshot"
@@ -47,14 +47,14 @@ module Google
47
47
  attr_accessor :service
48
48
 
49
49
  ##
50
- # @private The gRPC Google::Pubsub::V1::Snapshot object.
50
+ # @private The gRPC Google::Cloud::PubSub::V1::Snapshot object.
51
51
  attr_accessor :grpc
52
52
 
53
53
  ##
54
54
  # @private Create an empty {Snapshot} object.
55
55
  def initialize
56
56
  @service = nil
57
- @grpc = Google::Pubsub::V1::Snapshot.new
57
+ @grpc = Google::Cloud::PubSub::V1::Snapshot.new
58
58
  end
59
59
 
60
60
  ##
@@ -72,14 +72,14 @@ module Google
72
72
  # @example
73
73
  # require "google/cloud/pubsub"
74
74
  #
75
- # pubsub = Google::Cloud::Pubsub.new
75
+ # pubsub = Google::Cloud::PubSub.new
76
76
  # sub = pubsub.subscription "my-sub"
77
77
  #
78
78
  # snapshot = sub.create_snapshot "my-snapshot"
79
79
  # snapshot.topic.name #=> "projects/my-project/topics/my-topic"
80
80
  #
81
81
  def topic
82
- Topic.new_lazy @grpc.topic, service
82
+ Topic.from_name @grpc.topic, service
83
83
  end
84
84
 
85
85
  ##
@@ -99,7 +99,7 @@ module Google
99
99
  # @example
100
100
  # require "google/cloud/pubsub"
101
101
  #
102
- # pubsub = Google::Cloud::Pubsub.new
102
+ # pubsub = Google::Cloud::PubSub.new
103
103
  # sub = pubsub.subscription "my-sub"
104
104
  #
105
105
  # snapshot = sub.create_snapshot "my-snapshot"
@@ -157,7 +157,7 @@ module Google
157
157
  # @example
158
158
  # require "google/cloud/pubsub"
159
159
  #
160
- # pubsub = Google::Cloud::Pubsub.new
160
+ # pubsub = Google::Cloud::PubSub.new
161
161
  #
162
162
  # pubsub.snapshots.each do |snapshot|
163
163
  # snapshot.delete
@@ -170,7 +170,7 @@ module Google
170
170
  end
171
171
 
172
172
  ##
173
- # @private New Snapshot from a Google::Pubsub::V1::Snapshot
173
+ # @private New Snapshot from a Google::Cloud::PubSub::V1::Snapshot
174
174
  # object.
175
175
  def self.from_grpc grpc, service
176
176
  new.tap do |f|
@@ -196,5 +196,7 @@ module Google
196
196
  end
197
197
  end
198
198
  end
199
+
200
+ Pubsub = PubSub unless const_defined? :Pubsub
199
201
  end
200
202
  end
@@ -17,7 +17,7 @@ require "delegate"
17
17
 
18
18
  module Google
19
19
  module Cloud
20
- module Pubsub
20
+ module PubSub
21
21
  class Snapshot
22
22
  ##
23
23
  # Snapshot::List is a special case Array with additional values.
@@ -25,7 +25,7 @@ module Google
25
25
  ##
26
26
  # If not empty, indicates that there are more snapshots
27
27
  # that match the request and this value should be passed to
28
- # the next {Google::Cloud::Pubsub::Project#snapshots} to continue.
28
+ # the next {Google::Cloud::PubSub::Project#snapshots} to continue.
29
29
  attr_accessor :token
30
30
 
31
31
  ##
@@ -45,7 +45,7 @@ module Google
45
45
  # @example
46
46
  # require "google/cloud/pubsub"
47
47
  #
48
- # pubsub = Google::Cloud::Pubsub.new
48
+ # pubsub = Google::Cloud::PubSub.new
49
49
  #
50
50
  # snapshots = pubsub.snapshots
51
51
  # if snapshots.next?
@@ -64,7 +64,7 @@ module Google
64
64
  # @example
65
65
  # require "google/cloud/pubsub"
66
66
  #
67
- # pubsub = Google::Cloud::Pubsub.new
67
+ # pubsub = Google::Cloud::PubSub.new
68
68
  #
69
69
  # snapshots = pubsub.snapshots
70
70
  # if snapshots.next?
@@ -98,7 +98,7 @@ module Google
98
98
  # @example Iterating each snapshot by passing a block:
99
99
  # require "google/cloud/pubsub"
100
100
  #
101
- # pubsub = Google::Cloud::Pubsub.new
101
+ # pubsub = Google::Cloud::PubSub.new
102
102
  #
103
103
  # snapshots = pubsub.snapshots
104
104
  # snapshots.all do |snapshot|
@@ -108,7 +108,7 @@ module Google
108
108
  # @example Using the enumerator by not passing a block:
109
109
  # require "google/cloud/pubsub"
110
110
  #
111
- # pubsub = Google::Cloud::Pubsub.new
111
+ # pubsub = Google::Cloud::PubSub.new
112
112
  #
113
113
  # snapshots = pubsub.snapshots
114
114
  # all_names = snapshots.all.map do |snapshot|
@@ -118,7 +118,7 @@ module Google
118
118
  # @example Limit the number of API calls made:
119
119
  # require "google/cloud/pubsub"
120
120
  #
121
- # pubsub = Google::Cloud::Pubsub.new
121
+ # pubsub = Google::Cloud::PubSub.new
122
122
  #
123
123
  # snapshots = pubsub.snapshots
124
124
  # snapshots.all(request_limit: 10) do |snapshot|
@@ -144,7 +144,7 @@ module Google
144
144
 
145
145
  ##
146
146
  # @private New Snapshots::List from a
147
- # Google::Pubsub::V1::ListSnapshotsRequest object.
147
+ # Google::Cloud::PubSub::V1::ListSnapshotsRequest object.
148
148
  def self.from_grpc grpc_list, service, max = nil
149
149
  subs = new(Array(grpc_list.snapshots).map do |grpc|
150
150
  Snapshot.from_grpc grpc, service
@@ -174,5 +174,7 @@ module Google
174
174
  end
175
175
  end
176
176
  end
177
+
178
+ Pubsub = PubSub unless const_defined? :Pubsub
177
179
  end
178
180
  end
@@ -15,19 +15,20 @@
15
15
 
16
16
  require "google/cloud/pubsub/service"
17
17
  require "google/cloud/pubsub/subscriber/stream"
18
+ require "google/cloud/pubsub/subscriber/timed_unary_buffer"
18
19
  require "monitor"
19
20
 
20
21
  module Google
21
22
  module Cloud
22
- module Pubsub
23
+ module PubSub
23
24
  ##
24
25
  # Subscriber object used to stream and process messages from a
25
- # Subscription. See {Google::Cloud::Pubsub::Subscription#listen}
26
+ # Subscription. See {Google::Cloud::PubSub::Subscription#listen}
26
27
  #
27
28
  # @example
28
29
  # require "google/cloud/pubsub"
29
30
  #
30
- # pubsub = Google::Cloud::Pubsub.new
31
+ # pubsub = Google::Cloud::PubSub.new
31
32
  #
32
33
  # sub = pubsub.subscription "my-topic-sub"
33
34
  #
@@ -68,7 +69,8 @@ module Google
68
69
 
69
70
  ##
70
71
  # @private Implementation attributes.
71
- attr_reader :stream_inventory, :stream_pool, :thread_pool, :service
72
+ attr_reader :stream_inventory, :stream_pool, :thread_pool, :buffer,
73
+ :service
72
74
 
73
75
  ##
74
76
  # @private Create an empty {Subscriber} object.
@@ -94,6 +96,8 @@ module Google
94
96
  end
95
97
  @stream_pool = stream_pool.map(&:value)
96
98
 
99
+ @buffer = TimedUnaryBuffer.new self
100
+
97
101
  super() # to init MonitorMixin
98
102
  end
99
103
 
@@ -107,6 +111,8 @@ module Google
107
111
  @started = true
108
112
  @stopped = false
109
113
 
114
+ # Start the buffer before the streams are all started
115
+ @buffer.start
110
116
  @stream_pool.map do |stream|
111
117
  Thread.new { stream.start }
112
118
  end
@@ -133,6 +139,8 @@ module Google
133
139
  end
134
140
  end
135
141
  stop_pool.map(&:join)
142
+ # Stop the buffer after the streams are all stopped
143
+ synchronize { @buffer.stop }
136
144
 
137
145
  self
138
146
  end
@@ -185,7 +193,7 @@ module Google
185
193
  # @example
186
194
  # require "google/cloud/pubsub"
187
195
  #
188
- # pubsub = Google::Cloud::Pubsub.new
196
+ # pubsub = Google::Cloud::PubSub.new
189
197
  #
190
198
  # sub = pubsub.subscription "my-topic-sub"
191
199
  #
@@ -224,7 +232,7 @@ module Google
224
232
  # @example
225
233
  # require "google/cloud/pubsub"
226
234
  #
227
- # pubsub = Google::Cloud::Pubsub.new
235
+ # pubsub = Google::Cloud::PubSub.new
228
236
  #
229
237
  # sub = pubsub.subscription "my-topic-sub"
230
238
  #
@@ -252,6 +260,7 @@ module Google
252
260
  @last_error = error
253
261
  @error_callbacks
254
262
  end
263
+ error_callbacks = default_error_callbacks if error_callbacks.empty?
255
264
  error_callbacks.each { |error_callback| error_callback.call error }
256
265
  end
257
266
 
@@ -267,7 +276,24 @@ module Google
267
276
  def inspect
268
277
  "#<#{self.class.name} #{self}>"
269
278
  end
279
+
280
+ protected
281
+
282
+ def default_error_callbacks
283
+ # This is memoized to reduce calls to the configuration.
284
+ @default_error_callbacks ||= begin
285
+ error_callback = Google::Cloud::PubSub.configuration.on_error
286
+ error_callback ||= Google::Cloud.configure.on_error
287
+ if error_callback
288
+ [error_callback]
289
+ else
290
+ []
291
+ end
292
+ end
293
+ end
270
294
  end
271
295
  end
296
+
297
+ Pubsub = PubSub unless const_defined? :Pubsub
272
298
  end
273
299
  end
@@ -15,7 +15,7 @@
15
15
 
16
16
  module Google
17
17
  module Cloud
18
- module Pubsub
18
+ module PubSub
19
19
  class Subscriber
20
20
  # @private
21
21
  class EnumeratorQueue
@@ -48,5 +48,7 @@ module Google
48
48
  end
49
49
  end
50
50
  end
51
+
52
+ Pubsub = PubSub unless const_defined? :Pubsub
51
53
  end
52
54
  end
@@ -17,7 +17,7 @@ require "monitor"
17
17
 
18
18
  module Google
19
19
  module Cloud
20
- module Pubsub
20
+ module PubSub
21
21
  class Subscriber
22
22
  ##
23
23
  # @private
@@ -114,7 +114,7 @@ module Google
114
114
  unless delay_gap.positive?
115
115
  delay_target = nil
116
116
  delay_gap = nil # wait until broadcast
117
- stream.delay_inventory!
117
+ stream.renew_lease!
118
118
  end
119
119
 
120
120
  @wait_cond.wait delay_gap
@@ -132,5 +132,7 @@ module Google
132
132
  end
133
133
  end
134
134
  end
135
+
136
+ Pubsub = PubSub unless const_defined? :Pubsub
135
137
  end
136
138
  end
@@ -13,7 +13,6 @@
13
13
  # limitations under the License.
14
14
 
15
15
 
16
- require "google/cloud/pubsub/subscriber/async_unary_pusher"
17
16
  require "google/cloud/pubsub/subscriber/enumerator_queue"
18
17
  require "google/cloud/pubsub/subscriber/inventory"
19
18
  require "google/cloud/pubsub/service"
@@ -23,7 +22,7 @@ require "concurrent"
23
22
 
24
23
  module Google
25
24
  module Cloud
26
- module Pubsub
25
+ module PubSub
27
26
  class Subscriber
28
27
  ##
29
28
  # @private
@@ -32,7 +31,7 @@ module Google
32
31
 
33
32
  ##
34
33
  # @private Implementation attributes.
35
- attr_reader :callback_thread_pool, :push_thread_pool
34
+ attr_reader :callback_thread_pool
36
35
 
37
36
  ##
38
37
  # Subscriber attributes.
@@ -48,18 +47,16 @@ module Google
48
47
  @paused = nil
49
48
  @pause_cond = new_cond
50
49
 
51
- @inventory = Inventory.new self, subscriber.stream_inventory
50
+ @inventory = Inventory.new self, @subscriber.stream_inventory
52
51
  @callback_thread_pool = Concurrent::FixedThreadPool.new \
53
- subscriber.callback_threads
54
- @push_thread_pool = Concurrent::FixedThreadPool.new \
55
- subscriber.push_threads
52
+ @subscriber.callback_threads
56
53
 
57
54
  @stream_keepalive_task = Concurrent::TimerTask.new(
58
55
  execution_interval: 30
59
56
  ) do
60
57
  # push empty request every 30 seconds to keep stream alive
61
58
  unless inventory.empty?
62
- push Google::Pubsub::V1::StreamingPullRequest.new
59
+ push Google::Cloud::PubSub::V1::StreamingPullRequest.new
63
60
  end
64
61
  end.execute
65
62
 
@@ -96,12 +93,6 @@ module Google
96
93
 
97
94
  # Once all the callbacks are stopped, we can stop the inventory.
98
95
  @inventory.stop
99
-
100
- # Stop the publisher and send the final batch of changes.
101
- @async_pusher.stop if @async_pusher # will push current batch
102
-
103
- # Stop the push thread pool now that the pusher is stopped.
104
- @push_thread_pool.shutdown
105
96
  end
106
97
 
107
98
  self
@@ -116,13 +107,6 @@ module Google
116
107
  end
117
108
 
118
109
  def wait!
119
- synchronize do
120
- # # Wait for the push thread pool to finish pushing all remaining
121
- # changes. Do not wait indefinitely.
122
- @push_thread_pool.wait_for_termination 60
123
- @push_thread_pool.kill if @push_thread_pool.shuttingdown?
124
- end
125
-
126
110
  self
127
111
  end
128
112
 
@@ -133,9 +117,8 @@ module Google
133
117
  return true if ack_ids.empty?
134
118
 
135
119
  synchronize do
136
- @async_pusher ||= AsyncUnaryPusher.new(self).start
137
- @async_pusher.acknowledge ack_ids
138
120
  @inventory.remove ack_ids
121
+ @subscriber.buffer.acknowledge ack_ids
139
122
  unpause_streaming!
140
123
  end
141
124
 
@@ -149,19 +132,13 @@ module Google
149
132
  return true if mod_ack_ids.empty?
150
133
 
151
134
  synchronize do
152
- @async_pusher ||= AsyncUnaryPusher.new(self).start
153
- @async_pusher.delay deadline, mod_ack_ids
154
135
  @inventory.remove mod_ack_ids
136
+ @subscriber.buffer.modify_ack_deadline deadline, mod_ack_ids
155
137
  unpause_streaming!
156
138
  end
157
139
 
158
140
  true
159
141
  end
160
- alias delay modify_ack_deadline
161
-
162
- def async_pusher
163
- synchronize { @async_pusher }
164
- end
165
142
 
166
143
  def push request
167
144
  synchronize { @request_queue.push request }
@@ -173,12 +150,13 @@ module Google
173
150
 
174
151
  ##
175
152
  # @private
176
- def delay_inventory!
153
+ def renew_lease!
177
154
  synchronize do
178
155
  return true if @inventory.empty?
179
156
 
180
- @async_pusher ||= AsyncUnaryPusher.new(self).start
181
- @async_pusher.delay subscriber.deadline, @inventory.ack_ids
157
+ @subscriber.buffer.renew_lease @subscriber.deadline,
158
+ @inventory.ack_ids
159
+ unpause_streaming!
182
160
  end
183
161
 
184
162
  true
@@ -214,7 +192,7 @@ module Google
214
192
  @request_queue = EnumeratorQueue.new self
215
193
  @request_queue.push initial_input_request
216
194
  old_queue.each { |obj| @request_queue.push obj }
217
- enum = subscriber.service.streaming_pull @request_queue.each
195
+ enum = @subscriber.service.streaming_pull @request_queue.each
218
196
 
219
197
  @stopped = nil
220
198
  @paused = nil
@@ -239,8 +217,8 @@ module Google
239
217
 
240
218
  synchronize do
241
219
  # Create receipt of received messages reception
242
- @async_pusher ||= AsyncUnaryPusher.new(self).start
243
- @async_pusher.delay subscriber.deadline, received_ack_ids
220
+ @subscriber.buffer.modify_ack_deadline @subscriber.deadline,
221
+ received_ack_ids
244
222
 
245
223
  # Add received messages to inventory
246
224
  @inventory.add received_ack_ids
@@ -276,7 +254,7 @@ module Google
276
254
  retry
277
255
  rescue StandardError => e
278
256
  synchronize do
279
- subscriber.error! e
257
+ @subscriber.error! e
280
258
  start_streaming! unless @stopped
281
259
  end
282
260
 
@@ -290,9 +268,9 @@ module Google
290
268
 
291
269
  Concurrent::Future.new(executor: callback_thread_pool) do
292
270
  begin
293
- subscriber.callback.call rec_msg
271
+ @subscriber.callback.call rec_msg
294
272
  rescue StandardError => callback_error
295
- subscriber.error! callback_error
273
+ @subscriber.error! callback_error
296
274
  end
297
275
  end.execute
298
276
  end
@@ -339,12 +317,12 @@ module Google
339
317
  end
340
318
 
341
319
  def initial_input_request
342
- Google::Pubsub::V1::StreamingPullRequest.new.tap do |req|
343
- req.subscription = subscriber.subscription_name
344
- req.stream_ack_deadline_seconds = subscriber.deadline
320
+ Google::Cloud::PubSub::V1::StreamingPullRequest.new.tap do |req|
321
+ req.subscription = @subscriber.subscription_name
322
+ req.stream_ack_deadline_seconds = @subscriber.deadline
345
323
  req.modify_deadline_ack_ids += @inventory.ack_ids
346
324
  req.modify_deadline_seconds += \
347
- @inventory.ack_ids.map { subscriber.deadline }
325
+ @inventory.ack_ids.map { @subscriber.deadline }
348
326
  end
349
327
  end
350
328
 
@@ -368,5 +346,7 @@ module Google
368
346
  end
369
347
  end
370
348
  end
349
+
350
+ Pubsub = PubSub unless const_defined? :Pubsub
371
351
  end
372
352
  end