Skip to content

Commit f39be8e

Browse files
committed
merge workers with same moduleid, better to use a single pool
1 parent c7172d2 commit f39be8e

1 file changed

Lines changed: 19 additions & 3 deletions

File tree

worker.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,16 @@ func initWorkers(opt []workerOpt) error {
3737

3838
for _, o := range opt {
3939
worker, err := newWorker(o)
40-
worker.threads = make([]*phpThread, 0, o.num)
41-
workersReady.Add(o.num)
4240
if err != nil {
4341
return err
4442
}
45-
for i := 0; i < worker.num; i++ {
43+
if worker.threads == nil {
44+
worker.threads = make([]*phpThread, 0, o.num)
45+
} else {
46+
worker.num += o.num
47+
}
48+
workersReady.Add(o.num)
49+
for i := 0; i < o.num; i++ {
4650
thread := getInactivePHPThread()
4751
convertToWorkerThread(thread, worker)
4852
go func() {
@@ -72,6 +76,18 @@ func newWorker(o workerOpt) (*worker, error) {
7276
return nil, fmt.Errorf("worker filename is invalid %q: %w", o.fileName, err)
7377
}
7478

79+
// Check if a worker with the same fileName and moduleID already exists
80+
if existingWorkers, ok := workers[absFileName]; ok {
81+
for _, existingWorker := range existingWorkers {
82+
if existingWorker.moduleID == o.moduleID {
83+
if o.moduleID == 0 {
84+
return nil, fmt.Errorf("cannot add a multiple global workers with the same filename: %s", absFileName)
85+
}
86+
return existingWorker, nil
87+
}
88+
}
89+
}
90+
7591
if o.env == nil {
7692
o.env = make(PreparedEnv, 1)
7793
}

0 commit comments

Comments
 (0)