unleash 5.1.1 → 6.0.5.pre
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +93 -117
- data/lib/unleash/client.rb +21 -22
- data/lib/unleash/configuration.rb +2 -2
- data/lib/unleash/context.rb +35 -9
- data/lib/unleash/metrics_reporter.rb +12 -26
- data/lib/unleash/strategies.rb +14 -73
- data/lib/unleash/toggle_fetcher.rb +22 -56
- data/lib/unleash/variant.rb +6 -0
- data/lib/unleash/version.rb +1 -1
- data/lib/unleash.rb +1 -1
- data/unleash-client.gemspec +1 -0
- data/v6_MIGRATION_GUIDE.md +21 -0
- metadata +21 -22
- data/lib/unleash/activation_strategy.rb +0 -44
- data/lib/unleash/constraint.rb +0 -117
- data/lib/unleash/feature_toggle.rb +0 -253
- data/lib/unleash/metrics.rb +0 -41
- data/lib/unleash/strategy/application_hostname.rb +0 -26
- data/lib/unleash/strategy/base.rb +0 -16
- data/lib/unleash/strategy/default.rb +0 -13
- data/lib/unleash/strategy/flexible_rollout.rb +0 -64
- data/lib/unleash/strategy/gradual_rollout_random.rb +0 -24
- data/lib/unleash/strategy/gradual_rollout_sessionid.rb +0 -21
- data/lib/unleash/strategy/gradual_rollout_userid.rb +0 -21
- data/lib/unleash/strategy/remote_address.rb +0 -36
- data/lib/unleash/strategy/user_with_id.rb +0 -20
- data/lib/unleash/strategy/util.rb +0 -17
- data/lib/unleash/variant_definition.rb +0 -26
- data/lib/unleash/variant_override.rb +0 -44
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 106fa4a383a96648b0f8a8a4c22da588264773fc0a2d1f52fb74b0e1d342406f
|
4
|
+
data.tar.gz: 83a385a2bd95e7202f5aae99250201cd276a855658f7b0c82d2fe246647ab9c4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 44d54a5014e508f6cf14231024d47005ae832b63d268b73bc99ddcf9ee296b2df724f26160f63c8d15de78759df385f86372b8c60d2d5d7a5a6789ee50081406
|
7
|
+
data.tar.gz: 753dc9fa7ef2087cd8784ffb2310d3095d5456ba94da42bb1b4b7718bc878783b9fe4689a8dd774b1c97b6d9fd7a3e4c57f640fc3a17888ca423233233501493
|
data/CHANGELOG.md
CHANGED
@@ -13,6 +13,12 @@ Note: These changes are not considered notable:
|
|
13
13
|
|
14
14
|
## [Unreleased]
|
15
15
|
|
16
|
+
## [6.0.0.pre] - 2024-09-25
|
17
|
+
#### Changed
|
18
|
+
- No longer possible to override built in strategies with custom strategies (#152)
|
19
|
+
- No longer possible to access built in strategy's objects, these are now native code (#152)
|
20
|
+
- Core of the SDK swapped for Yggdrasil engine (#152)
|
21
|
+
|
16
22
|
## [5.1.1] - 2024-09-23
|
17
23
|
### Fixed
|
18
24
|
- increased accuracy of rollout distribution (#200)
|
data/README.md
CHANGED
@@ -6,33 +6,12 @@
|
|
6
6
|
|
7
7
|
Ruby client for the [Unleash](https://github.com/Unleash/unleash) feature management service.
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
- [Usage in a Rails Application](#usage-in-a-rails-application)
|
16
|
-
- [1. Add Initializer](#1-add-initializer)
|
17
|
-
- [1.a Initializer for standard Rails applications](#1a-initializer-for-standard-rails-applications)
|
18
|
-
- [1.b Add Initializer if using Puma in clustered mode](#1b-add-initializer-if-using-puma-in-clustered-mode)
|
19
|
-
- [with `preload_app!`](#with-preload_app)
|
20
|
-
- [without `preload_app!`](#without-preload_app)
|
21
|
-
- [1.c Add Initializer if using Phusion Passenger](#1c-add-initializer-if-using-phusion-passenger)
|
22
|
-
- [1.d Add Initializer hooks when using within Sidekiq](#1d-add-initializer-hooks-when-using-within-sidekiq)
|
23
|
-
- [2. Set Unleash::Context](#2-set-unleashcontext)
|
24
|
-
- [3. Sample usage](#3-sample-usage)
|
25
|
-
- [Variations](#variations)
|
26
|
-
- [Bootstrapping](#bootstrapping)
|
27
|
-
- [Client methods](#client-methods)
|
28
|
-
- [Local test client](#local-test-client)
|
29
|
-
- [Available Strategies](#available-strategies)
|
30
|
-
- [Custom Strategies](#custom-strategies)
|
31
|
-
- [Development](#development)
|
32
|
-
- [Releasing](#releasing)
|
33
|
-
- [Contributing](#contributing)
|
34
|
-
|
35
|
-
## Supported Ruby Interpreters
|
9
|
+
> **Migrating to v6**
|
10
|
+
>
|
11
|
+
> If you use [custom strategies](#custom-strategies) or override built-in ones, read the complete [migration guide](./v6_MIGRATION_GUIDE.md) before upgrading to v6.
|
12
|
+
|
13
|
+
|
14
|
+
## Supported Ruby interpreters
|
36
15
|
|
37
16
|
- MRI 3.3
|
38
17
|
- MRI 3.2
|
@@ -59,72 +38,70 @@ Or install it yourself as:
|
|
59
38
|
|
60
39
|
$ gem install unleash
|
61
40
|
|
62
|
-
##
|
41
|
+
## Configuration
|
63
42
|
|
64
43
|
It is **required** to configure:
|
65
44
|
|
66
|
-
- `
|
67
|
-
- `
|
68
|
-
- `custom_http_headers` with `{'Authorization': '<
|
69
|
-
|
70
|
-
Please substitute the example `'https://unleash.herokuapp.com/api'` for the url of your own instance.
|
45
|
+
- `app_name` with the name of the running application
|
46
|
+
- `url` of your Unleash server
|
47
|
+
- `custom_http_headers` with `{'Authorization': '<YOUR_API_TOKEN>'}` when using Unleash v4+
|
71
48
|
|
72
49
|
It is **highly recommended** to configure:
|
73
50
|
|
74
|
-
- `instance_id` parameter with a unique identifier for the running instance
|
51
|
+
- `instance_id` parameter with a unique identifier for the running instance
|
75
52
|
|
76
53
|
```ruby
|
77
54
|
Unleash.configure do |config|
|
78
55
|
config.app_name = 'my_ruby_app'
|
79
|
-
config.url = '
|
80
|
-
config.custom_http_headers = {'Authorization': '<
|
56
|
+
config.url = '<YOUR_UNLEASH_URL>/api'
|
57
|
+
config.custom_http_headers = {'Authorization': '<YOUR_API_TOKEN>'}
|
81
58
|
end
|
82
59
|
```
|
83
60
|
|
84
61
|
or instantiate the client with the valid configuration:
|
85
62
|
|
86
63
|
```ruby
|
87
|
-
UNLEASH = Unleash::Client.new(url: '
|
64
|
+
UNLEASH = Unleash::Client.new(url: '<YOUR_UNLEASH_URL>/api', app_name: 'my_ruby_app', custom_http_headers: {'Authorization': '<YOUR_API_TOKEN>'})
|
88
65
|
```
|
89
66
|
|
90
67
|
## Dynamic custom HTTP headers
|
91
68
|
|
92
|
-
If you need custom HTTP headers that change during the lifetime of the client,
|
69
|
+
If you need custom HTTP headers that change during the lifetime of the client, you can pass `custom_http_headers` as a `Proc`.
|
93
70
|
|
94
71
|
```ruby
|
95
72
|
Unleash.configure do |config|
|
96
73
|
config.app_name = 'my_ruby_app'
|
97
|
-
config.url = '
|
74
|
+
config.url = '<YOUR_UNLEASH_URL>/api'
|
98
75
|
config.custom_http_headers = proc do
|
99
76
|
{
|
100
|
-
'Authorization': '<
|
77
|
+
'Authorization': '<YOUR_API_TOKEN>',
|
101
78
|
'X-Client-Request-Time': Time.now.iso8601
|
102
79
|
}
|
103
80
|
end
|
104
81
|
end
|
105
82
|
```
|
106
83
|
|
107
|
-
#### List of
|
84
|
+
#### List of arguments
|
108
85
|
|
109
|
-
| Argument | Description | Required? | Type | Default
|
86
|
+
| Argument | Description | Required? | Type | Default value |
|
110
87
|
| --------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------- | --------------------------------- | ---------------------------------------------- |
|
111
88
|
| `url` | Unleash server URL. | Y | String | N/A |
|
112
89
|
| `app_name` | Name of your program. | Y | String | N/A |
|
113
|
-
| `instance_id` | Identifier for the running instance of program
|
114
|
-
| `environment` | Unleash context option
|
115
|
-
| `project_name` | Name of the project to retrieve
|
116
|
-
| `refresh_interval` | How often the
|
117
|
-
| `metrics_interval` | How often the
|
118
|
-
| `disable_client` | Disables all communication with the Unleash server, effectively taking it _offline_. If set, `is_enabled?`
|
90
|
+
| `instance_id` | Identifier for the running instance of your program—set this to be able trace where metrics are being collected from. | N | String | random UUID |
|
91
|
+
| `environment` | Unleash context option, for example, `prod` or `dev`. Not yet in use. **Not** the same as the SDK's [Unleash environment](https://docs.getunleash.io/reference/environments). | N | String | `default` |
|
92
|
+
| `project_name` | Name of the project to retrieve feature flags from. If not set, all feature flags will be retrieved. | N | String | nil |
|
93
|
+
| `refresh_interval` | How often the Unleash client should check with the server for configuration changes. | N | Integer | 15 |
|
94
|
+
| `metrics_interval` | How often the Unleash client should send metrics to server. | N | Integer | 60 |
|
95
|
+
| `disable_client` | Disables all communication with the Unleash server, effectively taking it _offline_. If set, `is_enabled?` always answer with the `default_value` and configuration validation is skipped. Will also forcefully set `disable_metrics` to `true`. Defeats the entire purpose of using Unleash, except when running tests. | N | Boolean | `false` |
|
119
96
|
| `disable_metrics` | Disables sending metrics to Unleash server. If the `disable_client` option is set to `true`, then this option will also be set to `true`, regardless of the value provided. | N | Boolean | `false` |
|
120
|
-
| `custom_http_headers` | Custom headers to send to Unleash. As of Unleash v4.0.0, the `Authorization` header is required. For example: `{'Authorization': '<
|
97
|
+
| `custom_http_headers` | Custom headers to send to Unleash. As of Unleash v4.0.0, the `Authorization` header is required. For example: `{'Authorization': '<YOUR_API_TOKEN>'}`. | N | Hash/Proc | {} |
|
121
98
|
| `timeout` | How long to wait for the connection to be established or wait in reading state (open_timeout/read_timeout) | N | Integer | 30 |
|
122
99
|
| `retry_limit` | How many consecutive failures in connecting to the Unleash server are allowed before giving up. The default is to retry indefinitely. | N | Float::INFINITY | 5 |
|
123
|
-
| `backup_file` | Filename to store the last known state from the Unleash server.
|
100
|
+
| `backup_file` | Filename to store the last known state from the Unleash server. It is best to not change this from the default. | N | String | `Dir.tmpdir + "/unleash-#{app_name}-repo.json` |
|
124
101
|
| `logger` | Specify a custom `Logger` class to handle logs for the Unleash client. | N | Class | `Logger.new(STDOUT)` |
|
125
102
|
| `log_level` | Change the log level for the `Logger` class. Constant from `Logger::Severity`. | N | Constant | `Logger::WARN` |
|
126
|
-
| `bootstrap_config` | Bootstrap config
|
127
|
-
| `strategies` | Strategies manager that holds all strategies and allows to add custom strategies | N | Unleash::Strategies | `Unleash::Strategies.new` |
|
103
|
+
| `bootstrap_config` | Bootstrap config for loading data on startup—useful for loading large states on startup without (or before) hitting the network. | N | Unleash::Bootstrap::Configuration | `nil` |
|
104
|
+
| `strategies` | Strategies manager that holds all strategies and allows to add custom strategies. | N | Unleash::Strategies | `Unleash::Strategies.new` |
|
128
105
|
|
129
106
|
For a more in-depth look, please see `lib/unleash/configuration.rb`.
|
130
107
|
|
@@ -133,13 +110,13 @@ For a more in-depth look, please see `lib/unleash/configuration.rb`.
|
|
133
110
|
| `UNLEASH_BOOTSTRAP_FILE` | File to read bootstrap data from |
|
134
111
|
| `UNLEASH_BOOTSTRAP_URL` | URL to read bootstrap data from |
|
135
112
|
|
136
|
-
## Usage in a plain Ruby
|
113
|
+
## Usage in a plain Ruby application
|
137
114
|
|
138
115
|
```ruby
|
139
116
|
require 'unleash'
|
140
117
|
require 'unleash/context'
|
141
118
|
|
142
|
-
@unleash = Unleash::Client.new(app_name: 'my_ruby_app', url: '
|
119
|
+
@unleash = Unleash::Client.new(app_name: 'my_ruby_app', url: '<YOUR_UNLEASH_URL>/api', custom_http_headers: { 'Authorization': '<YOUR_API_TOKEN>' })
|
143
120
|
|
144
121
|
feature_name = "AwesomeFeature"
|
145
122
|
unleash_context = Unleash::Context.new
|
@@ -158,7 +135,7 @@ else
|
|
158
135
|
end
|
159
136
|
```
|
160
137
|
|
161
|
-
## Usage in a Rails
|
138
|
+
## Usage in a Rails application
|
162
139
|
|
163
140
|
### 1. Add Initializer
|
164
141
|
|
@@ -183,10 +160,8 @@ UNLEASH = Unleash::Client.new
|
|
183
160
|
# Rails.configuration.unleash = Unleash::Client.new
|
184
161
|
```
|
185
162
|
|
186
|
-
For `config.instance_id` use a string with a unique identification for the running instance.
|
187
|
-
|
188
|
-
Or the docker container id, if you are running in docker.
|
189
|
-
If it is not set the client will generate an unique UUID for each execution.
|
163
|
+
For `config.instance_id` use a string with a unique identification for the running instance. For example, it could be the hostname if you only run one App per host, or the docker container ID, if you are running in Docker.
|
164
|
+
If not set, the client will generate a unique UUID for each execution.
|
190
165
|
|
191
166
|
To have it available in the `rails console` command as well, also add to the file above:
|
192
167
|
|
@@ -213,12 +188,12 @@ Then you may keep the client configuration still in `config/initializers/unleash
|
|
213
188
|
```ruby
|
214
189
|
Unleash.configure do |config|
|
215
190
|
config.app_name = Rails.application.class.parent.to_s
|
216
|
-
config.url = '
|
217
|
-
config.custom_http_headers = {'Authorization': '<
|
191
|
+
config.url = '<YOUR_UNLEASH_URL>/api'
|
192
|
+
config.custom_http_headers = {'Authorization': '<YOUR_API_TOKEN>'}
|
218
193
|
end
|
219
194
|
```
|
220
195
|
|
221
|
-
But you must ensure that the
|
196
|
+
But you must ensure that the Unleash client is instantiated only after the process is forked.
|
222
197
|
This is done by creating the client inside the `on_worker_boot` code block in `puma.rb` as below:
|
223
198
|
|
224
199
|
```ruby
|
@@ -241,14 +216,14 @@ end
|
|
241
216
|
|
242
217
|
By not using `preload_app!`:
|
243
218
|
|
244
|
-
-
|
245
|
-
-
|
219
|
+
- The `Rails` constant will **not** be available.
|
220
|
+
- Phased restarts will be possible.
|
246
221
|
|
247
222
|
You need to ensure that in `puma.rb`:
|
248
223
|
|
249
|
-
-
|
250
|
-
-
|
251
|
-
-
|
224
|
+
- The Unleash SDK is loaded with `require 'unleash'` explicitly, as it will not be pre-loaded.
|
225
|
+
- All parameters are set explicitly in the `on_worker_boot` block, as `config/initializers/unleash.rb` is not read.
|
226
|
+
- There are no references to `Rails` constant, as that is not yet available.
|
252
227
|
|
253
228
|
Example for `puma.rb`:
|
254
229
|
|
@@ -263,8 +238,8 @@ on_worker_boot do
|
|
263
238
|
|
264
239
|
::UNLEASH = Unleash::Client.new(
|
265
240
|
app_name: 'my_rails_app',
|
266
|
-
url: '
|
267
|
-
custom_http_headers: {'Authorization': '<
|
241
|
+
url: '<YOUR_UNLEASH_URL>/api',
|
242
|
+
custom_http_headers: {'Authorization': '<YOUR_API_TOKEN>'},
|
268
243
|
)
|
269
244
|
end
|
270
245
|
|
@@ -277,7 +252,7 @@ Note that we also added shutdown hooks in `on_worker_shutdown`, to ensure a clea
|
|
277
252
|
|
278
253
|
#### 1.c Add Initializer if using [Phusion Passenger](https://github.com/phusion/passenger)
|
279
254
|
|
280
|
-
The
|
255
|
+
The Unleash client needs to be configured and instantiated inside the `PhusionPassenger.on_event(:starting_worker_process)` code block due to [smart spawning](https://www.phusionpassenger.com/library/indepth/ruby/spawn_methods/#smart-spawning-caveats):
|
281
256
|
|
282
257
|
The initializer in `config/initializers/unleash.rb` should look like:
|
283
258
|
|
@@ -288,8 +263,8 @@ PhusionPassenger.on_event(:starting_worker_process) do |forked|
|
|
288
263
|
config.app_name = Rails.application.class.parent.to_s
|
289
264
|
# config.instance_id = "#{Socket.gethostname}"
|
290
265
|
config.logger = Rails.logger
|
291
|
-
config.url = '
|
292
|
-
config.custom_http_headers = {'Authorization': '<
|
266
|
+
config.url = '<YOUR_UNLEASH_URL>/api'
|
267
|
+
config.custom_http_headers = {'Authorization': '<YOUR_API_TOKEN>'}
|
293
268
|
end
|
294
269
|
|
295
270
|
UNLEASH = Unleash::Client.new
|
@@ -299,7 +274,7 @@ end
|
|
299
274
|
|
300
275
|
#### 1.d Add Initializer hooks when using within [Sidekiq](https://github.com/mperham/sidekiq)
|
301
276
|
|
302
|
-
Note that in this case we require that the code block for `Unleash.configure` is set beforehand.
|
277
|
+
Note that in this case, we require that the code block for `Unleash.configure` is set beforehand.
|
303
278
|
For example in `config/initializers/unleash.rb`.
|
304
279
|
|
305
280
|
```ruby
|
@@ -316,7 +291,7 @@ end
|
|
316
291
|
|
317
292
|
### 2. Set Unleash::Context
|
318
293
|
|
319
|
-
|
294
|
+
Add the following method and callback in the application controller to have `@unleash_context` set for all requests:
|
320
295
|
|
321
296
|
Add in `app/controllers/application_controller.rb`:
|
322
297
|
|
@@ -333,7 +308,7 @@ Add in `app/controllers/application_controller.rb`:
|
|
333
308
|
end
|
334
309
|
```
|
335
310
|
|
336
|
-
|
311
|
+
Alternatively, you can add this method only to the controllers that use Unleash.
|
337
312
|
|
338
313
|
### 3. Sample usage
|
339
314
|
|
@@ -406,11 +381,11 @@ end
|
|
406
381
|
|
407
382
|
Note:
|
408
383
|
|
409
|
-
- The block/lambda/proc can use feature name and context as
|
384
|
+
- The block/lambda/proc can use the feature name and context as arguments.
|
410
385
|
- The client will evaluate the fallback function once per call of `is_enabled()`.
|
411
|
-
Please keep this in mind when creating your fallback function
|
386
|
+
Please keep this in mind when creating your fallback function.
|
412
387
|
- The returned value of the block should be a boolean.
|
413
|
-
However, the client will coerce the result to boolean via `!!`.
|
388
|
+
However, the client will coerce the result to a boolean via `!!`.
|
414
389
|
- If both a `default_value` and `fallback_function` are supplied,
|
415
390
|
the client will define the default value by `OR`ing the default value and the output of the fallback function.
|
416
391
|
|
@@ -442,21 +417,21 @@ Bootstrapping can be configured by providing a bootstrap configuration when init
|
|
442
417
|
|
443
418
|
```ruby
|
444
419
|
@unleash = Unleash::Client.new(
|
445
|
-
url: '
|
420
|
+
url: '<YOUR_UNLEASH_URL>/api',
|
446
421
|
app_name: 'my_ruby_app',
|
447
|
-
custom_http_headers: { 'Authorization': '<
|
422
|
+
custom_http_headers: { 'Authorization': '<YOUR_API_TOKEN>' },
|
448
423
|
bootstrap_config: Unleash::Bootstrap::Configuration.new({
|
449
|
-
url: "
|
450
|
-
url_headers: {'Authorization': '<
|
424
|
+
url: "<YOUR_UNLEASH_URL>/api/client/features",
|
425
|
+
url_headers: {'Authorization': '<YOUR_API_TOKEN>'}
|
451
426
|
})
|
452
427
|
)
|
453
428
|
```
|
454
429
|
|
455
430
|
The `Bootstrap::Configuration` initializer takes a hash with one of the following options specified:
|
456
431
|
|
457
|
-
- `file_path` - An absolute or relative path to a file containing a JSON string of the response body from the Unleash server. This can also be set
|
458
|
-
- `url` - A url pointing to an Unleash server's features endpoint, the code sample above is illustrative. This can also be set
|
459
|
-
- `url_headers` - Headers for the GET
|
432
|
+
- `file_path` - An absolute or relative path to a file containing a JSON string of the response body from the Unleash server. This can also be set through the `UNLEASH_BOOTSTRAP_FILE` environment variable.
|
433
|
+
- `url` - A url pointing to an Unleash server's features endpoint, the code sample above is illustrative. This can also be set through the `UNLEASH_BOOTSTRAP_URL` environment variable.
|
434
|
+
- `url_headers` - Headers for the GET HTTP request to the `url` above. Only used if the `url` parameter is also set. If this option isn't set then the bootstrapper will use the same url headers as the Unleash client.
|
460
435
|
- `data` - A raw JSON string as returned by the Unleash server.
|
461
436
|
- `block` - A lambda containing custom logic if you need it, an example is provided below.
|
462
437
|
|
@@ -470,13 +445,13 @@ The order of preference is as follows:
|
|
470
445
|
|
471
446
|
Example usage:
|
472
447
|
|
473
|
-
First
|
448
|
+
First, save the toggles locally:
|
474
449
|
|
475
450
|
```shell
|
476
|
-
curl -H 'Authorization: <
|
451
|
+
curl -H 'Authorization: <YOUR_API_TOKEN>' -XGET '<YOUR_UNLEASH_URL>/api' > ./default-toggles.json
|
477
452
|
```
|
478
453
|
|
479
|
-
|
454
|
+
Then use them on startup:
|
480
455
|
|
481
456
|
```ruby
|
482
457
|
|
@@ -486,8 +461,8 @@ custom_boostrapper = lambda {
|
|
486
461
|
|
487
462
|
@unleash = Unleash::Client.new(
|
488
463
|
app_name: 'my_ruby_app',
|
489
|
-
url: '
|
490
|
-
custom_http_headers: { 'Authorization': '<
|
464
|
+
url: '<YOUR_UNLEASH_URL>/api',
|
465
|
+
custom_http_headers: { 'Authorization': '<YOUR_API_TOKEN>' },
|
491
466
|
bootstrap_config: Unleash::Bootstrap::Configuration.new({
|
492
467
|
block: custom_boostrapper
|
493
468
|
})
|
@@ -499,19 +474,19 @@ Be aware that the client initializer will block until bootstrapping is complete.
|
|
499
474
|
|
500
475
|
#### Client methods
|
501
476
|
|
502
|
-
| Method
|
477
|
+
| Method name | Description | Return type |
|
503
478
|
| -------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------ |
|
504
|
-
| `is_enabled?` |
|
505
|
-
| `enabled?` |
|
506
|
-
| `if_enabled` |
|
507
|
-
| `is_disabled?` |
|
508
|
-
| `disabled?` |
|
509
|
-
| `if_disabled` |
|
510
|
-
| `get_variant` |
|
511
|
-
| `shutdown` |
|
512
|
-
| `shutdown!` |
|
513
|
-
|
514
|
-
For the full method signatures,
|
479
|
+
| `is_enabled?` | Checks if a feature toggle is enabled or not | Boolean |
|
480
|
+
| `enabled?` | A more idiomatic Ruby alias for the `is_enabled?` method | Boolean |
|
481
|
+
| `if_enabled` | Runs a code block, if a feature is enabled | `yield` |
|
482
|
+
| `is_disabled?` | Checks if feature toggle is enabled or not | Boolean |
|
483
|
+
| `disabled?` | A more idiomatic Ruby alias for the `is_disabled?` method | Boolean |
|
484
|
+
| `if_disabled` | Runs a code block, if a feature is disabled | `yield` |
|
485
|
+
| `get_variant` | Gets variant for a given feature | `Unleash::Variant` |
|
486
|
+
| `shutdown` | Saves metrics to disk, flushes metrics to server, and then kills `ToggleFetcher` and `MetricsReporter` threads—a safe shutdown, not generally needed in long-running applications, like web applications | nil |
|
487
|
+
| `shutdown!` | Kills `ToggleFetcher` and `MetricsReporter` threads immediately | nil |
|
488
|
+
|
489
|
+
For the full method signatures, see [client.rb](lib/unleash/client.rb).
|
515
490
|
|
516
491
|
## Local test client
|
517
492
|
|
@@ -523,9 +498,9 @@ bundle exec bin/unleash-client --help
|
|
523
498
|
bundle exec examples/simple.rb
|
524
499
|
```
|
525
500
|
|
526
|
-
## Available
|
501
|
+
## Available strategies
|
527
502
|
|
528
|
-
This client comes with
|
503
|
+
This client comes with all the required strategies out of the box:
|
529
504
|
|
530
505
|
- ApplicationHostnameStrategy
|
531
506
|
- DefaultStrategy
|
@@ -537,10 +512,10 @@ This client comes with the all the required strategies out of the box:
|
|
537
512
|
- UnknownStrategy
|
538
513
|
- UserWithIdStrategy
|
539
514
|
|
540
|
-
## Custom
|
515
|
+
## Custom strategies
|
541
516
|
|
542
|
-
|
543
|
-
In order for strategy to work correctly it should support two methods `name` and `is_enabled
|
517
|
+
You can add [custom activation strategies](https://docs.getunleash.io/advanced/custom_activation_strategy) using configuration.
|
518
|
+
In order for the strategy to work correctly it should support two methods `name` and `is_enabled?`.
|
544
519
|
|
545
520
|
```ruby
|
546
521
|
class MyCustomStrategy
|
@@ -575,16 +550,17 @@ To install this gem onto your local machine, run `bundle exec rake install`.
|
|
575
550
|
|
576
551
|
## Releasing
|
577
552
|
|
578
|
-
|
553
|
+
To release a new version, follow these steps:
|
579
554
|
|
580
|
-
|
581
|
-
-
|
582
|
-
|
583
|
-
-
|
584
|
-
-
|
585
|
-
|
586
|
-
|
587
|
-
|
555
|
+
1. Update version number:
|
556
|
+
- Increment the version number in the `./lib/unleash/version.rb` file according to [Semantic Versioning](https://semver.org/spec/v2.0.0.html) guidelines.
|
557
|
+
2. Update documentation:
|
558
|
+
- If the update includes a major or minor version change, update the [Installation section](#installation) in [README.md](README.md).
|
559
|
+
- Update [CHANGELOG.md](CHANGELOG.md) following the format on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
|
560
|
+
3. Commit changes:
|
561
|
+
- Commit the changes with a message like: `chore: bump version to x.y.z.`
|
562
|
+
4. Release the gem:
|
563
|
+
- Run `bundle exec rake release` to create a git tag for the new version, push commits and tags to origin, and publish `.gem` file to [rubygems.org](https://rubygems.org).
|
588
564
|
|
589
565
|
## Contributing
|
590
566
|
|
@@ -594,4 +570,4 @@ Be sure to run both `bundle exec rspec` and `bundle exec rubocop` in your branch
|
|
594
570
|
|
595
571
|
Please include tests with any pull requests, to avoid regressions.
|
596
572
|
|
597
|
-
Check out our guide for more information on how to build and scale [feature flag systems](https://docs.getunleash.io/topics/feature-flags/feature-flag-best-practices)
|
573
|
+
Check out our guide for more information on how to build and scale [feature flag systems](https://docs.getunleash.io/topics/feature-flags/feature-flag-best-practices).
|
data/lib/unleash/client.rb
CHANGED
@@ -2,7 +2,7 @@ require 'unleash/configuration'
|
|
2
2
|
require 'unleash/toggle_fetcher'
|
3
3
|
require 'unleash/metrics_reporter'
|
4
4
|
require 'unleash/scheduled_executor'
|
5
|
-
require 'unleash/
|
5
|
+
require 'unleash/variant'
|
6
6
|
require 'unleash/util/http'
|
7
7
|
require 'logger'
|
8
8
|
require 'time'
|
@@ -11,14 +11,17 @@ module Unleash
|
|
11
11
|
class Client
|
12
12
|
attr_accessor :fetcher_scheduled_executor, :metrics_scheduled_executor
|
13
13
|
|
14
|
+
# rubocop:disable Metrics/AbcSize
|
14
15
|
def initialize(*opts)
|
15
16
|
Unleash.configuration = Unleash::Configuration.new(*opts) unless opts.empty?
|
16
17
|
Unleash.configuration.validate!
|
17
18
|
|
18
19
|
Unleash.logger = Unleash.configuration.logger.clone
|
19
20
|
Unleash.logger.level = Unleash.configuration.log_level
|
21
|
+
Unleash.engine = YggdrasilEngine.new
|
22
|
+
Unleash.engine.register_custom_strategies(Unleash.configuration.strategies.custom_strategies)
|
20
23
|
|
21
|
-
Unleash.toggle_fetcher = Unleash::ToggleFetcher.new
|
24
|
+
Unleash.toggle_fetcher = Unleash::ToggleFetcher.new Unleash.engine
|
22
25
|
if Unleash.configuration.disable_client
|
23
26
|
Unleash.logger.warn "Unleash::Client is disabled! Will only return default (or bootstrapped if available) results!"
|
24
27
|
Unleash.logger.warn "Unleash::Client is disabled! Metrics and MetricsReporter are also disabled!"
|
@@ -30,6 +33,7 @@ module Unleash
|
|
30
33
|
start_toggle_fetcher
|
31
34
|
start_metrics unless Unleash.configuration.disable_metrics
|
32
35
|
end
|
36
|
+
# rubocop:enable Metrics/AbcSize
|
33
37
|
|
34
38
|
def is_enabled?(feature, context = nil, default_value_param = false, &fallback_blk)
|
35
39
|
Unleash.logger.debug "Unleash::Client.is_enabled? feature: #{feature} with context #{context}"
|
@@ -40,15 +44,16 @@ module Unleash
|
|
40
44
|
default_value_param
|
41
45
|
end
|
42
46
|
|
43
|
-
|
44
|
-
if
|
47
|
+
toggle_enabled = Unleash.engine.enabled?(feature, context)
|
48
|
+
if toggle_enabled.nil?
|
45
49
|
Unleash.logger.debug "Unleash::Client.is_enabled? feature: #{feature} not found"
|
50
|
+
Unleash.engine.count_toggle(feature, false)
|
46
51
|
return default_value
|
47
52
|
end
|
48
53
|
|
49
|
-
|
54
|
+
Unleash.engine.count_toggle(feature, toggle_enabled)
|
50
55
|
|
51
|
-
|
56
|
+
toggle_enabled
|
52
57
|
end
|
53
58
|
|
54
59
|
def is_disabled?(feature, context = nil, default_value_param = true, &fallback_blk)
|
@@ -71,23 +76,19 @@ module Unleash
|
|
71
76
|
end
|
72
77
|
|
73
78
|
def get_variant(feature, context = Unleash::Context.new, fallback_variant = disabled_variant)
|
74
|
-
Unleash.
|
75
|
-
|
76
|
-
toggle_as_hash = Unleash&.toggles&.select{ |toggle| toggle['name'] == feature }&.first
|
77
|
-
|
78
|
-
if toggle_as_hash.nil?
|
79
|
-
Unleash.logger.debug "Unleash::Client.get_variant feature: #{feature} not found"
|
80
|
-
return fallback_variant
|
81
|
-
end
|
82
|
-
|
83
|
-
toggle = Unleash::FeatureToggle.new(toggle_as_hash, Unleash&.segment_cache)
|
84
|
-
variant = toggle.get_variant(context, fallback_variant)
|
79
|
+
variant = Unleash.engine.get_variant(feature, context)
|
85
80
|
|
86
81
|
if variant.nil?
|
87
82
|
Unleash.logger.debug "Unleash::Client.get_variant variants for feature: #{feature} not found"
|
83
|
+
Unleash.engine.count_toggle(feature, false)
|
88
84
|
return fallback_variant
|
89
85
|
end
|
90
86
|
|
87
|
+
variant = Variant.new(variant)
|
88
|
+
|
89
|
+
Unleash.engine.count_variant(feature, variant.name)
|
90
|
+
Unleash.engine.count_toggle(feature, variant.feature_enabled)
|
91
|
+
|
91
92
|
# TODO: Add to README: name, payload, enabled (bool)
|
92
93
|
|
93
94
|
variant
|
@@ -96,7 +97,6 @@ module Unleash
|
|
96
97
|
# safe shutdown: also flush metrics to server and toggles to disk
|
97
98
|
def shutdown
|
98
99
|
unless Unleash.configuration.disable_client
|
99
|
-
Unleash.toggle_fetcher.save!
|
100
100
|
Unleash.reporter.post unless Unleash.configuration.disable_metrics
|
101
101
|
shutdown!
|
102
102
|
end
|
@@ -117,12 +117,12 @@ module Unleash
|
|
117
117
|
'appName': Unleash.configuration.app_name,
|
118
118
|
'instanceId': Unleash.configuration.instance_id,
|
119
119
|
'sdkVersion': "unleash-client-ruby:" + Unleash::VERSION,
|
120
|
-
'strategies': Unleash.strategies.
|
120
|
+
'strategies': Unleash.strategies.known_strategies,
|
121
121
|
'started': Time.now.iso8601(Unleash::TIME_RESOLUTION),
|
122
122
|
'interval': Unleash.configuration.metrics_interval_in_millis,
|
123
123
|
'platformName': RUBY_ENGINE,
|
124
124
|
'platformVersion': RUBY_VERSION,
|
125
|
-
'yggdrasilVersion':
|
125
|
+
'yggdrasilVersion': "0.13.2",
|
126
126
|
'specVersion': Unleash::CLIENT_SPECIFICATION_VERSION
|
127
127
|
}
|
128
128
|
end
|
@@ -140,7 +140,6 @@ module Unleash
|
|
140
140
|
end
|
141
141
|
|
142
142
|
def start_metrics
|
143
|
-
Unleash.toggle_metrics = Unleash::Metrics.new
|
144
143
|
Unleash.reporter = Unleash::MetricsReporter.new
|
145
144
|
self.metrics_scheduled_executor = Unleash::ScheduledExecutor.new(
|
146
145
|
'MetricsReporter',
|
@@ -166,7 +165,7 @@ module Unleash
|
|
166
165
|
end
|
167
166
|
|
168
167
|
def disabled_variant
|
169
|
-
@disabled_variant ||= Unleash::
|
168
|
+
@disabled_variant ||= Unleash::Variant.disabled_variant
|
170
169
|
end
|
171
170
|
|
172
171
|
def first_fetch_is_eager
|
@@ -40,9 +40,9 @@ module Unleash
|
|
40
40
|
def validate!
|
41
41
|
return if self.disable_client
|
42
42
|
|
43
|
-
raise ArgumentError, "
|
43
|
+
raise ArgumentError, "app_name is a required parameter." if self.app_name.nil?
|
44
44
|
|
45
|
-
validate_custom_http_headers!(self.custom_http_headers)
|
45
|
+
validate_custom_http_headers!(self.custom_http_headers) unless self.url.nil?
|
46
46
|
end
|
47
47
|
|
48
48
|
def refresh_backup_file!
|