我是靠谱客的博主 聪慧蓝天,最近开发中收集的这篇文章主要介绍APISIX源码解析-执行阶段【http_header_filter_phase、http_body_filter_phase、http_log_phase、http_balancer_phase】,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

在这里插入图片描述

http_header_filter_phase

function _M.http_header_filter_phase()
    if ngx_var.ctx_ref ~= '' then
        -- prevent for the table leak
        local stash_ctx = fetch_ctx()

        -- internal redirect, so we should apply the ctx
        if ngx_var.from_error_page == "true" then
            ngx.ctx = stash_ctx
        end
    end

    core.response.set_header("Server", ver_header)

    local up_status = get_var("upstream_status")
    if up_status and #up_status == 3
       and tonumber(up_status) >= 500
       and tonumber(up_status) <= 599
    then
        set_resp_upstream_status(up_status)
    elseif up_status and #up_status > 3 then
        -- the up_status can be "502, 502" or "502, 502 : "
        local last_status
        if str_byte(up_status, -1) == str_byte(" ") then
            last_status = str_sub(up_status, -6, -3)
        else
            last_status = str_sub(up_status, -3)
        end

        if tonumber(last_status) >= 500 and tonumber(last_status) <= 599 then
            set_resp_upstream_status(up_status)
        end
    end

    common_phase("header_filter")
end

1、设置header: “Server:APISIX”

2、设置上游状态header:X-APISIX-Upstream-Status

3、执行插件中 的“header_filter” 阶段

http_body_filter_phase

function _M.http_body_filter_phase()
    common_phase("body_filter")
end

执行插件中的“body_filter” 阶段

http_log_phase

function _M.http_log_phase()
    if ngx_var.ctx_ref ~= '' then
        -- prevent for the table leak
        local stash_ctx = fetch_ctx()

        -- internal redirect, so we should apply the ctx
        if ngx_var.from_error_page == "true" then
            ngx.ctx = stash_ctx
        end
    end

    local api_ctx = common_phase("log")
    if not api_ctx then
        return
    end

    healthcheck_passive(api_ctx)

    if api_ctx.server_picker and api_ctx.server_picker.after_balance then
        api_ctx.server_picker.after_balance(api_ctx, false)
    end

    if api_ctx.uri_parse_param then
        core.tablepool.release("uri_parse_param", api_ctx.uri_parse_param)
    end

    core.ctx.release_vars(api_ctx)
    if api_ctx.plugins then
        core.tablepool.release("plugins", api_ctx.plugins)
    end

    if api_ctx.curr_req_matched then
        core.tablepool.release("matched_route_record", api_ctx.curr_req_matched)
    end

    core.tablepool.release("api_ctx", api_ctx)
end

1、执行插件中的“log” 阶段

2、release相关缓存

http_balancer_phase

function _M.http_balancer_phase()
    local api_ctx = ngx.ctx.api_ctx
    if not api_ctx then
        core.log.error("invalid api_ctx")
        return core.response.exit(500)
    end

    load_balancer.run(api_ctx.matched_route, api_ctx, common_phase)
end

1、设置请求超时

    if timeout then
        local ok, err = set_timeouts(timeout.connect, timeout.send,
                                     timeout.read)
        if not ok then
            core.log.error("could not set upstream timeouts: ", err)
        end
    end

2、设置重试次数

未设置,则默认为上游个数-1

   local retries = up_conf.retries
   if not retries or retries < 0 then
       retries = #up_conf.nodes - 1
   end

3、选择上游

4、set_current_peer设置 proxy server

最后

以上就是聪慧蓝天为你收集整理的APISIX源码解析-执行阶段【http_header_filter_phase、http_body_filter_phase、http_log_phase、http_balancer_phase】的全部内容,希望文章能够帮你解决APISIX源码解析-执行阶段【http_header_filter_phase、http_body_filter_phase、http_log_phase、http_balancer_phase】所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(49)

评论列表共有 0 条评论

立即
投稿
返回
顶部