chore: use sync.WaitGroup.Go when possible (#1996)

* chore: use sync.WaitGroup.Go when possible

* Update internal/watcher/watcher.go

Co-authored-by: Alexandre Daubois <2144837+alexandre-daubois@users.noreply.github.com>

---------

Co-authored-by: Alexandre Daubois <2144837+alexandre-daubois@users.noreply.github.com>
This commit is contained in:
Kévin Dunglas
2025-11-20 11:48:18 +01:00
committed by GitHub
parent ea042637e6
commit c93729e136
3 changed files with 14 additions and 15 deletions

View File

@@ -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) {

View File

@@ -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

View File

@@ -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()
}()
})
}
}