amplitude-experiment 1.1.2 → 1.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +2 -1
- data/lib/experiment/factory.rb +4 -9
- data/lib/experiment/local/fetcher.rb +1 -1
- data/lib/experiment/persistent_http_client.rb +11 -5
- data/lib/experiment/remote/client.rb +1 -1
- data/lib/experiment/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: da33da0e966b08a4172225241b5a89b4d0b8c0b91af484b6ebaf33aa8e27fb21
|
4
|
+
data.tar.gz: 22176a1dcf52fcc64035eb2fc914e891718846e93b83d9f8b29faa5437ca759b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f41ba9124f0f65dec01fe7223a1ce0ba5337e1e93afc9e73b97a2b73442bd95e71f7c8c3c15afc2ed9aa3fa2c3af758171c7910721dc15a00a299b510810c1e2
|
7
|
+
data.tar.gz: 6d3a204bae5dc360865018a0fe0cf3dd5c3cc6add4f3ef925556c05c75668a066aa476f2ef07eb475d999d8bdf7b8ad586fc5e593c9777dc7e618096ce800631
|
data/README.md
CHANGED
@@ -10,6 +10,8 @@
|
|
10
10
|
# Experiment Ruby SDK
|
11
11
|
Amplitude Ruby Server SDK for Experiment.
|
12
12
|
|
13
|
+
> ⚠️ **Local evaluation process fork safety:** When using a `LocalEvaluationClient` and forking the process, you must call `#start` _after forking_ to update the flag configuration state on the new process. For example, in Puma, this means calling `#start` in the [`on_worker_boot` hook](https://www.rubydoc.info/gems/puma/Puma%2FDSL:on_worker_boot).
|
14
|
+
|
13
15
|
## Installation
|
14
16
|
Into Gemfile from rubygems.org:
|
15
17
|
```ruby
|
@@ -24,7 +26,6 @@ To install beta versions:
|
|
24
26
|
gem install amplitude-experiment --pre
|
25
27
|
```
|
26
28
|
|
27
|
-
|
28
29
|
## Remote Evaluation Quick Start
|
29
30
|
```ruby
|
30
31
|
require 'amplitude-experiment'
|
data/lib/experiment/factory.rb
CHANGED
@@ -2,7 +2,6 @@
|
|
2
2
|
module AmplitudeExperiment
|
3
3
|
@local_instance = {}
|
4
4
|
@remote_instance = {}
|
5
|
-
@default_instance = '$default_instance'
|
6
5
|
|
7
6
|
# Initializes a singleton Client. This method returns a default singleton instance, subsequent calls to
|
8
7
|
# init will return the initial instance regardless of input.
|
@@ -10,10 +9,8 @@ module AmplitudeExperiment
|
|
10
9
|
# @param [String] api_key The environment API Key
|
11
10
|
# @param [Config] config Optional Config.
|
12
11
|
def self.initialize_remote(api_key, config = nil)
|
13
|
-
unless @
|
14
|
-
|
15
|
-
end
|
16
|
-
@local_instance.fetch(@default_instance)
|
12
|
+
@remote_instance.store(api_key, RemoteEvaluationClient.new(api_key, config)) unless @remote_instance.key?(api_key)
|
13
|
+
@remote_instance.fetch(api_key)
|
17
14
|
end
|
18
15
|
|
19
16
|
# Initializes a local evaluation Client. A local evaluation client can evaluate local flags or experiments for a
|
@@ -23,9 +20,7 @@ module AmplitudeExperiment
|
|
23
20
|
# @param [String] api_key The environment API Key
|
24
21
|
# @param [Config] config Optional Config.
|
25
22
|
def self.initialize_local(api_key, config = nil)
|
26
|
-
unless @
|
27
|
-
|
28
|
-
end
|
29
|
-
@remote_instance.fetch(@default_instance)
|
23
|
+
@local_instance.store(api_key, LocalEvaluationClient.new(api_key, config)) unless @local_instance.key?(api_key)
|
24
|
+
@local_instance.fetch(api_key)
|
30
25
|
end
|
31
26
|
end
|
@@ -9,7 +9,7 @@ module AmplitudeExperiment
|
|
9
9
|
@api_key = api_key
|
10
10
|
@server_url = server_url
|
11
11
|
@logger = logger
|
12
|
-
@http = PersistentHttpClient.get(server_url, { read_timeout: FLAG_CONFIG_TIMEOUT })
|
12
|
+
@http = PersistentHttpClient.get(server_url, { read_timeout: FLAG_CONFIG_TIMEOUT }, @api_key)
|
13
13
|
end
|
14
14
|
|
15
15
|
# Fetch local evaluation mode flag configs from the Experiment API server.
|
@@ -1,14 +1,20 @@
|
|
1
1
|
module AmplitudeExperiment
|
2
|
-
# Persist Http Client to reuse connection and reduce IO
|
2
|
+
# Persist Http Client to reuse connection and reduce IO. Connections are
|
3
|
+
# shared by api key.
|
4
|
+
#
|
5
|
+
# WARNING: these connections are not safe for concurrent requests. Callers
|
6
|
+
# must synchronize requests per connection.
|
3
7
|
class PersistentHttpClient
|
4
8
|
DEFAULT_OPTIONS = { read_timeout: 80 }.freeze
|
5
9
|
|
6
10
|
class << self
|
7
11
|
# url: URI / String
|
8
12
|
# options: any options that Net::HTTP.new accepts
|
9
|
-
|
13
|
+
# api_key: the deployment key for ensuring different deployments don't
|
14
|
+
# share connections.
|
15
|
+
def get(url, options, api_key)
|
10
16
|
uri = url.is_a?(URI) ? url : URI(url)
|
11
|
-
connection_manager.get_client(uri, options)
|
17
|
+
connection_manager.get_client(uri, options, api_key)
|
12
18
|
end
|
13
19
|
|
14
20
|
private
|
@@ -72,7 +78,7 @@ module AmplitudeExperiment
|
|
72
78
|
self.last_used = Time.now
|
73
79
|
end
|
74
80
|
|
75
|
-
def get_client(uri, options)
|
81
|
+
def get_client(uri, options, api_key)
|
76
82
|
mutex.synchronize do
|
77
83
|
# refresh the last time a client was used,
|
78
84
|
# this prevents the client from becoming stale
|
@@ -81,7 +87,7 @@ module AmplitudeExperiment
|
|
81
87
|
# we use params as a cache key for clients.
|
82
88
|
# 2 connections to the same host but with different
|
83
89
|
# options are going to use different HTTP clients
|
84
|
-
params = [uri.host, uri.port, options]
|
90
|
+
params = [uri.host, uri.port, options, api_key]
|
85
91
|
client = clients_store[params]
|
86
92
|
|
87
93
|
return client if client
|
@@ -99,7 +99,7 @@ module AmplitudeExperiment
|
|
99
99
|
'Content-Type' => 'application/json;charset=utf-8'
|
100
100
|
}
|
101
101
|
read_timeout = timeout_millis / 1000 if (timeout_millis / 1000) > 0
|
102
|
-
http = PersistentHttpClient.get(@uri, { read_timeout: read_timeout })
|
102
|
+
http = PersistentHttpClient.get(@uri, { read_timeout: read_timeout }, @api_key)
|
103
103
|
request = Net::HTTP::Post.new(@uri, headers)
|
104
104
|
request.body = user_context.to_json
|
105
105
|
if request.body.length > 8000
|
data/lib/experiment/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: amplitude-experiment
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Amplitude
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-07-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: psych
|