Commit c7bbcd7
authored
[Website] Fix Bad Gateway and optimize PHP workers (#3219)
## Motivation for the change, related issues
This PR fixes Bad Gateway (502) errors that occur when multiple
concurrent requests are made to the PHP request handler.
## Implementation details
1. **The primary PHP instance was not used for enqueued requests.**
Instead, all enqueued requests needed to wait for the secondary
instances, and the primary instance only served new requests directly,
never from the queue.
The fix refactors `PHPProcessManager` to a pool-based design where all
spawned instances (including primary) are added to an idle pool,
`acquirePHPInstance()` takes from the pool or spawns new ones if needed,
and instances are returned to the pool for reuse. All PHP instances
should be automatically rotated every 400 requests.
3. **Increased instance wait timeout from 5s to 30s:** When all PHP
instances are busy, new requests wait for one to become available. The
previous 5s timeout was likely a bit too short for some scenarios. This
timeout limits wait time for an available instance, not PHP execution
time.
4. **Semaphore stale resolver bug:** When a semaphore acquire timed out,
its resolver remained in the queue. Later releases would notify these
stale resolvers instead of actual waiting requests. The fix removes
timed-out resolvers from the queue.
## Testing Instructions (or ideally a Blueprint)
The fix can be verified by running concurrent requests in the browser
console:
```javascript
const iframe = document.querySelector('iframe');
const playground = iframe.contentWindow.playground;
const start = Date.now();
const requests = [
playground.run({ code: '<?php sleep(4); echo "done1";' }),
playground.run({ code: '<?php sleep(4); echo "done2";' }),
playground.run({ code: '<?php sleep(4); echo "done3";' }),
playground.run({ code: '<?php sleep(4); echo "done4";' }),
playground.run({ code: '<?php sleep(4); echo "done5";' }),
playground.run({ code: '<?php sleep(4); echo "done6";' }),
// Add more and modify sleep time as needed for testing.
];
const results = await Promise.allSettled(requests);
console.log('Total time:', Date.now() - start, 'ms');
console.log(results);
```
On `trunk`, some of these promises would be rejected. In this branch,
all should succeed.1 parent 67aa97a commit c7bbcd7
File tree
6 files changed
+200
-244
lines changed- packages
- php-wasm
- node/src/test
- universal/src/lib
- util/src/lib
- playground/wordpress/src
6 files changed
+200
-244
lines changedLines changed: 27 additions & 21 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
159 | 159 | | |
160 | 160 | | |
161 | 161 | | |
162 | | - | |
| 162 | + | |
163 | 163 | | |
164 | 164 | | |
165 | 165 | | |
| |||
169 | 169 | | |
170 | 170 | | |
171 | 171 | | |
| 172 | + | |
| 173 | + | |
172 | 174 | | |
173 | 175 | | |
174 | 176 | | |
175 | 177 | | |
| 178 | + | |
176 | 179 | | |
177 | 180 | | |
178 | 181 | | |
179 | 182 | | |
180 | | - | |
181 | | - | |
182 | | - | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
183 | 193 | | |
184 | 194 | | |
185 | | - | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
186 | 199 | | |
187 | | - | |
188 | | - | |
| 200 | + | |
189 | 201 | | |
190 | 202 | | |
191 | 203 | | |
192 | | - | |
193 | | - | |
194 | | - | |
195 | | - | |
196 | | - | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
202 | | - | |
203 | | - | |
204 | | - | |
205 | | - | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
206 | 212 | | |
207 | 213 | | |
Lines changed: 27 additions & 21 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
73 | 73 | | |
74 | 74 | | |
75 | 75 | | |
76 | | - | |
| 76 | + | |
77 | 77 | | |
78 | 78 | | |
79 | 79 | | |
| |||
83 | 83 | | |
84 | 84 | | |
85 | 85 | | |
| 86 | + | |
| 87 | + | |
86 | 88 | | |
87 | 89 | | |
88 | 90 | | |
89 | 91 | | |
| 92 | + | |
90 | 93 | | |
91 | 94 | | |
92 | 95 | | |
93 | 96 | | |
94 | | - | |
95 | | - | |
96 | | - | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
97 | 107 | | |
98 | 108 | | |
99 | | - | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
100 | 113 | | |
101 | | - | |
102 | | - | |
| 114 | + | |
103 | 115 | | |
104 | 116 | | |
105 | 117 | | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
119 | | - | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
120 | 126 | | |
121 | 127 | | |
0 commit comments