google-cloud-pubsub 2.14.0 → 2.15.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c4aa541774d81af56f0d85bea8428b5dfa7f348194faccd34924a0bc60e76eb5
4
- data.tar.gz: 342948cbfc7001c74cc8df748a1fdf0964b4611495c710cd7e8d24a6e25291ae
3
+ metadata.gz: 487a660ea90e29fd8dcde076a9baf33aabefdba1d06267160b56254129b2af1a
4
+ data.tar.gz: 21138b050ced799b7a16cd2048a321d6be6f488457474c313a9976236d4599ab
5
5
  SHA512:
6
- metadata.gz: bf8df4d2a93ee5133e982158e754f5f21f8f5a2e4066430232c139fa80194d11e85657ecb7b12c573d8777b5434e113e82922ce84d2d841f25a51ae67b7587b7
7
- data.tar.gz: 1af4583788b6e79a93b854bd941fed593407afd6c0be1a771a2016a78629a88853f994e0e0e9188f1dc7c56393d1d7b75acf97ca1337cf3b3cd254df3f4e40c6
6
+ metadata.gz: 6a4aded9ee5ca61d0496ec669d6fdf1ac842eab912409cf2c2b2b52059d0151cfd7ff847c0d64b0be32946fb5484d8bfaf9499f8e061cee386868969179e11fd
7
+ data.tar.gz: 2964d7408a959048069ee489f22b72427ba6713453518b0a79cb660fef25ca38951f9d5ad5d903494757ed325932238309ff271e6f460718f6b337e294738eb5
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Release History
2
2
 
3
+ ### 2.15.0 (2023-01-22)
4
+
5
+ #### Features
6
+
7
+ * support publisher compression ([#19910](https://github.com/googleapis/google-cloud-ruby/issues/19910))
8
+
3
9
  ### 2.14.0 (2023-01-12)
4
10
 
5
11
  #### Features
@@ -255,6 +255,10 @@ module Google
255
255
  end
256
256
  end
257
257
 
258
+ def total_message_bytes
259
+ @total_message_bytes
260
+ end
261
+
258
262
  protected
259
263
 
260
264
  def items_add msg, callback
@@ -288,10 +292,6 @@ module Google
288
292
  @items.count
289
293
  end
290
294
 
291
- def total_message_bytes
292
- @total_message_bytes
293
- end
294
-
295
295
  Item = Struct.new :msg, :callback do
296
296
  def bytesize
297
297
  msg.to_proto.bytesize
@@ -70,7 +70,8 @@ module Google
70
70
  ##
71
71
  # @private Implementation accessors
72
72
  attr_reader :service, :batch, :publish_thread_pool,
73
- :callback_thread_pool, :flow_controller
73
+ :callback_thread_pool, :flow_controller,
74
+ :compress, :compression_bytes_threshold
74
75
 
75
76
  ##
76
77
  # @private Create a new instance of the object.
@@ -80,7 +81,9 @@ module Google
80
81
  max_messages: 100,
81
82
  interval: 0.01,
82
83
  threads: {},
83
- flow_control: {}
84
+ flow_control: {},
85
+ compress: nil,
86
+ compression_bytes_threshold: nil
84
87
  # init MonitorMixin
85
88
  super()
86
89
  @topic_name = service.topic_path topic_name
@@ -105,6 +108,9 @@ module Google
105
108
  @cond = new_cond
106
109
  @flow_controller = FlowController.new(**@flow_control)
107
110
  @thread = Thread.new { run_background }
111
+ @compress = compress || Google::Cloud::PubSub::DEFAULT_COMPRESS
112
+ @compression_bytes_threshold = compression_bytes_threshold ||
113
+ Google::Cloud::PubSub::DEFAULT_COMPRESSION_BYTES_THRESHOLD
108
114
  end
109
115
 
110
116
  ##
@@ -370,7 +376,9 @@ module Google
370
376
  items = batch.rebalance!
371
377
 
372
378
  unless items.empty?
373
- grpc = @service.publish topic_name, items.map(&:msg)
379
+ grpc = @service.publish topic_name,
380
+ items.map(&:msg),
381
+ compress: compress && batch.total_message_bytes >= compression_bytes_threshold
374
382
  items.zip Array(grpc.message_ids) do |item, id|
375
383
  @flow_controller.release item.bytesize
376
384
  next unless item.callback
@@ -39,11 +39,32 @@ module Google
39
39
  # @private The messages to publish
40
40
  attr_reader :messages
41
41
 
42
+ ##
43
+ # @private Enables publisher compression
44
+ attr_reader :compress
45
+
46
+ ##
47
+ # @private The threshold bytes size for compression
48
+ attr_reader :compression_bytes_threshold
49
+
50
+ ##
51
+ # @private The total bytes size of messages data.
52
+ attr_reader :total_message_bytes
53
+
42
54
  ##
43
55
  # @private Create a new instance of the object.
44
- def initialize data, attributes, ordering_key, extra_attrs
56
+ def initialize data,
57
+ attributes,
58
+ ordering_key,
59
+ extra_attrs,
60
+ compress: nil,
61
+ compression_bytes_threshold: nil
45
62
  @messages = []
46
63
  @mode = :batch
64
+ @compress = compress || Google::Cloud::PubSub::DEFAULT_COMPRESS
65
+ @compression_bytes_threshold = compression_bytes_threshold ||
66
+ Google::Cloud::PubSub::DEFAULT_COMPRESSION_BYTES_THRESHOLD
67
+ @total_message_bytes = 0
47
68
  return if data.nil?
48
69
  @mode = :single
49
70
  publish data, attributes, ordering_key: ordering_key, **extra_attrs
@@ -74,6 +95,7 @@ module Google
74
95
  #
75
96
  def publish data, attributes = nil, ordering_key: nil, **extra_attrs
76
97
  msg = Convert.pubsub_message data, attributes, ordering_key, extra_attrs
98
+ @total_message_bytes += msg.data.bytesize + 2
77
99
  @messages << msg
78
100
  end
79
101
 
@@ -92,6 +114,15 @@ module Google
92
114
  msgs
93
115
  end
94
116
  end
117
+
118
+ ##
119
+ # @private Call the publish API with arrays of data and attrs.
120
+ def publish_batch_messages topic_name, service
121
+ grpc = service.publish topic_name,
122
+ messages,
123
+ compress: compress && total_message_bytes >= compression_bytes_threshold
124
+ to_gcloud_messages Array(grpc.message_ids)
125
+ end
95
126
  end
96
127
  end
97
128
 
@@ -24,6 +24,9 @@ require "google/cloud/pubsub/snapshot"
24
24
  module Google
25
25
  module Cloud
26
26
  module PubSub
27
+ DEFAULT_COMPRESS = false
28
+ DEFAULT_COMPRESSION_BYTES_THRESHOLD = 240
29
+
27
30
  ##
28
31
  # # Project
29
32
  #
@@ -103,6 +106,9 @@ module Google
103
106
  # * `:threads` (Hash) The number of threads to create to handle concurrent calls by the publisher:
104
107
  # * `:publish` (Integer) The number of threads used to publish messages. Default is 2.
105
108
  # * `:callback` (Integer) The number of threads to handle the published messages' callbacks. Default is 4.
109
+ # * `:compress` (Boolean) The flag that enables publisher compression. Default is false
110
+ # * `:compression_bytes_threshold` (Integer) The number of bytes above which compress should be enabled.
111
+ # Default is 240.
106
112
  # * `:flow_control` (Hash) The client flow control settings for message publishing:
107
113
  # * `:message_limit` (Integer) The maximum number of messages allowed to wait to be published. Default is
108
114
  # `10 * max_messages`.
@@ -211,11 +217,13 @@ module Google
211
217
  # the batch is published. Default is 0.01.
212
218
  # * `:threads` (Hash) The number of threads to create to handle concurrent
213
219
  # calls by the publisher:
214
- #
215
220
  # * `:publish` (Integer) The number of threads used to publish messages.
216
221
  # Default is 2.
217
222
  # * `:callback` (Integer) The number of threads to handle the published
218
223
  # messages' callbacks. Default is 4.
224
+ # * `:compress` (Boolean) The flag that enables publisher compression. Default is false
225
+ # * `:compression_bytes_threshold` (Integer) The number of bytes above which compress should be enabled.
226
+ # Default is 240.
219
227
  # * `:flow_control` (Hash) The client flow control settings for message publishing:
220
228
  # * `:message_limit` (Integer) The maximum number of messages allowed to wait to be published. Default is
221
229
  # `10 * max_messages`.
@@ -642,13 +650,6 @@ module Google
642
650
  def ensure_service!
643
651
  raise "Must have active connection to service" unless service
644
652
  end
645
-
646
- ##
647
- # Call the publish API with arrays of data data and attrs.
648
- def publish_batch_messages topic_name, batch
649
- grpc = service.publish topic_name, batch.messages
650
- batch.to_gcloud_messages Array(grpc.message_ids)
651
- end
652
653
  end
653
654
  end
654
655
 
@@ -173,8 +173,10 @@ module Google
173
173
  # Raises GRPC status code 5 if the topic does not exist.
174
174
  # The messages parameter is an array of arrays.
175
175
  # The first element is the data, second is attributes hash.
176
- def publish topic, messages
177
- publisher.publish topic: topic_path(topic), messages: messages
176
+ def publish topic, messages, compress: false
177
+ request = { topic: topic_path(topic), messages: messages }
178
+ compress_options = ::Gapic::CallOptions.new metadata: { "grpc-internal-encoding-request": "gzip" }
179
+ compress ? (publisher.publish request, compress_options) : (publisher.publish request)
178
180
  end
179
181
 
180
182
  ##
@@ -676,12 +676,19 @@ module Google
676
676
  # topic.publish "task completed",
677
677
  # ordering_key: "task-key"
678
678
  #
679
- def publish data = nil, attributes = nil, ordering_key: nil, **extra_attrs, &block
679
+ def publish data = nil, attributes = nil, ordering_key: nil, compress: nil, compression_bytes_threshold: nil,
680
+ **extra_attrs, &block
680
681
  ensure_service!
681
- batch = BatchPublisher.new data, attributes, ordering_key, extra_attrs
682
+ batch = BatchPublisher.new data,
683
+ attributes,
684
+ ordering_key,
685
+ extra_attrs,
686
+ compress: compress,
687
+ compression_bytes_threshold: compression_bytes_threshold
688
+
682
689
  block&.call batch
683
690
  return nil if batch.messages.count.zero?
684
- publish_batch_messages batch
691
+ batch.publish_batch_messages name, service
685
692
  end
686
693
 
687
694
  ##
@@ -1085,13 +1092,6 @@ module Google
1085
1092
  ensure_service!
1086
1093
  reload! if reference?
1087
1094
  end
1088
-
1089
- ##
1090
- # Call the publish API with arrays of data data and attrs.
1091
- def publish_batch_messages batch
1092
- grpc = service.publish name, batch.messages
1093
- batch.to_gcloud_messages Array(grpc.message_ids)
1094
- end
1095
1095
  end
1096
1096
  end
1097
1097
 
@@ -16,7 +16,7 @@
16
16
  module Google
17
17
  module Cloud
18
18
  module PubSub
19
- VERSION = "2.14.0".freeze
19
+ VERSION = "2.15.0".freeze
20
20
  end
21
21
 
22
22
  Pubsub = PubSub unless const_defined? :Pubsub
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google-cloud-pubsub
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.14.0
4
+ version: 2.15.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Moore
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2023-01-12 00:00:00.000000000 Z
12
+ date: 2023-01-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: concurrent-ruby