Commit adbb4ec
feat: config reloading (#1771)
## What kind of change does this PR introduce?
File based configuration reloading using fsnotify.
## What is the current behavior?
Currently the Auth config is loaded once from the environment or file
(-c flag) and persists until the service is restarted.
## What is the new behavior?
A new optional flag (long: `--watch-dir`, short: `-w`) has been added.
When present any files with a ".env" suffix will be loaded into the
environment before the `*GlobalConfiguration` is created, otherwise
existing behavior is preserved.
In addition when the watch-dir flag is present a goroutine will be
started in serve_cmd.go and begin blocking on a call to
`(*Reloader).Watch` with a callback function that accepts a
`*conf.GlobalConfiguration object`. Each time this function is called we
create a new `*api.API` object and store it within our `AtomicHandler`,
previously given as the root handler to the `*http.Server`.
The Reloader uses some simple heuristics to deal with a few edge cases,
an overview:
- At most 1 configuration reload may happen per 10 seconds with a +-1s
margin of error.
- After a file within `--watch-dir` has changed the 10 second grace
period begins. After that it will reload the config.
- Config reloads first sort each file by name then processes them in
sequence.
- Directories within `--watch-dir` are ignored during config reloading.
- Implementation quirk: directory changes can trigger a config reload,
as I don't stat fsnotify events. This and similar superfulous reloads
could be easily fixed by storing a snapshot of `os.Environ()` after
successful reloads to compare with the latest via `slices.Equal()`
before reloading.
- Files that do not end with a `.env` suffix are ignored.
- It handles the removal or renaming of the `-watch-dir` during runtime,
but an error message will be printed every 10 seconds as long as it's
missing.
- The config file passed with -c is only loaded once. Live reloads only
read the config dir. Meaning it would be possible to create a config dir
change that results in a new final configuration on the next reload due
to the persistence of `os.Environ()`.
---------
Co-authored-by: Chris Stockton <[email protected]>1 parent d56f239 commit adbb4ec
File tree
15 files changed
+880
-93
lines changed- cmd
- internal
- api
- conf
- reloader
- testdata
15 files changed
+880
-93
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
13 | 16 | | |
14 | 17 | | |
15 | 18 | | |
| |||
22 | 25 | | |
23 | 26 | | |
24 | 27 | | |
25 | | - | |
26 | | - | |
| 28 | + | |
| 29 | + | |
27 | 30 | | |
28 | 31 | | |
29 | 32 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
6 | 9 | | |
| 10 | + | |
7 | 11 | | |
8 | 12 | | |
9 | 13 | | |
10 | 14 | | |
| 15 | + | |
11 | 16 | | |
12 | 17 | | |
13 | 18 | | |
| |||
21 | 26 | | |
22 | 27 | | |
23 | 28 | | |
24 | | - | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
25 | 38 | | |
26 | 39 | | |
27 | 40 | | |
| |||
32 | 45 | | |
33 | 46 | | |
34 | 47 | | |
35 | | - | |
36 | | - | |
37 | 48 | | |
38 | 49 | | |
39 | 50 | | |
40 | | - | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
41 | 107 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
| 39 | + | |
39 | 40 | | |
40 | 41 | | |
41 | 42 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
61 | 61 | | |
62 | 62 | | |
63 | 63 | | |
| 64 | + | |
| 65 | + | |
64 | 66 | | |
65 | 67 | | |
66 | 68 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
357 | 357 | | |
358 | 358 | | |
359 | 359 | | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| |||
647 | 648 | | |
648 | 649 | | |
649 | 650 | | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
650 | 726 | | |
651 | 727 | | |
652 | 728 | | |
653 | 729 | | |
654 | 730 | | |
655 | 731 | | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
656 | 737 | | |
| 738 | + | |
657 | 739 | | |
658 | 740 | | |
659 | 741 | | |
660 | | - | |
| 742 | + | |
661 | 743 | | |
662 | 744 | | |
663 | 745 | | |
664 | | - | |
| 746 | + | |
665 | 747 | | |
666 | 748 | | |
667 | 749 | | |
668 | | - | |
| 750 | + | |
669 | 751 | | |
670 | 752 | | |
671 | 753 | | |
672 | 754 | | |
673 | | - | |
| 755 | + | |
674 | 756 | | |
675 | 757 | | |
676 | 758 | | |
677 | 759 | | |
678 | 760 | | |
679 | | - | |
| 761 | + | |
680 | 762 | | |
681 | 763 | | |
682 | 764 | | |
683 | 765 | | |
684 | | - | |
| 766 | + | |
685 | 767 | | |
686 | 768 | | |
687 | 769 | | |
688 | 770 | | |
689 | 771 | | |
690 | | - | |
| 772 | + | |
691 | 773 | | |
692 | 774 | | |
693 | 775 | | |
694 | 776 | | |
695 | 777 | | |
696 | | - | |
| 778 | + | |
697 | 779 | | |
698 | 780 | | |
699 | 781 | | |
700 | 782 | | |
701 | 783 | | |
702 | | - | |
| 784 | + | |
703 | 785 | | |
704 | 786 | | |
705 | 787 | | |
| |||
712 | 794 | | |
713 | 795 | | |
714 | 796 | | |
715 | | - | |
| 797 | + | |
716 | 798 | | |
717 | 799 | | |
718 | 800 | | |
| |||
724 | 806 | | |
725 | 807 | | |
726 | 808 | | |
727 | | - | |
| 809 | + | |
728 | 810 | | |
729 | 811 | | |
730 | 812 | | |
731 | 813 | | |
732 | | - | |
| 814 | + | |
733 | 815 | | |
734 | 816 | | |
735 | 817 | | |
| |||
0 commit comments