Version: 2.11

批处理器

批处理器可用于聚合条目(日志/任何数据)并进行批处理。 当 batch_max_size 设置为零时,处理器将立即执行每个条目。将批处理的最大值设置为大于 1 将开始聚合条目,直到达到最大值或超时。

配置

创建批处理器的唯一必需参数是函数。当批处理达到最大值或缓冲区持续时间超过时,函数将被执行。

名称必选项描述
name可选的标识批处理者的唯一标识符
batch_max_size可选的每批的最大大小,默认为 1000
inactive_timeout可选的如果不活动,将刷新缓冲区的最大时间(以秒为单位),默认值为 5
buffer_duration可选的必须先处理批次中最旧条目的最大期限(以秒为单位),默认是 5
max_retry_count可选的从处理管道中移除之前的最大重试次数;默认为 0
retry_delay可选的如果执行失败,应该延迟进程执行的秒数;默认为 1

以下代码显示了如何使用批处理程序的示例。批处理器将一个要执行的函数作为第一个参数,将批处理配置作为第二个参数。

  1. local bp = require("apisix.utils.batch-processor")
  2. local func_to_execute = function(entries)
  3. -- serialize to json array core.json.encode(entries)
  4. -- process/send data
  5. return true
  6. end
  7. local config = {
  8. max_retry_count = 2,
  9. buffer_duration = 60,
  10. inactive_timeout = 5,
  11. batch_max_size = 1,
  12. retry_delay = 0
  13. }
  14. local batch_processor, err = bp:new(func_to_execute, config)
  15. if batch_processor then
  16. batch_processor:push({hello='world'})
  17. end

注意:请确保批处理的最大值(条目数)在函数执行的范围内。 刷新批处理的计时器基于 inactive_timeout 配置运行。因此,为了获得最佳使用效果, 保持 inactive_timeout 小于 buffer_duration