Skip to content

Commit

Permalink
reload the configuration when receiving the SIGHUP signal (#2015)
Browse files Browse the repository at this point in the history
Fix #35 

This adds support for reloading configuration when receiving the SIGHUP
signal. This only works on unix-like platforms, and only with the
configuration file

Co-authored-by: Gary Pennington <gary@apollographql.com>
  • Loading branch information
Geoffroy Couprie and garypen authored Nov 2, 2022
1 parent 06aff46 commit c4cce27
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
7 changes: 7 additions & 0 deletions NEXT_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ Example of logs in JSON:

By [@bnjjj](https://github.com/bnjjj) in https://github.com/apollographql/router/pull/1982

### Reload the configuration when receiving the SIGHUP signal [Issue #35](https://github.com/apollographql/router/issues/35))

This adds support for reloading configuration when receiving the SIGHUP signal. This only works on unix-like platforms,
and only with the configuration file.

By [@Geal](https://github.com/Geal) in https://github.com/apollographql/router/pull/2015

## 🐛 Fixes

### Fix the deduplication logic in deduplication caching [Issue #1984](https://github.com/apollographql/router/issues/1984))
Expand Down
47 changes: 43 additions & 4 deletions apollo-router/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,10 +318,49 @@ impl ConfigurationSource {
.boxed()
} else {
match ConfigurationSource::read_config(&path) {
Ok(configuration) => stream::once(future::ready(UpdateConfiguration(
Box::new(configuration),
)))
.boxed(),
Ok(configuration) => {
#[cfg(any(test, not(unix)))]
{
stream::once(future::ready(UpdateConfiguration(Box::new(
configuration,
))))
.boxed()
}

#[cfg(all(not(test), unix))]
{
let mut sighup_stream = tokio::signal::unix::signal(
tokio::signal::unix::SignalKind::hangup(),
)
.expect("Failed to install SIGHUP signal handler");

let (mut tx, rx) = futures::channel::mpsc::channel(1);
tokio::task::spawn(async move {
while let Some(()) = sighup_stream.recv().await {
tx.send(()).await.unwrap();
}
});
futures::stream::select(
stream::once(future::ready(UpdateConfiguration(Box::new(
configuration,
))))
.boxed(),
rx.filter_map(
move |()| match ConfigurationSource::read_config(&path) {
Ok(configuration) => future::ready(Some(
UpdateConfiguration(Box::new(configuration)),
)),
Err(err) => {
tracing::error!("{}", err);
future::ready(None)
}
},
)
.boxed(),
)
.boxed()
}
}
Err(err) => {
tracing::error!("{}", err);
stream::empty().boxed()
Expand Down

0 comments on commit c4cce27

Please sign in to comment.