-
Notifications
You must be signed in to change notification settings - Fork 21
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
Monitor config file for changes and reload when needed #79
Conversation
67a2c20
to
cd3a161
Compare
cd3a161
to
4394736
Compare
match parsed_limits { | ||
Ok(limits) => { | ||
match &self { | ||
Self::Blocking(limiter) => limiter.configure_with(limits)?, |
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.
Nice! the configure_with
that was "reconciling" limits in the external storage, now it is a perfect match for the in-memory limits.
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.
owesome!
@@ -219,6 +249,40 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { | |||
} | |||
}; | |||
|
|||
let _watcher = if let Ok(limits_file_path) = env::var(LIMITS_FILE_ENV) { |
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.
nothing to be done with the watcher? maybe register a signal to stop watching?
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.
Well that's what this does… RAII… i.e. we keep it around, and things get cleaned up upon drop()
, e.g. in the case of linux, that'd be here.
.drop()
is automatically invoked when the thing gets out of scope and can safely be dropped. Not keeping it around, it be dropped immediately…
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.
btw, this is very much like defer
al as you'd know it from golang… only it's automatic so that you cannot forget to tell it to release the resources!
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.
Ok, let me verify if I understood.
When main()
function run, the execution point is blocked at
run_http_server(&http_api_address, rate_limiter.clone()).await?;
When the process gets killed (with a signal), the execution continues in main
returning Ok(())
. Then, _watcher
which is an Option holding the watcher, gets out of scope and dropped. Notify lib will clean up at that moment.
is that right?
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.
Yes that's exactly it. Or to be real pedantic, while you hit the return, the function doesn't return until .drop()
has itself returned on the Option
(that if it owns the Watcher
eventually delegates to its drop()
)…
The doc might provide more clarity than my explanation here… otherwise (and I recommend it in all cases) Rust for Rustaceans is excellent, and the free first chapter has the part on Drop
and drop order on page 9!
process::exit(1) | ||
} | ||
|
||
let limiter = Arc::clone(&rate_limiter); |
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 wonder if this is the same as
let limiter = rate_limiter.clone();
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.
As per the documentation, it is indeed:
// The two syntaxes below are equivalent.
let a = foo.clone();
let b = Arc::clone(&foo);
Dunno why I used one over the other… which ever you prefer ¯_(ツ)_/¯
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.
👍 thanks for the doc ref
Two more questions:
|
No the event triggers only when observed.
That's what the Do you want to do this as part of this PR? I have one question about this tho: should we have a default? If so which? I'd create another issue & PR for this, with #78 being merged… this is the behavior you'd get running |
I would say a different PR. For the remaining questions: I agree that and env var should always be optional. The main question is, do we want to have this limits file path as a required input to the process? If yes, then I would go to command line arg. The libraries to manage command line args usually have clear mechanisms to flag some command line arg as |
Looks like we have a similar view on this :) |
See issue #74
What has changed
Testing it locally
$ cargo build
$ LIMITS_FILE=limitador-server/examples/limits.yaml RUST_LOG=info ./target/debug/limitador-server
http://localhost:8080/limits/test_namespace
limitador-server/examples/limits.yaml
and save