hot config reload with OS signal
Imagine a scenario where you have a monolithic application which uses a config file to store information about log directories, cert dirs and other service information. As an example to it following config file (- it is taken and modified from Haaukins project which I work on- ) can be considered:
In this config file we have some set of keys which are defined to be used inside the application, however let’s say we would like to update some values from the config file. Then in normal cases (-if no hot reload kind of function implemented- ), user needs to restart entire application. It means application will have some down time, it may be less or more however it is not good way of doing it, in particular to update only a value from config file.
In this point, os signals can be used to update config file without restarting or closing the application. There are some other libraries which it is possible to enable watch on config file mode. It means the library will immediately notify entire application when there is change on the config file. However in this scenario, I assume that there is no such a library or framework is integrated.
Here I am considering the situation from Go language perspective, this may differ or not needed for some programming languages or frameworks.
Channel and go routine will be used to listen any SIGHUP signal to the process of the application.
First of all, it is nice to create the function which will re-assign config variable of the application when SIGHUP signal received.
When SIGNUP signal received by user given function above needs to be called to update configuration file.
Here is the code which listens any SIGHUP signal to the application
This function can be called before or after the application started. It can be called as shown below:
Then it can be tested with :
kill -SIGHUP <process-id>
It will create SIGHUP signal on the process to call
This is how OS Signal can be used to update configuration file in an application which is written in Go, when you do not have already implemented library or framework.
Originally published at https://mrturkmen.com on April 18, 2021.