prefab-cloud-ruby 0.23.1 → 0.23.3

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: 55f7405129237177e62c5dfe267782a09825ef548daa1dc4991ab21a66769455
4
- data.tar.gz: ec4e47e424a6898f31bcba403293de19d8a70d67a951c3d6a830e5cdbb71156e
3
+ metadata.gz: 45234586612606d2efe94b569039c7f7866b948b1cc9ef1d252c3af68b678ac0
4
+ data.tar.gz: c4ca19037512f306fe4fe62a6031d56153a68d549e58a64e3754050af6b91948
5
5
  SHA512:
6
- metadata.gz: 0e78923408d4bf39a2a73c7e9d90c88274b1da106ff148094c2bcba694195d93eab81d373fe36e404740e0266f06b35d928bc21caa10e85c374fbf3667d055af
7
- data.tar.gz: db4052d845d6dbda926579da65af9bececa24c96676b44f0d7af01bfed85e527d4768d22b095867f6ec6ec6d30d8e3975fd6edebf4af4d9ea47d1550a5215fb9
6
+ metadata.gz: 1562416434735eb380f3cc7e159120a43e4f18e22f486020cfe7a35dc0c66f2c10a04104e46120c6f455a70ff7078c35ce16978bb1a111669686971be3a9c432
7
+ data.tar.gz: 4671527a60e7166e3acbeca47bbe211d405ceda58600c621406cf865b0942864d7b2d7c5f71c8fbb7ccc0e020113cf8921f7dcf5a9fe5b7a81b887392ef35460
data/README.md CHANGED
@@ -1,5 +1,6 @@
1
1
  # prefab-cloud-ruby
2
- Ruby Client for Prefab FeatureFlags, Config as a Service: https://www.prefab.cloud
2
+
3
+ Ruby Client for Prefab Feature Flags, Dynamic log levels, and Config as a Service: https://www.prefab.cloud
3
4
 
4
5
  ```ruby
5
6
  client = Prefab::Client.new
@@ -20,25 +21,36 @@ See full documentation https://docs.prefab.cloud/docs/ruby-sdk/ruby
20
21
 
21
22
  ## Supports
22
23
 
23
- * [FeatureFlags](https://www.prefab.cloud/documentation/feature_flags) as a Service
24
- * Millions of individual limits sharing the same policies
25
- * WebUI for tweaking limits & feature flags
26
- * Infinite retention for [deduplication workflows](https://www.prefab.cloud/documentation/once_and_only_once)
24
+ * Feature Flags
25
+ * Dynamic log levels
26
+ * Live Config
27
+ * WebUI for tweaking config, log levels, and feature flags
27
28
 
28
29
  ## Important note about Forking and realtime updates
29
- Many ruby web servers fork. GRPC does not like to be forked. You should manually start gRPC streaming in the on_worker_boot or after_fork hook of your server. See some details on GRPC and forking: https://github.com/grpc/grpc/issues/7951#issuecomment-335998583
30
30
 
31
- ```ruby
31
+ Many ruby web servers fork. GRPC does not like to be forked. See some details on GRPC and forking: https://github.com/grpc/grpc/issues/7951#issuecomment-335998583
32
32
 
33
+ If you're using Puma or Unicorn, you can do the following.
34
+
35
+ ```ruby
33
36
  #config/initializers/prefab.rb
34
37
  $prefab = Prefab::Client.new
35
- Rails.logger = $prefab.log
38
+ $prefab.set_rails_loggers
36
39
  ```
37
40
 
38
41
  ```ruby
39
42
  #puma.rb
40
43
  on_worker_boot do
41
- $prefab.config_client.start_streaming
44
+ $prefab = Prefab::Client.new
45
+ $prefab.set_rails_loggers
46
+ end
47
+ ```
48
+
49
+ ```ruby
50
+ # unicorn.rb
51
+ after_fork do |server, worker|
52
+ $prefab = Prefab::Client.new(options)
53
+ $prefab.set_rails_loggers
42
54
  end
43
55
  ```
44
56
 
@@ -76,5 +88,4 @@ REMOTE_BRANCH=main LOCAL_BRANCH=main bundle exec rake release
76
88
 
77
89
  ## Copyright
78
90
 
79
- Copyright (c) 2023 Jeff Dwyer. See LICENSE.txt for
80
- further details.
91
+ Copyright (c) 2023 Jeff Dwyer. See LICENSE.txt for further details.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.23.1
1
+ 0.23.3
@@ -34,6 +34,8 @@ module Prefab
34
34
  end
35
35
  end
36
36
  str
37
+ rescue StandardError => e
38
+ "Error printing resolved config: #{e.message}"
37
39
  end
38
40
 
39
41
  def raw(key)
@@ -0,0 +1,16 @@
1
+ module Prefab
2
+ class ExponentialBackoff
3
+ def initialize(max_delay:, initial_delay: 2, multiplier: 2)
4
+ @initial_delay = initial_delay
5
+ @max_delay = max_delay
6
+ @multiplier = multiplier
7
+ @delay = initial_delay
8
+ end
9
+
10
+ def call
11
+ delay = @delay
12
+ @delay = [@delay * @multiplier, @max_delay].min
13
+ delay
14
+ end
15
+ end
16
+ end
@@ -14,9 +14,13 @@ module Prefab
14
14
 
15
15
  def initialize(client:, max_paths:, sync_interval:)
16
16
  @max_paths = max_paths
17
- @sync_interval = sync_interval
18
17
  @client = client
19
18
  @start_at = now
19
+ @sync_interval = if sync_interval.is_a?(Numeric)
20
+ proc { sync_interval }
21
+ else
22
+ sync_interval || ExponentialBackoff.new(initial_delay: 8, max_delay: 60 * 10)
23
+ end
20
24
 
21
25
  @pool = Concurrent::ThreadPoolExecutor.new(
22
26
  fallback_policy: :discard,
@@ -78,17 +82,17 @@ module Prefab
78
82
 
79
83
  def start_periodic_sync
80
84
  Thread.new do
81
- log_internal "Initialized log path collector instance_hash=#{@client.instance_hash} max_paths=#{@max_paths} sync_interval=#{@sync_interval}"
85
+ log_internal "Initialized log path collector instance_hash=#{@client.instance_hash} max_paths=#{@max_paths}"
82
86
 
83
87
  loop do
84
- sleep @sync_interval
88
+ sleep @sync_interval.call
85
89
  sync
86
90
  end
87
91
  end
88
92
  end
89
93
 
90
94
  def log_internal(message)
91
- @client.log.log_internal message, 'log_path_collector', nil, ::Logger::INFO
95
+ @client.log.log_internal message, 'log_path_collector', nil, ::Logger::DEBUG
92
96
  end
93
97
 
94
98
  def now
@@ -39,7 +39,6 @@ module Prefab
39
39
  end
40
40
 
41
41
  DEFAULT_MAX_PATHS = 1_000
42
- DEFAULT_SYNC_INTERVAL = 60
43
42
 
44
43
  private def init(
45
44
  api_key: ENV['PREFAB_API_KEY'],
@@ -62,7 +61,7 @@ module Prefab
62
61
  prefab_envs: ENV['PREFAB_ENVS'].nil? ? [] : ENV['PREFAB_ENVS'].split(','),
63
62
  collect_logs: true,
64
63
  collect_max_paths: DEFAULT_MAX_PATHS,
65
- collect_sync_interval: DEFAULT_SYNC_INTERVAL
64
+ collect_sync_interval: nil
66
65
  )
67
66
  @api_key = api_key
68
67
  @logdev = logdev
@@ -71,7 +70,7 @@ module Prefab
71
70
  @namespace = namespace
72
71
  @log_formatter = log_formatter
73
72
  @log_prefix = log_prefix
74
- @prefab_api_url = prefab_api_url
73
+ @prefab_api_url = remove_trailing_slash(prefab_api_url)
75
74
  @prefab_grpc_url = prefab_grpc_url
76
75
  @on_no_default = on_no_default
77
76
  @initialization_timeout_sec = initialization_timeout_sec
@@ -103,5 +102,11 @@ module Prefab
103
102
  def url_for_api_cdn
104
103
  ENV['PREFAB_CDN_URL'] || "#{@prefab_api_url.gsub(/\./, '-')}.global.ssl.fastly.net"
105
104
  end
105
+
106
+ private
107
+
108
+ def remove_trailing_slash(url)
109
+ url.end_with?('/') ? url[0..-2] : url
110
+ end
106
111
  end
107
112
  end
@@ -8,6 +8,7 @@ require 'openssl'
8
8
  require 'ld-eventsource'
9
9
  require 'prefab_pb'
10
10
  require 'prefab/error'
11
+ require 'prefab/exponential_backoff'
11
12
  require 'prefab/errors/initialization_timeout_error'
12
13
  require 'prefab/errors/invalid_api_key_error'
13
14
  require 'prefab/errors/missing_default_error'
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Juwelier::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: prefab-cloud-ruby 0.23.1 ruby lib
5
+ # stub: prefab-cloud-ruby 0.23.3 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "prefab-cloud-ruby".freeze
9
- s.version = "0.23.1"
9
+ s.version = "0.23.3"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib".freeze]
13
13
  s.authors = ["Jeff Dwyer".freeze]
14
- s.date = "2023-03-30"
14
+ s.date = "2023-04-07"
15
15
  s.description = "RateLimits & Config as a service".freeze
16
16
  s.email = "jdwyer@prefab.cloud".freeze
17
17
  s.extra_rdoc_files = [
@@ -45,6 +45,7 @@ Gem::Specification.new do |s|
45
45
  "lib/prefab/errors/initialization_timeout_error.rb",
46
46
  "lib/prefab/errors/invalid_api_key_error.rb",
47
47
  "lib/prefab/errors/missing_default_error.rb",
48
+ "lib/prefab/exponential_backoff.rb",
48
49
  "lib/prefab/feature_flag_client.rb",
49
50
  "lib/prefab/internal_logger.rb",
50
51
  "lib/prefab/local_config_parser.rb",
@@ -71,6 +72,7 @@ Gem::Specification.new do |s|
71
72
  "test/test_config_resolver.rb",
72
73
  "test/test_config_value_unwrapper.rb",
73
74
  "test/test_criteria_evaluator.rb",
75
+ "test/test_exponential_backoff.rb",
74
76
  "test/test_feature_flag_client.rb",
75
77
  "test/test_helper.rb",
76
78
  "test/test_integration.rb",
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+
5
+ class TestExponentialBackoff < Minitest::Test
6
+ def test_backoff
7
+ backoff = Prefab::ExponentialBackoff.new(max_delay: 120)
8
+
9
+ assert_equal 2, backoff.call
10
+ assert_equal 4, backoff.call
11
+ assert_equal 8, backoff.call
12
+ assert_equal 16, backoff.call
13
+ assert_equal 32, backoff.call
14
+ assert_equal 64, backoff.call
15
+ assert_equal 120, backoff.call
16
+ assert_equal 120, backoff.call
17
+ end
18
+ end
@@ -47,8 +47,7 @@ class TestLogPathCollector < Minitest::Test
47
47
  prefab_config_override_dir: 'none',
48
48
  prefab_config_classpath_dir: 'test',
49
49
  prefab_envs: ['unit_tests'],
50
- api_key: '123-development-yourapikey-SDK',
51
- collect_sync_interval: 1000 # we'll trigger sync manually in our test
50
+ api_key: '123-development-yourapikey-SDK'
52
51
  }.merge(overrides))
53
52
 
54
53
  Prefab::Client.new(options)
data/test/test_options.rb CHANGED
@@ -5,6 +5,18 @@ require 'test_helper'
5
5
  class TestOptions < Minitest::Test
6
6
  API_KEY = 'abcdefg'
7
7
 
8
+ def test_prefab_api_url
9
+ assert_equal 'https://api.prefab.cloud', Prefab::Options.new.prefab_api_url
10
+
11
+ with_env 'PREFAB_API_URL', 'https://api.prefab.cloud' do
12
+ assert_equal 'https://api.prefab.cloud', Prefab::Options.new.prefab_api_url
13
+ end
14
+
15
+ with_env 'PREFAB_API_URL', 'https://api.prefab.cloud/' do
16
+ assert_equal 'https://api.prefab.cloud', Prefab::Options.new.prefab_api_url
17
+ end
18
+ end
19
+
8
20
  def test_works_with_named_arguments
9
21
  assert_equal API_KEY, Prefab::Options.new(api_key: API_KEY).api_key
10
22
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prefab-cloud-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.23.1
4
+ version: 0.23.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Dwyer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-03-30 00:00:00.000000000 Z
11
+ date: 2023-04-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -232,6 +232,7 @@ files:
232
232
  - lib/prefab/errors/initialization_timeout_error.rb
233
233
  - lib/prefab/errors/invalid_api_key_error.rb
234
234
  - lib/prefab/errors/missing_default_error.rb
235
+ - lib/prefab/exponential_backoff.rb
235
236
  - lib/prefab/feature_flag_client.rb
236
237
  - lib/prefab/internal_logger.rb
237
238
  - lib/prefab/local_config_parser.rb
@@ -258,6 +259,7 @@ files:
258
259
  - test/test_config_resolver.rb
259
260
  - test/test_config_value_unwrapper.rb
260
261
  - test/test_criteria_evaluator.rb
262
+ - test/test_exponential_backoff.rb
261
263
  - test/test_feature_flag_client.rb
262
264
  - test/test_helper.rb
263
265
  - test/test_integration.rb