diff --git a/internal/watcher/watcher.go b/internal/watcher/watcher.go index 798b31b9..c1b87572 100644 --- a/internal/watcher/watcher.go +++ b/internal/watcher/watcher.go @@ -53,18 +53,19 @@ func InitWatcher(ct context.Context, filePatterns []string, callback func(), slo if len(filePatterns) == 0 { return nil } + if watcherIsActive.Load() { return ErrAlreadyStarted } + watcherIsActive.Store(true) ctx = ct logger = slogger activeWatcher = &watcher{callback: callback} - err := activeWatcher.startWatching(ctx, filePatterns) - if err != nil { + + if err := activeWatcher.startWatching(ctx, filePatterns); err != nil { return err } - reloadWaitGroup = sync.WaitGroup{} return nil } @@ -73,6 +74,7 @@ func DrainWatcher() { if !watcherIsActive.Load() { return } + watcherIsActive.Store(false) if logger.Enabled(ctx, slog.LevelDebug) { diff --git a/phpmainthread.go b/phpmainthread.go index 40864206..246cb6e2 100644 --- a/phpmainthread.go +++ b/phpmainthread.go @@ -66,15 +66,12 @@ func initPHPThreads(numThreads int, numMaxThreads int, phpIni map[string]string) } // start the underlying C threads - ready := sync.WaitGroup{} - ready.Add(numThreads) + var ready sync.WaitGroup + for i := 0; i < numThreads; i++ { - thread := phpThreads[i] - go func() { - thread.boot() - ready.Done() - }() + ready.Go(phpThreads[i].boot) } + ready.Wait() return mainThread, nil diff --git a/worker.go b/worker.go index cf6ea9db..b6ac0ab0 100644 --- a/worker.go +++ b/worker.go @@ -36,7 +36,6 @@ var ( func initWorkers(opt []workerOpt) error { workers = make([]*worker, 0, len(opt)) - workersReady := sync.WaitGroup{} directoriesToWatch := getDirectoriesToWatch(opt) watcherIsEnabled = len(directoriesToWatch) > 0 @@ -48,15 +47,16 @@ func initWorkers(opt []workerOpt) error { workers = append(workers, w) } + var workersReady sync.WaitGroup + for _, w := range workers { - workersReady.Add(w.num) for i := 0; i < w.num; i++ { thread := getInactivePHPThread() convertToWorkerThread(thread, w) - go func() { + + workersReady.Go(func() { thread.state.waitFor(stateReady) - workersReady.Done() - }() + }) } }