Understanding Edge Function CPU limits
了解 Edge Functions 如何管理 CPU 资源,以及当达到限制时会发生什么。
🌐 Learn how Edge Functions manage CPU resources and what happens when limits are reached.
隔离物是如何工作的 #
🌐 How isolates work
一个隔离体就像一个可以处理多个函数请求的工作者。它会一直工作,直到达到 400 秒的时间限制。边缘函数使用带有软 CPU 和硬 CPU 限制的隔离体。
🌐 An isolate is like a worker that can handle multiple requests for a function. It works until a time limit of 400 seconds is reached. Edge Functions use isolates with soft and hard CPU limits.
软限制 #
🌐 Soft limit
当隔离器达到软上限时,它会退役。这意味着:
🌐 When the isolate hits the soft limit, it retires. This means:
- 它不会接受任何新的请求
- 它会处理完它已经在处理的请求
- 它会一直运行,直到达到 CPU 时间的硬限制或到达 400 秒的时间上限,以先到者为准
硬限制 #
🌐 Hard limit
如果在达到软限制后还有新请求:
🌐 If there are new requests after the soft limit is reached:
- 创建了一个新的隔离区来处理它们
- 原始隔离会一直持续,直到达到硬性限制或时间限制
- 这确保现有请求能完成,同时新的请求由一个新的隔离线程处理
当前限制 #
🌐 Current limits
- 挂钟时间限制: 总时长400秒
- CPU 执行时间: 200 毫秒的实际计算
当超出限制时会发生什么 #
🌐 What happens when limits are exceeded
当你的函数超过 CPU 限制时,你可能会看到:
🌐 When your function exceeds CPU limits, you may see:
- 546 错误响应
- 函数终止,原因是
CPUTime关闭 - 新实例启动时性能下降
优化 CPU 使用 #
🌐 Optimizing CPU usage
分析你的代码 #
🌐 Profile your code
找出你函数中占用 CPU 最多的部分:
🌐 Identify CPU-intensive sections in your function:
- 复杂计算
- 数据处理循环
- 加密操作
优化算法 #
🌐 Optimize algorithms
- 使用更高效的数据结构
- 缓存计算结果
- 降低算法复杂度
卸掉繁重的工作 #
🌐 Offload heavy work
- 将密集处理移到后台任务
- 使用外部服务来处理大型计算
- 把大任务拆分成小功能
额外资源 #
🌐 Additional resources