Brian 11 лет назад
Родитель
Сommit
9bb534707f
1 измененных файлов с 28 добавлено и 3 удалено
  1. 28 3
      services/spelling/app/coffee/ASpellWorkerPool.coffee

+ 28 - 3
services/spelling/app/coffee/ASpellWorkerPool.coffee

@@ -1,12 +1,19 @@
 ASpellWorker = require "./ASpellWorker"
 ASpellWorker = require "./ASpellWorker"
 _ = require "underscore"
 _ = require "underscore"
+logger = require 'logger-sharelatex'
 
 
 class ASpellWorkerPool
 class ASpellWorkerPool
+	MAX_REQUESTS: 100*1024
+	MAX_WORKERS: 32
+
 	constructor: (@options) ->
 	constructor: (@options) ->
 		@PROCESS_POOL = []
 		@PROCESS_POOL = []
 		@timeout = 1000
 		@timeout = 1000
 
 
 	create: (language) ->
 	create: (language) ->
+		if @PROCESS_POOL.length >= @MAX_WORKERS
+			logger.log maxworkers: @MAX_WORKERS, "maximum number of workers already running"
+			return null
 		worker = new ASpellWorker(language, @options)
 		worker = new ASpellWorker(language, @options)
 		worker.pipe.on 'exit', () =>
 		worker.pipe.on 'exit', () =>
 			@cleanup
 			@cleanup
@@ -27,11 +34,29 @@ class ASpellWorkerPool
 		else
 		else
 			worker = availableWorker
 			worker = availableWorker
 
 
-		timer = setTimeout () ->
+		if not worker?
+			# return error if too many workers
+			callback(new Error("no worker available"))
+			return
+
+		if worker.idleTimer?
+			clearTimeout worker.idleTimer
+			worker.idleTimer = null
+
+		killTimer = setTimeout () ->
 			worker.pipe.kill('SIGKILL')
 			worker.pipe.kill('SIGKILL')
 		, timeout || 1000
 		, timeout || 1000
-		worker.check words, (err, output) ->
-			clearTimeout timer
+
+		worker.check words, (err, output) =>
+			clearTimeout killTimer
 			callback(err, output)
 			callback(err, output)
+			# queue a shutdown if worker is not used
+			if worker.count > @MAX_REQUESTS
+				worker.shutdown("reached limit of " + @MAX_REQUESTS + " requests")
+			else
+				worker.idleTimer = setTimeout () ->
+					worker.shutdown("idle worker")
+					worker.idleTimer = null
+				, 1000
 
 
 module.exports = ASpellWorkerPool
 module.exports = ASpellWorkerPool