google-cloud-firestore 2.11.0 → 2.13.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,80 @@
1
+ # Copyright 2023 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
+
16
+ module Google
17
+ module Cloud
18
+ module Firestore
19
+ ##
20
+ # @private Implements 5/5/5 ramp-up via Token Bucket algorithm.
21
+ #
22
+ # 5/5/5 is a ramp up strategy that starts with a budget of 500 operations per
23
+ # second. Additionally, every 5 minutes, the maximum budget can increase by
24
+ # 50%. Thus, at 5:01 into a long bulk-writing process, the maximum budget
25
+ # becomes 750 operations per second. At 10:01, the budget becomes 1,125
26
+ # operations per second.
27
+ #
28
+ class RateLimiter
29
+ DEFAULT_STARTING_MAXIMUM_OPS_PER_SECOND = 500.0
30
+ DEFAULT_PHASE_LENGTH = 300.0
31
+
32
+ attr_reader :bandwidth
33
+
34
+ ##
35
+ # Initialize the object
36
+ def initialize starting_ops: nil, phase_length: nil
37
+ @start_time = time
38
+ @last_fetched = time
39
+ @bandwidth = (starting_ops || DEFAULT_STARTING_MAXIMUM_OPS_PER_SECOND).to_f
40
+ @phase_length = phase_length || DEFAULT_PHASE_LENGTH
41
+ end
42
+
43
+ ##
44
+ # Wait till the number of tokens is available
45
+ # Assumes that the bandwidth is distributed evenly across the entire second.
46
+ #
47
+ # Example - If the limit is 500 qps, then it has been further broken down to 2e+6 nsec
48
+ # per query
49
+ #
50
+ # @return [nil]
51
+ def wait_for_tokens size
52
+ available_time = @last_fetched + (size / @bandwidth)
53
+ waiting_time = [0, available_time - time].max
54
+ sleep waiting_time
55
+ @last_fetched = time
56
+ increase_bandwidth
57
+ end
58
+
59
+ private
60
+
61
+ ##
62
+ # Returns time elapsed since epoch.
63
+ #
64
+ # @return [Float] Float denoting time elapsed since epoch
65
+ def time
66
+ Time.now.to_f
67
+ end
68
+
69
+ ##
70
+ # Increase the bandwidth as per 555 rule
71
+ #
72
+ # @return [nil]
73
+ def increase_bandwidth
74
+ intervals = (time - @start_time) / @phase_length
75
+ @bandwidth = (DEFAULT_STARTING_MAXIMUM_OPS_PER_SECOND * (1.5**intervals.floor)).to_f
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
@@ -186,6 +186,18 @@ module Google
186
186
  )
187
187
  end
188
188
 
189
+ ##
190
+ # Makes the BatchWrite API call. Contains the list of write operations to be processed.
191
+ #
192
+ # @return [::Google::Cloud::Firestore::V1::BatchWriteResponse]
193
+ def batch_write writes
194
+ batch_write_req = {
195
+ database: database_path,
196
+ writes: writes
197
+ }
198
+ firestore.batch_write batch_write_req, call_options(parent: database_path)
199
+ end
200
+
189
201
  def database_path project_id: project, database_id: database
190
202
  # Originally used V1::FirestoreClient.database_root_path until it was removed in #5405.
191
203
  "projects/#{project_id}/databases/#{database_id}"
@@ -16,7 +16,7 @@
16
16
  module Google
17
17
  module Cloud
18
18
  module Firestore
19
- VERSION = "2.11.0".freeze
19
+ VERSION = "2.13.0".freeze
20
20
  end
21
21
  end
22
22
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google-cloud-firestore
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.11.0
4
+ version: 2.13.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Google Inc
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-02-23 00:00:00.000000000 Z
11
+ date: 2023-05-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: google-cloud-core
@@ -228,6 +228,11 @@ files:
228
228
  - lib/google/cloud/firestore/aggregate_query.rb
229
229
  - lib/google/cloud/firestore/aggregate_query_snapshot.rb
230
230
  - lib/google/cloud/firestore/batch.rb
231
+ - lib/google/cloud/firestore/bulk_commit_batch.rb
232
+ - lib/google/cloud/firestore/bulk_writer.rb
233
+ - lib/google/cloud/firestore/bulk_writer_exception.rb
234
+ - lib/google/cloud/firestore/bulk_writer_operation.rb
235
+ - lib/google/cloud/firestore/bulk_writer_scheduler.rb
231
236
  - lib/google/cloud/firestore/client.rb
232
237
  - lib/google/cloud/firestore/collection_group.rb
233
238
  - lib/google/cloud/firestore/collection_reference.rb
@@ -240,13 +245,17 @@ files:
240
245
  - lib/google/cloud/firestore/document_reference.rb
241
246
  - lib/google/cloud/firestore/document_reference/list.rb
242
247
  - lib/google/cloud/firestore/document_snapshot.rb
248
+ - lib/google/cloud/firestore/errors.rb
243
249
  - lib/google/cloud/firestore/field_path.rb
244
250
  - lib/google/cloud/firestore/field_value.rb
251
+ - lib/google/cloud/firestore/filter.rb
245
252
  - lib/google/cloud/firestore/generate.rb
253
+ - lib/google/cloud/firestore/promise/future.rb
246
254
  - lib/google/cloud/firestore/query.rb
247
255
  - lib/google/cloud/firestore/query_listener.rb
248
256
  - lib/google/cloud/firestore/query_partition.rb
249
257
  - lib/google/cloud/firestore/query_snapshot.rb
258
+ - lib/google/cloud/firestore/rate_limiter.rb
250
259
  - lib/google/cloud/firestore/resource_path.rb
251
260
  - lib/google/cloud/firestore/service.rb
252
261
  - lib/google/cloud/firestore/transaction.rb