Skip to content

Commit c93729e

Browse files
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>
1 parent ea04263 commit c93729e

3 files changed

Lines changed: 14 additions & 15 deletions

File tree

internal/watcher/watcher.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,19 @@ func InitWatcher(ct context.Context, filePatterns []string, callback func(), slo
5353
if len(filePatterns) == 0 {
5454
return nil
5555
}
56+
5657
if watcherIsActive.Load() {
5758
return ErrAlreadyStarted
5859
}
60+
5961
watcherIsActive.Store(true)
6062
ctx = ct
6163
logger = slogger
6264
activeWatcher = &watcher{callback: callback}
63-
err := activeWatcher.startWatching(ctx, filePatterns)
64-
if err != nil {
65+
66+
if err := activeWatcher.startWatching(ctx, filePatterns); err != nil {
6567
return err
6668
}
67-
reloadWaitGroup = sync.WaitGroup{}
6869

6970
return nil
7071
}
@@ -73,6 +74,7 @@ func DrainWatcher() {
7374
if !watcherIsActive.Load() {
7475
return
7576
}
77+
7678
watcherIsActive.Store(false)
7779

7880
if logger.Enabled(ctx, slog.LevelDebug) {

phpmainthread.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,12 @@ func initPHPThreads(numThreads int, numMaxThreads int, phpIni map[string]string)
6666
}
6767

6868
// start the underlying C threads
69-
ready := sync.WaitGroup{}
70-
ready.Add(numThreads)
69+
var ready sync.WaitGroup
70+
7171
for i := 0; i < numThreads; i++ {
72-
thread := phpThreads[i]
73-
go func() {
74-
thread.boot()
75-
ready.Done()
76-
}()
72+
ready.Go(phpThreads[i].boot)
7773
}
74+
7875
ready.Wait()
7976

8077
return mainThread, nil

worker.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ var (
3636

3737
func initWorkers(opt []workerOpt) error {
3838
workers = make([]*worker, 0, len(opt))
39-
workersReady := sync.WaitGroup{}
4039
directoriesToWatch := getDirectoriesToWatch(opt)
4140
watcherIsEnabled = len(directoriesToWatch) > 0
4241

@@ -48,15 +47,16 @@ func initWorkers(opt []workerOpt) error {
4847
workers = append(workers, w)
4948
}
5049

50+
var workersReady sync.WaitGroup
51+
5152
for _, w := range workers {
52-
workersReady.Add(w.num)
5353
for i := 0; i < w.num; i++ {
5454
thread := getInactivePHPThread()
5555
convertToWorkerThread(thread, w)
56-
go func() {
56+
57+
workersReady.Go(func() {
5758
thread.state.waitFor(stateReady)
58-
workersReady.Done()
59-
}()
59+
})
6060
}
6161
}
6262

0 commit comments

Comments
 (0)