fix(vitepress): 构建阶段避免限流演示定时器常驻
原因:RateLimitingDemo 在脚本顶层直接启动 setInterval,SSR 构建时也会执行,导致 Node 事件循环一直有活跃句柄,构建进程无法退出。 解决:将定时器移动到 onMounted,仅在浏览器端启动,并在 onUnmounted 中清理,避免构建阶段创建常驻任务。 结果:构建流程可正常结束,不再卡住。
This commit is contained in:
@@ -221,7 +221,7 @@
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { ref, computed, reactive } from 'vue'
|
||||
import { ref, computed, reactive, onMounted, onUnmounted } from 'vue'
|
||||
|
||||
const currentAlgo = ref('token')
|
||||
const isMatching = ref(false)
|
||||
@@ -447,19 +447,36 @@ const getStatusEmoji = (status) => {
|
||||
}
|
||||
}
|
||||
|
||||
// 定时补充令牌
|
||||
setInterval(() => {
|
||||
if (bucketState.tokens < bucketState.capacity) {
|
||||
bucketState.tokens = Math.min(bucketState.tokens + bucketState.rate, bucketState.capacity)
|
||||
}
|
||||
}, 1000)
|
||||
let tokenInterval = null
|
||||
let leakyInterval = null
|
||||
|
||||
// 定时漏桶流出
|
||||
setInterval(() => {
|
||||
if (leakyState.current > 0) {
|
||||
leakyState.current = Math.max(0, leakyState.current - leakyState.rate)
|
||||
onMounted(() => {
|
||||
tokenInterval = setInterval(() => {
|
||||
if (bucketState.tokens < bucketState.capacity) {
|
||||
bucketState.tokens = Math.min(
|
||||
bucketState.tokens + bucketState.rate,
|
||||
bucketState.capacity
|
||||
)
|
||||
}
|
||||
}, 1000)
|
||||
|
||||
leakyInterval = setInterval(() => {
|
||||
if (leakyState.current > 0) {
|
||||
leakyState.current = Math.max(0, leakyState.current - leakyState.rate)
|
||||
}
|
||||
}, 1000)
|
||||
})
|
||||
|
||||
onUnmounted(() => {
|
||||
if (tokenInterval) {
|
||||
clearInterval(tokenInterval)
|
||||
tokenInterval = null
|
||||
}
|
||||
}, 1000)
|
||||
if (leakyInterval) {
|
||||
clearInterval(leakyInterval)
|
||||
leakyInterval = null
|
||||
}
|
||||
})
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
|
||||
Reference in New Issue
Block a user