Prevent slow shutdown caused by waiting full metrics export interval #1827
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.
We noticed that when using the new metrics functionality in the library that our Rails application now took longer to shut down. We want to export metrics every 60 seconds, but we don't want to wait an additional 60 seconds for the app to stop. This was caused by the shutdown logic in
PeriodicMetricReaderwhich worked by setting@continueto false, indicating to the loop instartto no longer continue sleeping and writing. However, this meant that on shutdown, it takes a full@export_intervalin order to "notice" that@continueis false an the program should exit.With this change, upon
shutdown, the program performs a finalexportand then kills the writer thread. This is an approach suggested by @xuan-cao-swi in #1662 (comment).Some questions:
killneed to acquire the lock first?@continueto false, if@continueis no longer used to control shutdown behavior?@continueeven needed any more?ConditionVariablewithwait(@export_mutex, @export_interval)in the writing loop, andsignal()inshutdown? This would allow the writing loop "wake up" responsively on shutdown, while ordinarily waiting@export_intervalbefore looping and writing again.