Skip to content

Commit 2178ca6

Browse files
authored
enhancement: Generate OptimizelyConfig object on API Call instead of SDK initialization (#296)
## Summary `OptimizelyConfig` object is generated when SDK is being initialized. This operation is heavy and blocks the SDK from initializing quickly. This object is only used when users explicitly access it using `get_optimizely_config` API call. This PR moves the creation of this object from SDK initialization to the actual API call. ## Test plan - Manually tested thoroughly - All unit tests pass - All FSC tests pass ## Issues fixes #295
1 parent 8683bea commit 2178ca6

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

lib/optimizely/config_manager/http_project_config_manager.rb

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
#
4-
# Copyright 2019-2020, Optimizely and contributors
4+
# Copyright 2019-2020, 2022, Optimizely and contributors
55
#
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.
@@ -33,7 +33,7 @@ module Optimizely
3333
class HTTPProjectConfigManager < ProjectConfigManager
3434
# Config manager that polls for the datafile and updated ProjectConfig based on an update interval.
3535

36-
attr_reader :stopped, :optimizely_config
36+
attr_reader :stopped
3737

3838
# Initialize config manager. One of sdk_key or url has to be set to be able to use.
3939
#
@@ -80,8 +80,8 @@ def initialize(
8080
@last_modified = nil
8181
@skip_json_validation = skip_json_validation
8282
@notification_center = notification_center.is_a?(Optimizely::NotificationCenter) ? notification_center : NotificationCenter.new(@logger, @error_handler)
83+
@optimizely_config = nil
8384
@config = datafile.nil? ? nil : DatafileProjectConfig.create(datafile, @logger, @error_handler, @skip_json_validation)
84-
@optimizely_config = @config.nil? ? nil : OptimizelyConfig.new(@config).config
8585
@mutex = Mutex.new
8686
@resource = ConditionVariable.new
8787
@async_scheduler = AsyncScheduler.new(method(:fetch_datafile_config), @polling_interval, auto_update, @logger)
@@ -140,6 +140,12 @@ def config
140140
@config
141141
end
142142

143+
def optimizely_config
144+
@optimizely_config = OptimizelyConfig.new(@config).config if @optimizely_config.nil?
145+
146+
@optimizely_config
147+
end
148+
143149
private
144150

145151
def fetch_datafile_config
@@ -209,7 +215,9 @@ def set_config(config)
209215
end
210216

211217
@config = config
212-
@optimizely_config = OptimizelyConfig.new(config).config
218+
219+
# clearing old optimizely config so that a fresh one is generated on the next api call.
220+
@optimizely_config = nil
213221

214222
@notification_center.send_notifications(NotificationCenter::NOTIFICATION_TYPES[:OPTIMIZELY_CONFIG_UPDATE])
215223

lib/optimizely/config_manager/static_project_config_manager.rb

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
#
4-
# Copyright 2019-2020, Optimizely and contributors
4+
# Copyright 2019-2020, 2022, Optimizely and contributors
55
#
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.
@@ -23,7 +23,7 @@
2323
module Optimizely
2424
class StaticProjectConfigManager < ProjectConfigManager
2525
# Implementation of ProjectConfigManager interface.
26-
attr_reader :config, :optimizely_config
26+
attr_reader :config
2727

2828
def initialize(datafile, logger, error_handler, skip_json_validation)
2929
# Looks up and sets datafile and config based on response body.
@@ -40,8 +40,13 @@ def initialize(datafile, logger, error_handler, skip_json_validation)
4040
error_handler,
4141
skip_json_validation
4242
)
43+
@optimizely_config = nil
44+
end
45+
46+
def optimizely_config
47+
@optimizely_config = OptimizelyConfig.new(@config).config if @optimizely_config.nil?
4348

44-
@optimizely_config = @config.nil? ? nil : OptimizelyConfig.new(@config).config
49+
@optimizely_config
4550
end
4651
end
4752
end

0 commit comments

Comments
 (0)