@@ -4,6 +4,7 @@ package frankenphp
44import "C"
55import (
66 "fmt"
7+ "sort"
78 "sync"
89 "time"
910
@@ -24,12 +25,12 @@ type worker struct {
2425}
2526
2627var (
27- workers map [string ]* worker
28+ workers map [string ][] * worker
2829 watcherIsEnabled bool
2930)
3031
3132func initWorkers (opt []workerOpt ) error {
32- workers = make (map [string ]* worker , len (opt ))
33+ workers = make (map [string ][] * worker , len (opt ))
3334 workersReady := sync.WaitGroup {}
3435 directoriesToWatch := getDirectoriesToWatch (opt )
3536 watcherIsEnabled = len (directoriesToWatch ) > 0
@@ -84,7 +85,17 @@ func newWorker(o workerOpt) (*worker, error) {
8485 requestChan : make (chan * frankenPHPContext ),
8586 moduleID : o .moduleID ,
8687 }
87- workers [absFileName ] = w
88+
89+ // Check if we already have workers for this filename
90+ if _ , ok := workers [absFileName ]; ! ok {
91+ workers [absFileName ] = make ([]* worker , 0 )
92+ }
93+ workers [absFileName ] = append (workers [absFileName ], w )
94+
95+ // Sort workers by descending moduleID, this way FrankenPHPApp::ServeHTTP will prefer a module-specific worker over a global one
96+ sort .Slice (workers [absFileName ], func (i , j int ) bool {
97+ return workers [absFileName ][i ].moduleID > workers [absFileName ][j ].moduleID
98+ })
8899
89100 return w , nil
90101}
@@ -97,23 +108,25 @@ func DrainWorkers() {
97108func drainWorkerThreads () []* phpThread {
98109 ready := sync.WaitGroup {}
99110 drainedThreads := make ([]* phpThread , 0 )
100- for _ , worker := range workers {
101- worker .threadMutex .RLock ()
102- ready .Add (len (worker .threads ))
103- for _ , thread := range worker .threads {
104- if ! thread .state .requestSafeStateChange (stateRestarting ) {
105- // no state change allowed == thread is shutting down
106- // we'll proceed to restart all other threads anyways
107- continue
111+ for _ , workersList := range workers {
112+ for _ , worker := range workersList {
113+ worker .threadMutex .RLock ()
114+ ready .Add (len (worker .threads ))
115+ for _ , thread := range worker .threads {
116+ if ! thread .state .requestSafeStateChange (stateRestarting ) {
117+ // no state change allowed == thread is shutting down
118+ // we'll proceed to restart all other threads anyways
119+ continue
120+ }
121+ close (thread .drainChan )
122+ drainedThreads = append (drainedThreads , thread )
123+ go func (thread * phpThread ) {
124+ thread .state .waitFor (stateYielding )
125+ ready .Done ()
126+ }(thread )
108127 }
109- close (thread .drainChan )
110- drainedThreads = append (drainedThreads , thread )
111- go func (thread * phpThread ) {
112- thread .state .waitFor (stateYielding )
113- ready .Done ()
114- }(thread )
128+ worker .threadMutex .RUnlock ()
115129 }
116- worker .threadMutex .RUnlock ()
117130 }
118131 ready .Wait ()
119132
0 commit comments