2019-07-03 12:41:01 +00:00
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Sanity-check the conversion and remove this comment.
|
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS206: Consider reworking classes to avoid initClass
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2022-12-20 12:11:25 +00:00
|
|
|
import _ from 'underscore'
|
2023-05-08 13:40:37 +00:00
|
|
|
import Settings from '@overleaf/settings'
|
2022-12-20 12:11:25 +00:00
|
|
|
import logger from '@overleaf/logger'
|
|
|
|
import metrics from '@overleaf/metrics'
|
|
|
|
import OError from '@overleaf/o-error'
|
|
|
|
import { ASpellWorker } from './ASpellWorker.js'
|
2019-07-03 12:41:01 +00:00
|
|
|
|
2022-12-20 12:11:25 +00:00
|
|
|
export class ASpellWorkerPool {
|
2019-07-03 12:41:01 +00:00
|
|
|
static initClass() {
|
2023-05-08 13:40:37 +00:00
|
|
|
this.prototype.MAX_REQUESTS = Settings.maxRequestsPerWorker
|
2019-07-03 12:41:01 +00:00
|
|
|
this.prototype.MAX_WORKERS = 32
|
|
|
|
this.prototype.MAX_IDLE_TIME = 1000
|
|
|
|
this.prototype.MAX_REQUEST_TIME = 60 * 1000
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(options) {
|
|
|
|
this.options = options
|
|
|
|
this.PROCESS_POOL = []
|
|
|
|
}
|
|
|
|
|
|
|
|
create(language) {
|
|
|
|
if (this.PROCESS_POOL.length >= this.MAX_WORKERS) {
|
2022-05-16 12:38:18 +00:00
|
|
|
logger.debug(
|
2019-07-03 12:41:01 +00:00
|
|
|
{ maxworkers: this.MAX_WORKERS },
|
|
|
|
'maximum number of workers already running'
|
|
|
|
)
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
const worker = new ASpellWorker(language, this.options)
|
|
|
|
worker.pipe.on('exit', () => {
|
|
|
|
if (worker.killTimer != null) {
|
|
|
|
clearTimeout(worker.killTimer)
|
|
|
|
worker.killTimer = null
|
|
|
|
}
|
|
|
|
if (worker.idleTimer != null) {
|
|
|
|
clearTimeout(worker.idleTimer)
|
|
|
|
worker.idleTimer = null
|
|
|
|
}
|
2022-05-16 12:38:18 +00:00
|
|
|
logger.debug(
|
2019-07-03 12:41:01 +00:00
|
|
|
{ process: worker.pipe.pid, lang: language },
|
|
|
|
'removing aspell worker from pool'
|
|
|
|
)
|
|
|
|
return this.cleanup()
|
|
|
|
})
|
|
|
|
this.PROCESS_POOL.push(worker)
|
|
|
|
metrics.gauge('aspellWorkerPool-size', this.PROCESS_POOL.length)
|
|
|
|
return worker
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup() {
|
2021-07-13 11:04:47 +00:00
|
|
|
const active = this.PROCESS_POOL.filter(worker => worker.state !== 'killed')
|
2019-07-03 12:41:01 +00:00
|
|
|
this.PROCESS_POOL = active
|
|
|
|
return metrics.gauge('aspellWorkerPool-size', this.PROCESS_POOL.length)
|
|
|
|
}
|
|
|
|
|
|
|
|
check(language, words, timeout, callback) {
|
|
|
|
// look for an existing process in the pool
|
|
|
|
let worker
|
|
|
|
const availableWorker = _.find(
|
|
|
|
this.PROCESS_POOL,
|
2021-07-13 11:04:47 +00:00
|
|
|
cached => cached.language === language && cached.isReady()
|
2019-07-03 12:41:01 +00:00
|
|
|
)
|
|
|
|
if (availableWorker == null) {
|
|
|
|
worker = this.create(language)
|
|
|
|
} else {
|
|
|
|
worker = availableWorker
|
|
|
|
}
|
|
|
|
|
|
|
|
if (worker == null) {
|
|
|
|
// return error if too many workers
|
2020-06-03 09:14:44 +00:00
|
|
|
callback(new OError('no worker available'))
|
2019-07-03 12:41:01 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (worker.idleTimer != null) {
|
|
|
|
clearTimeout(worker.idleTimer)
|
|
|
|
worker.idleTimer = null
|
|
|
|
}
|
|
|
|
|
|
|
|
worker.killTimer = setTimeout(
|
|
|
|
() => worker.kill('spell check timed out'),
|
|
|
|
timeout || this.MAX_REQUEST_TIME
|
|
|
|
)
|
|
|
|
|
|
|
|
return worker.check(words, (err, output) => {
|
|
|
|
if (worker.killTimer != null) {
|
|
|
|
clearTimeout(worker.killTimer)
|
|
|
|
worker.killTimer = null
|
|
|
|
}
|
|
|
|
callback(err, output)
|
|
|
|
if (err != null) {
|
|
|
|
return
|
|
|
|
} // process has shut down
|
|
|
|
if (worker.count > this.MAX_REQUESTS) {
|
|
|
|
return worker.shutdown(`reached limit of ${this.MAX_REQUESTS} requests`)
|
|
|
|
} else {
|
|
|
|
// queue a shutdown if worker is idle
|
2020-08-10 16:23:15 +00:00
|
|
|
return (worker.idleTimer = setTimeout(function () {
|
2019-07-03 12:41:01 +00:00
|
|
|
worker.shutdown('idle worker')
|
|
|
|
return (worker.idleTimer = null)
|
|
|
|
}, this.MAX_IDLE_TIME))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ASpellWorkerPool.initClass()
|