Skip to content

Commit 4d0fb7d

Browse files
committed
refactor: simplify Init()
1 parent 5447a7a commit 4d0fb7d

File tree

2 files changed

+25
-34
lines changed

2 files changed

+25
-34
lines changed

frankenphp.go

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,9 @@ func Config() PHPConfig {
138138
}
139139
}
140140

141-
func calculateMaxThreads(opt *opt) (int, int, int, error) {
141+
func calculateMaxThreads(opt *opt) (numWorkers int, _ error) {
142142
maxProcs := runtime.GOMAXPROCS(0) * 2
143143

144-
var numWorkers int
145144
for i, w := range opt.workers {
146145
if w.num <= 0 {
147146
// https://github.com/php/frankenphp/issues/126
@@ -159,21 +158,19 @@ func calculateMaxThreads(opt *opt) (int, int, int, error) {
159158
if numThreadsIsSet && !maxThreadsIsSet {
160159
opt.maxThreads = opt.numThreads
161160
if opt.numThreads <= numWorkers {
162-
err := fmt.Errorf("num_threads (%d) must be greater than the number of worker threads (%d)", opt.numThreads, numWorkers)
163-
return 0, 0, 0, err
161+
return 0, fmt.Errorf("num_threads (%d) must be greater than the number of worker threads (%d)", opt.numThreads, numWorkers)
164162
}
165163

166-
return opt.numThreads, numWorkers, opt.maxThreads, nil
164+
return numWorkers, nil
167165
}
168166

169167
if maxThreadsIsSet && !numThreadsIsSet {
170168
opt.numThreads = numWorkers + 1
171169
if !maxThreadsIsAuto && opt.numThreads > opt.maxThreads {
172-
err := fmt.Errorf("max_threads (%d) must be greater than the number of worker threads (%d)", opt.maxThreads, numWorkers)
173-
return 0, 0, 0, err
170+
return 0, fmt.Errorf("max_threads (%d) must be greater than the number of worker threads (%d)", opt.maxThreads, numWorkers)
174171
}
175172

176-
return opt.numThreads, numWorkers, opt.maxThreads, nil
173+
return numWorkers, nil
177174
}
178175

179176
if !numThreadsIsSet {
@@ -185,21 +182,19 @@ func calculateMaxThreads(opt *opt) (int, int, int, error) {
185182
}
186183
opt.maxThreads = opt.numThreads
187184

188-
return opt.numThreads, numWorkers, opt.maxThreads, nil
185+
return numWorkers, nil
189186
}
190187

191188
// both num_threads and max_threads are set
192189
if opt.numThreads <= numWorkers {
193-
err := fmt.Errorf("num_threads (%d) must be greater than the number of worker threads (%d)", opt.numThreads, numWorkers)
194-
return 0, 0, 0, err
190+
return 0, fmt.Errorf("num_threads (%d) must be greater than the number of worker threads (%d)", opt.numThreads, numWorkers)
195191
}
196192

197193
if !maxThreadsIsAuto && opt.maxThreads < opt.numThreads {
198-
err := fmt.Errorf("max_threads (%d) must be greater than or equal to num_threads (%d)", opt.maxThreads, opt.numThreads)
199-
return 0, 0, 0, err
194+
return 0, fmt.Errorf("max_threads (%d) must be greater than or equal to num_threads (%d)", opt.maxThreads, opt.numThreads)
200195
}
201196

202-
return opt.numThreads, numWorkers, opt.maxThreads, nil
197+
return numWorkers, nil
203198
}
204199

205200
// Init starts the PHP runtime and the configured workers.
@@ -230,29 +225,25 @@ func Init(options ...Option) error {
230225
if opt.logger == nil {
231226
// set a default logger
232227
// to disable logging, set the logger to slog.New(slog.NewTextHandler(io.Discard, nil))
233-
l := slog.New(slog.NewTextHandler(os.Stdout, nil))
234-
235-
loggerMu.Lock()
236-
logger = l
237-
loggerMu.Unlock()
238-
} else {
239-
loggerMu.Lock()
240-
logger = opt.logger
241-
loggerMu.Unlock()
228+
opt.logger = slog.New(slog.NewTextHandler(os.Stdout, nil))
242229
}
243230

231+
loggerMu.Lock()
232+
logger = opt.logger
233+
loggerMu.Unlock()
234+
244235
if opt.metrics != nil {
245236
metrics = opt.metrics
246237
}
247238

248239
maxWaitTime = opt.maxWaitTime
249240

250-
totalThreadCount, workerThreadCount, maxThreadCount, err := calculateMaxThreads(opt)
241+
workerThreadCount, err := calculateMaxThreads(opt)
251242
if err != nil {
252243
return err
253244
}
254245

255-
metrics.TotalThreads(totalThreadCount)
246+
metrics.TotalThreads(opt.numThreads)
256247

257248
config := Config()
258249

@@ -265,18 +256,18 @@ func Init(options ...Option) error {
265256
logger.Warn(`Zend Max Execution Timers are not enabled, timeouts (e.g. "max_execution_time") are disabled, recompile PHP with the "--enable-zend-max-execution-timers" configuration option to fix this issue`)
266257
}
267258
} else {
268-
totalThreadCount = 1
259+
opt.numThreads = 1
269260
logger.Warn(`ZTS is not enabled, only 1 thread will be available, recompile PHP using the "--enable-zts" configuration option or performance will be degraded`)
270261
}
271262

272-
mainThread, err := initPHPThreads(totalThreadCount, maxThreadCount, opt.phpIni)
263+
mainThread, err := initPHPThreads(opt.numThreads, opt.maxThreads, opt.phpIni)
273264
if err != nil {
274265
return err
275266
}
276267

277-
regularRequestChan = make(chan *frankenPHPContext, totalThreadCount-workerThreadCount)
278-
regularThreads = make([]*phpThread, 0, totalThreadCount-workerThreadCount)
279-
for i := 0; i < totalThreadCount-workerThreadCount; i++ {
268+
regularRequestChan = make(chan *frankenPHPContext, opt.numThreads-workerThreadCount)
269+
regularThreads = make([]*phpThread, 0, opt.numThreads-workerThreadCount)
270+
for i := 0; i < opt.numThreads-workerThreadCount; i++ {
280271
convertToRegularThread(getInactivePHPThread())
281272
}
282273

phpmainthread_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,13 +282,13 @@ func TestCorrectThreadCalculation(t *testing.T) {
282282
}
283283

284284
func testThreadCalculation(t *testing.T, expectedNumThreads int, expectedMaxThreads int, o *opt) {
285-
totalThreadCount, _, maxThreadCount, err := calculateMaxThreads(o)
285+
_, err := calculateMaxThreads(o)
286286
assert.NoError(t, err, "no error should be returned")
287-
assert.Equal(t, expectedNumThreads, totalThreadCount, "num_threads must be correct")
288-
assert.Equal(t, expectedMaxThreads, maxThreadCount, "max_threads must be correct")
287+
assert.Equal(t, expectedNumThreads, o.numThreads, "num_threads must be correct")
288+
assert.Equal(t, expectedMaxThreads, o.maxThreads, "max_threads must be correct")
289289
}
290290

291291
func testThreadCalculationError(t *testing.T, o *opt) {
292-
_, _, _, err := calculateMaxThreads(o)
292+
_, err := calculateMaxThreads(o)
293293
assert.Error(t, err, "configuration must error")
294294
}

0 commit comments

Comments
 (0)