rollout-redis-adapter 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a9cc50e6246dc24c608dd1ed9574950ae757ac12a6338c42dec4a1bbd1b844fd
4
+ data.tar.gz: 43eb90671e27630916809f429f7f101ab56a75abbcb6c5e60974a8dc7eef5701
5
+ SHA512:
6
+ metadata.gz: b93786646e9fcad1049aff85e8c0ebb40d15624ce836ef325ea75df8f88d0f496cb1b7d8d4e0ca35a75760e3f89be5065dc98f61c72a078b6b2c3eb1d2a54ca4
7
+ data.tar.gz: 9de7bddd73e7a9ee24d97e5ca34883b54b432fbb9310094ff4a4bb452b194a973420afd06eeeb04fe56d4dfb121a109c37d3ab72b7a9fb42d2659efb422d3a2d
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010-InfinityAndBeyond BitLove, Inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,133 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'redis'
4
+ require 'rollout'
5
+ require 'rollout/logging'
6
+ require 'rollout/redis/codec'
7
+
8
+ class Rollout
9
+ module Adapters
10
+ class Redis
11
+ FEATURES_KEY = 'feature:__features__'
12
+
13
+ def initialize(client)
14
+ @client = client
15
+ end
16
+
17
+ def fetch_feature(name)
18
+ ::Rollout::Redis::Codec.decode(name, @client.get(key(name)))
19
+ end
20
+
21
+ def fetch_features(names)
22
+ return [] if names.empty?
23
+
24
+ payloads = @client.mget(*names.map { |name| key(name) })
25
+ names.zip(payloads).map { |name, payload| ::Rollout::Redis::Codec.decode(name, payload) }
26
+ end
27
+
28
+ def feature_names
29
+ (@client.get(FEATURES_KEY) || '').split(',')
30
+ end
31
+
32
+ def feature_exists?(name)
33
+ if @client.respond_to?(:exists?)
34
+ @client.exists?(key(name))
35
+ else
36
+ @client.exists(key(name))
37
+ end
38
+ end
39
+
40
+ def save_feature(state)
41
+ @client.set(key(state.name), ::Rollout::Redis::Codec.encode(state))
42
+ names = feature_names.map(&:to_s) | [state.name.to_s]
43
+ @client.set(FEATURES_KEY, names.join(','))
44
+ end
45
+
46
+ def delete_feature(name)
47
+ names = feature_names
48
+ names.delete(name.to_s)
49
+ @client.set(FEATURES_KEY, names.join(','))
50
+ @client.del(key(name))
51
+ end
52
+
53
+ def clear_features
54
+ @client.del(FEATURES_KEY)
55
+ end
56
+
57
+ def mutate_feature(name)
58
+ mutation = yield fetch_feature(name)
59
+ save_feature(mutation.fetch(:state))
60
+ event = mutation[:event]
61
+ if event
62
+ record_event(
63
+ event,
64
+ history_length: mutation.fetch(:history_length),
65
+ global: mutation[:global],
66
+ )
67
+ end
68
+ mutation
69
+ end
70
+
71
+ def record_event(event, history_length:, global: false)
72
+ storage_key = events_key(event.feature)
73
+ @client.zadd(storage_key, -event.timestamp, event.serialize)
74
+ @client.zremrangebyrank(storage_key, history_length, -1)
75
+
76
+ return unless global
77
+
78
+ @client.zadd(global_events_key, -event.timestamp, event.serialize)
79
+ @client.zremrangebyrank(global_events_key, history_length, -1)
80
+ end
81
+
82
+ def feature_events(name, limit: nil)
83
+ events_from(events_key(name), limit: limit)
84
+ end
85
+
86
+ def global_events(limit: nil)
87
+ events_from(global_events_key, limit: limit)
88
+ end
89
+
90
+ def feature_updated_at(name)
91
+ _, score = @client.zrange(events_key(name), 0, 0, with_scores: true).first
92
+ Time.at(-score.to_f / 1_000_000) if score
93
+ end
94
+
95
+ def delete_feature_events(name)
96
+ @client.del(events_key(name))
97
+ end
98
+
99
+ private
100
+
101
+ def key(name)
102
+ "feature:#{name}"
103
+ end
104
+
105
+ def events_key(name)
106
+ "feature:#{name}:logging:events"
107
+ end
108
+
109
+ def global_events_key
110
+ 'feature:_global_:logging:events'
111
+ end
112
+
113
+ def events_from(storage_key, limit: nil)
114
+ stop = event_range_stop(limit)
115
+ return [] if stop == :empty
116
+
117
+ @client
118
+ .zrange(storage_key, 0, stop, with_scores: true)
119
+ .map { |value| ::Rollout::Redis::Codec.decode_event(*value) }
120
+ .reverse
121
+ end
122
+
123
+ def event_range_stop(limit)
124
+ return -1 if limit.nil?
125
+ raise ArgumentError, "limit must be an Integer" unless limit.is_a?(Integer)
126
+ raise ArgumentError, "limit must be >= 0" if limit < 0
127
+ return :empty if limit.zero?
128
+
129
+ limit - 1
130
+ end
131
+ end
132
+ end
133
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'rollout/feature_state'
5
+ require 'rollout/logging'
6
+
7
+ class Rollout
8
+ module Redis
9
+ class Codec
10
+ def self.decode(name, payload)
11
+ if payload.nil? || payload.empty?
12
+ return FeatureState.new(
13
+ name: name,
14
+ percentage: 0,
15
+ users: [],
16
+ groups: [],
17
+ data: {},
18
+ )
19
+ end
20
+
21
+ raw_percentage, raw_users, raw_groups, raw_data = payload.split('|', 4)
22
+ data = raw_data.nil? || raw_data.strip.empty? ? {} : JSON.parse(raw_data)
23
+
24
+ FeatureState.new(
25
+ name: name,
26
+ percentage: raw_percentage.to_f,
27
+ users: (raw_users || '').split(','),
28
+ groups: (raw_groups || '').split(','),
29
+ data: data,
30
+ )
31
+ end
32
+
33
+ def self.encode(feature_state)
34
+ "#{feature_state.percentage}|#{feature_state.users.join(',')}|#{feature_state.groups.join(',')}|#{feature_state.data.to_json}"
35
+ end
36
+
37
+ def self.decode_event(value, score)
38
+ hash = JSON.parse(value, symbolize_names: true)
39
+
40
+ Logging::Event.new(**hash.merge(created_at: Time.at(-score.to_f / 1_000_000)))
41
+ end
42
+ end
43
+ end
44
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rollout-redis-adapter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - FetLife
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: redis
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '4.0'
19
+ - - "<"
20
+ - !ruby/object:Gem::Version
21
+ version: '7'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: '4.0'
29
+ - - "<"
30
+ - !ruby/object:Gem::Version
31
+ version: '7'
32
+ - !ruby/object:Gem::Dependency
33
+ name: rollout
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '3.0'
39
+ - - "<"
40
+ - !ruby/object:Gem::Version
41
+ version: '4'
42
+ type: :runtime
43
+ prerelease: false
44
+ version_requirements: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '3.0'
49
+ - - "<"
50
+ - !ruby/object:Gem::Version
51
+ version: '4'
52
+ description: Redis adapter for the rollout gem.
53
+ email:
54
+ - dev@fetlife.com
55
+ executables: []
56
+ extensions: []
57
+ extra_rdoc_files: []
58
+ files:
59
+ - LICENSE
60
+ - lib/rollout/adapters/redis.rb
61
+ - lib/rollout/redis/codec.rb
62
+ homepage: https://github.com/FetLife/rollout
63
+ licenses:
64
+ - MIT
65
+ metadata: {}
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '2.3'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubygems_version: 3.6.9
81
+ specification_version: 4
82
+ summary: Redis adapter for the rollout gem.
83
+ test_files: []