mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-26 12:55:29 +00:00
block config updates if one is already in progress
This commit is contained in:
4
api.go
4
api.go
@@ -85,6 +85,10 @@ func (r *Router) handleConfigHTTP(w http.ResponseWriter, req *http.Request) {
|
|||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
w.Write(configJSON)
|
w.Write(configJSON)
|
||||||
case http.MethodPut:
|
case http.MethodPut:
|
||||||
|
if r.updatingConfig {
|
||||||
|
http.Error(w, "Config update in progress.", http.StatusConflict)
|
||||||
|
return
|
||||||
|
}
|
||||||
var newConfig config.Config
|
var newConfig config.Config
|
||||||
err := json.NewDecoder(req.Body).Decode(&newConfig)
|
err := json.NewDecoder(req.Body).Decode(&newConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ type Router struct {
|
|||||||
apiServer *http.Server
|
apiServer *http.Server
|
||||||
apiServerMu sync.Mutex
|
apiServerMu sync.Mutex
|
||||||
apiServerShutdown context.CancelFunc
|
apiServerShutdown context.CancelFunc
|
||||||
|
updatingConfig bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Router) addModule(moduleDecl config.ModuleConfig) error {
|
func (r *Router) addModule(moduleDecl config.ModuleConfig) error {
|
||||||
@@ -122,6 +123,7 @@ func NewRouter(routerConfig config.Config) (*Router, []module.ModuleError, []rou
|
|||||||
ConfigChange: make(chan config.Config, 1),
|
ConfigChange: make(chan config.Config, 1),
|
||||||
logger: slog.Default().With("component", "router"),
|
logger: slog.Default().With("component", "router"),
|
||||||
runningConfig: routerConfig,
|
runningConfig: routerConfig,
|
||||||
|
updatingConfig: false,
|
||||||
}
|
}
|
||||||
router.logger.Debug("creating")
|
router.logger.Debug("creating")
|
||||||
|
|
||||||
@@ -318,6 +320,10 @@ func (r *Router) RunningConfig() config.Config {
|
|||||||
func (r *Router) UpdateConfig(newConfig config.Config) ([]module.ModuleError, []route.RouteError) {
|
func (r *Router) UpdateConfig(newConfig config.Config) ([]module.ModuleError, []route.RouteError) {
|
||||||
r.runningConfigMu.Lock()
|
r.runningConfigMu.Lock()
|
||||||
defer r.runningConfigMu.Unlock()
|
defer r.runningConfigMu.Unlock()
|
||||||
|
r.updatingConfig = true
|
||||||
|
defer func() {
|
||||||
|
r.updatingConfig = false
|
||||||
|
}()
|
||||||
oldConfig := r.runningConfig
|
oldConfig := r.runningConfig
|
||||||
r.logger.Debug("received config update", "oldConfig", oldConfig, "newConfig", newConfig)
|
r.logger.Debug("received config update", "oldConfig", oldConfig, "newConfig", newConfig)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user