| 49 | } |
| 50 | |
| 51 | func (w *Watcher) reloadConfigIfChanged() { |
| 52 | data, err := os.ReadFile(w.configPath) |
| 53 | if err != nil { |
| 54 | log.Errorf("failed to read config file for hash check: %v", err) |
| 55 | return |
| 56 | } |
| 57 | if len(data) == 0 { |
| 58 | log.Debugf("ignoring empty config file write event") |
| 59 | return |
| 60 | } |
| 61 | sum := sha256.Sum256(data) |
| 62 | newHash := hex.EncodeToString(sum[:]) |
| 63 | |
| 64 | w.clientsMutex.RLock() |
| 65 | currentHash := w.lastConfigHash |
| 66 | w.clientsMutex.RUnlock() |
| 67 | |
| 68 | if currentHash != "" && currentHash == newHash { |
| 69 | log.Debugf("config file content unchanged (hash match), skipping reload") |
| 70 | return |
| 71 | } |
| 72 | log.Infof("config file changed, reloading: %s", w.configPath) |
| 73 | if w.reloadConfig() { |
| 74 | finalHash := newHash |
| 75 | if updatedData, errRead := os.ReadFile(w.configPath); errRead == nil && len(updatedData) > 0 { |
| 76 | sumUpdated := sha256.Sum256(updatedData) |
| 77 | finalHash = hex.EncodeToString(sumUpdated[:]) |
| 78 | } else if errRead != nil { |
| 79 | log.WithError(errRead).Debug("failed to compute updated config hash after reload") |
| 80 | } |
| 81 | w.clientsMutex.Lock() |
| 82 | w.lastConfigHash = finalHash |
| 83 | w.clientsMutex.Unlock() |
| 84 | w.persistConfigAsync() |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | func (w *Watcher) reloadConfig() bool { |
| 89 | log.Debug("=========================== CONFIG RELOAD ============================") |