我在CoffeeScript中编写类似于令牌桶的东西.
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成为一个只在需要时动态计算其值(取决于当前时间)的吸气剂.就像是
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