mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-26 21:05:30 +00:00
add mutex for router operations
This commit is contained in:
@@ -91,6 +91,7 @@ type showbridgeApp struct {
|
|||||||
router *showbridge.Router
|
router *showbridge.Router
|
||||||
routerRunner *sync.WaitGroup
|
routerRunner *sync.WaitGroup
|
||||||
tracer trace.Tracer
|
tracer trace.Tracer
|
||||||
|
routerMutex sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func readConfig(configPath string) (config.Config, error) {
|
func readConfig(configPath string) (config.Config, error) {
|
||||||
@@ -182,11 +183,13 @@ func run(ctx context.Context, c *cli.Command) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to initialize router: %w", err)
|
return fmt.Errorf("failed to initialize router: %w", err)
|
||||||
}
|
}
|
||||||
|
showbridgeApp.routerMutex.Lock()
|
||||||
showbridgeApp.router = router
|
showbridgeApp.router = router
|
||||||
|
|
||||||
showbridgeApp.routerRunner.Go(func() {
|
showbridgeApp.routerRunner.Go(func() {
|
||||||
router.Start(context.Background())
|
router.Start(context.Background())
|
||||||
})
|
})
|
||||||
|
showbridgeApp.routerMutex.Unlock()
|
||||||
|
|
||||||
go showbridgeApp.handleHangup()
|
go showbridgeApp.handleHangup()
|
||||||
|
|
||||||
@@ -208,6 +211,7 @@ func (app *showbridgeApp) handleHangup() {
|
|||||||
app.logger.Error("failed to reload configuration", "error", err)
|
app.logger.Error("failed to reload configuration", "error", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
app.routerMutex.Lock()
|
||||||
app.router.Stop()
|
app.router.Stop()
|
||||||
app.routerRunner.Wait()
|
app.routerRunner.Wait()
|
||||||
app.router = newRouter
|
app.router = newRouter
|
||||||
@@ -215,6 +219,7 @@ func (app *showbridgeApp) handleHangup() {
|
|||||||
app.router.Start(context.Background())
|
app.router.Start(context.Background())
|
||||||
})
|
})
|
||||||
app.logger.Info("configuration reloaded successfully")
|
app.logger.Info("configuration reloaded successfully")
|
||||||
|
app.routerMutex.Unlock()
|
||||||
case <-app.ctx.Done():
|
case <-app.ctx.Done():
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user