-
Notifications
You must be signed in to change notification settings - Fork 150
Throttle calls to a custom configuration loader #142
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| module Resque | ||
| class Pool | ||
| # Throttle the frequency of loading pool configuration | ||
| class ConfigThrottle | ||
| def initialize(period, config_loader, time_source: Time) | ||
| @period = period | ||
| @config_loader = config_loader | ||
| @resettable = config_loader.respond_to?(:reset!) | ||
| @last_check = 0 | ||
| @time_source = time_source | ||
| end | ||
|
|
||
| def call(env) | ||
| # We do not need to cache per `env`, since the value of `env` will not | ||
| # change during the life of the process. | ||
| if (now > @last_check + @period) | ||
| @cache = @config_loader.call(env) | ||
| @last_check = now | ||
| end | ||
| @cache | ||
| end | ||
|
|
||
| def reset! | ||
| @last_check = 0 | ||
| if @resettable | ||
| @config_loader.reset! | ||
| end | ||
| end | ||
|
|
||
| def now | ||
| @time_source.now.to_f | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| require 'spec_helper' | ||
| require 'resque/pool/config_throttle' | ||
|
|
||
| describe Resque::Pool::ConfigThrottle do | ||
| let(:fake_time) { FakeTime.new 1445898807 } | ||
|
|
||
| it "returns the config returned by the wrapped config loader for given env" do | ||
| wrapped_config = { | ||
| "dev" => {"qA,qB" => 1}, | ||
| "prd" => {"qA,qB" => 4} | ||
| } | ||
| wrapped_loader = lambda {|env| wrapped_config[env] } | ||
| throttle = Resque::Pool::ConfigThrottle.new(10, wrapped_loader) | ||
|
|
||
| throttle.call("prd").should eq({"qA,qB" => 4}) | ||
| end | ||
|
|
||
| it "does not call wrapped loader again until the specified period of time has elapsed" do | ||
| wrapped_loader = TestConfigLoader.new | ||
| wrapped_loader.configuration = {"qA,qB" => 1} | ||
|
|
||
| throttle = Resque::Pool::ConfigThrottle.new(10, wrapped_loader, time_source: fake_time) | ||
| first_call = throttle.call("prd") | ||
|
|
||
| new_config = {"qA,qB" => 22} | ||
| wrapped_loader.configuration = new_config | ||
| fake_time.advance_time 6 | ||
| # config changed, but not enough time elapsed | ||
|
|
||
| second_call = throttle.call("prd") | ||
|
|
||
| second_call.should eq(first_call) | ||
| wrapped_loader.times_called.should == 1 | ||
|
|
||
| fake_time.advance_time 6 | ||
| # now, enough time has elapsed to retrieve latest config | ||
|
|
||
| third_call = throttle.call("prd") | ||
|
|
||
| third_call.should_not eq(first_call) | ||
| third_call.should eq(new_config) | ||
| wrapped_loader.times_called.should == 2 | ||
|
|
||
| # further calls continue to use cached value | ||
| throttle.call("prd") | ||
| throttle.call("prd") | ||
| throttle.call("prd") | ||
| wrapped_loader.times_called.should == 2 | ||
| end | ||
|
|
||
| it "forces a call to the wrapperd loader after reset! called, even if required time hasn't elapsed" do | ||
| wrapped_loader = TestConfigLoader.new | ||
| wrapped_loader.configuration = {"qA,qB" => 1} | ||
|
|
||
| throttle = Resque::Pool::ConfigThrottle.new(10, wrapped_loader, time_source: fake_time) | ||
| first_call = throttle.call("prd") | ||
|
|
||
| new_config = {"qA,qB" => 22} | ||
| wrapped_loader.configuration = new_config | ||
| fake_time.advance_time 6 | ||
| # the 10 second period has *not* elapsed | ||
|
|
||
| throttle.reset! | ||
|
|
||
| second_call = throttle.call("prd") | ||
|
|
||
| second_call.should eq(new_config) | ||
| wrapped_loader.times_called.should == 2 | ||
| end | ||
|
|
||
| it "delegates reset! to the wrapped_loader, when supported" do | ||
| wrapped_loader = TestConfigLoader.new | ||
| throttle = Resque::Pool::ConfigThrottle.new(10, wrapped_loader) | ||
|
|
||
| wrapped_loader.times_reset.should == 0 | ||
| throttle.reset! | ||
| wrapped_loader.times_reset.should == 1 | ||
| end | ||
|
|
||
| it "does not delegate reset! to the wrapped_loader when it is not supported" do | ||
| wrapped_loader = lambda {|env| Hash.new } | ||
| throttle = Resque::Pool::ConfigThrottle.new(10, wrapped_loader) | ||
|
|
||
| expect { | ||
| throttle.reset! | ||
| }.to_not raise_error | ||
| end | ||
|
|
||
|
|
||
| class TestConfigLoader | ||
| attr_accessor :configuration | ||
| attr_reader :times_called | ||
| attr_reader :times_reset | ||
|
|
||
| def initialize | ||
| @times_called = 0 | ||
| @times_reset = 0 | ||
| end | ||
|
|
||
| def call(env) | ||
| @times_called += 1 | ||
| configuration | ||
| end | ||
|
|
||
| def reset! | ||
| @times_reset += 1 | ||
| end | ||
| end | ||
|
|
||
| class FakeTime | ||
| attr_reader :now | ||
|
|
||
| def initialize(start_time) | ||
| @now = start_time | ||
| end | ||
|
|
||
| def advance_time(seconds) | ||
| @now += seconds | ||
| end | ||
| end | ||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment (and my own playing around with decomposing the default loader) makes me feel that sending in
envas an argument tocallis the wrong API. I'm currently thinking thatenvironmentshould be passed in to the config loader'sinitialize, so we can codify the assumption thatenvwill not change.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I came to the same conclusion. Not every config loader will care about the environment - in fact most probably won't.
I agree that any config loader that has a dependency on other information (redis connection, db connection, current environment, etc) should take that info via the constructor.