Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Modify data kind constants to avoid Process.warmup rehash #301

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions lib/ldclient-rb/impl/data_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,56 @@
module LaunchDarkly
module Impl
module DataStore

class DataKind
FEATURES = "features".freeze
SEGMENTS = "segments".freeze

FEATURE_PREREQ_FN = lambda { |flag| (flag[:prerequisites] || []).map { |p| p[:key] } }.freeze

attr_reader :namespace
attr_reader :priority

#
# @param namespace [String]
# @param priority [Integer]
#
def initialize(namespace:, priority:)
@namespace = namespace
@priority = priority
end

#
# Maintain the same behavior when these data kinds were standard ruby hashes.
#
# @param key [Symbol]
# @return [Object]
#
def [](key)
return priority if key == :priority
return namespace if key == :namespace
return get_dependency_keys_fn() if key == :get_dependency_keys
nil
end

#
# Retrieve the dependency keys for a particular data kind. Right now, this is only defined for flags.
#
def get_dependency_keys_fn()
return nil unless @namespace == FEATURES

FEATURE_PREREQ_FN
end

def eql?(other)
namespace == other.namespace && priority == other.priority
end

def hash
[namespace, priority].hash
end
end

class StatusProvider
include LaunchDarkly::Interfaces::DataStore::StatusProvider

Expand Down
11 changes: 2 additions & 9 deletions lib/ldclient-rb/in_memory_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,10 @@ module LaunchDarkly
# to ensure data consistency during non-atomic updates.

# @private
FEATURES = {
namespace: "features",
priority: 1, # that is, features should be stored after segments
get_dependency_keys: lambda { |flag| (flag[:prerequisites] || []).map { |p| p[:key] } },
}.freeze
FEATURES = Impl::DataStore::DataKind.new(namespace: "features", priority: 1).freeze

# @private
SEGMENTS = {
namespace: "segments",
priority: 0,
}.freeze
SEGMENTS = Impl::DataStore::DataKind.new(namespace: "segments", priority: 0).freeze

# @private
ALL_KINDS = [FEATURES, SEGMENTS].freeze
Expand Down