我正在用 CoffeeScript 编写类似 token 桶的东西。
class TokenBucket
constructor: (@speed, @threshold) ->
@currentVolume = 0
@interval = setInterval =>
@currentVolume += 1
if @threshold < @currentVolume
@currentVolume = @threshold
, @speed * 1000
stop: ->
clearInterval @interval
return
get: ->
if @currentVolume == 0
false
else
@currentVolume -= 1
true
对于不熟悉 CoffeeScript 的人,让我简单解释一下。构建 TokenBucket
后,它会调用 setInterval
并每隔几秒增加一次“桶”的回调。
问题是,如果用户忘记在存在之前调用 stop
,setInterval
将保持 Node.js 进程运行,因为事件循环永远不会是空的。我想做的是告诉 Node.js 这个 setInterval
并不重要,如果其他一切都完成了,你可以终止它。我怎样才能做到这一点?
最佳答案
What I want to do is to tell Node.js that this setInterval in not important, you can terminate it if everything else is done. How can I achieve that?
您正在寻找 unref
method ,其中“将允许您创建一个事件的计时器,但如果它是事件循环中唯一剩下的项目,则不会保持程序运行。”。
class TokenBucket
constructor: (@speed, @threshold) ->
…
@interval = setInterval =>
…
, @speed * 1000
@interval.unref()
但是,我想知道这是否是最好的解决方案。可能最好让 @curretnVolume
成为一个仅在需要时动态计算其值(取决于当前时间)的 getter。有点像
class TokenBucket
constructor: (@speed, @threshold) ->
@_start = Date.now()
Object.defineProperty @::, "currentVolume",
enumerable: true,
configurable: true,
get: ->
dt = (Date.now() - @_start) / 1000
Math.min @threshold, Math.floor dt/@speed
get: ->
if @currentVolume == 0
false
else
@_start = Math.max @_start, Date.now()-@threshold*@speed*1000 # not 100% accurate
@_start += @peed*1000
true
关于javascript - setInterval() 保持 Node.js 进程运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30058371/