APISIX源码解析-执行阶段【init_worker】

APISIX源码解析-执行阶段【init_worker】

ngx_lua 模块执行顺序与阶段

在这里插入图片描述

init_worker()

1、初始化woker.event
local ok, err = we.configure({shm = "worker-events", interval = 0.1})
2、初始化注册中心
    local discovery = require("apisix.discovery.init").discovery
    if discovery and discovery.init_worker then
        discovery.init_worker()
    end

注册中心支持:consul、dns、eureka、nacos
example:

#discovery:                       # service discovery center
#  dns:
#    resolver:
#      - "127.0.0.1:8600"         # use the real address of your dns server
#  eureka:
#    host:                        # it's possible to define multiple eureka hosts addresses of the same eureka cluster.
#      - "http://127.0.0.1:8761"
#    prefix: "/eureka/"
#    fetch_interval: 30           # default 30s
#    weight: 100                  # default weight for node
#    timeout:
#      connect: 2000              # default 2000ms
#      send: 2000                 # default 2000ms
#      read: 5000                 # default 5000ms
3、初始化balancer
    require("apisix.balancer").init_worker()
    # 这边没有实现任何方法
    function _M.init_worker()
    end
4、初始化admin route
router = route.new(uri_route)
local uri_route = {
    {
        paths = [[/apisix/admin/*]],
        methods = {"GET", "PUT", "POST", "DELETE", "PATCH"},
        handler = run,
    },
    {
        paths = [[/apisix/admin/stream_routes/*]],
        methods = {"GET", "PUT", "POST", "DELETE", "PATCH"},
        handler = run_stream,
    },
    {
        paths = [[/apisix/admin/plugins/list]],
        methods = {"GET"},
        handler = get_plugins_list,
    },
    {
        paths = reload_event,
        methods = {"PUT"},
        handler = post_reload_plugins,
    },
}
## 同时会同步一次插件到etcd中
sync_local_conf_to_etcd(true)
5、初始化后台定时器timer
local timer, err = core.timer.new("background", background_timer, opts)
6、初始化plugin

将config-default.yaml 和 config.yaml 文件中配置的插件放到 local_plugins 和 local_plugins_hash中,同时local_plugins_hash按优先级排序

local ok, err = load(http_plugin_names)
local function load(plugin_names)
    local processed = {}
    for _, name in ipairs(plugin_names) do
        if processed[name] == nil then
            processed[name] = true
        end
    end

    core.log.warn("new plugins: ", core.json.delay_encode(processed))
    #### 先删除
    for name in pairs(local_plugins_hash) do
        unload_plugin(name)
    end

    core.table.clear(local_plugins)
    core.table.clear(local_plugins_hash)
    ###  再加载
    for name in pairs(processed) do
        load_plugin(name, local_plugins)
    end

    -- sort by plugin's priority
    if #local_plugins > 1 then
        sort_tab(local_plugins, sort_plugin)
    end
    ###  再优先级排序 加入 local_plugins_hash
    for i, plugin in ipairs(local_plugins) do
        local_plugins_hash[plugin.name] = plugin
        if local_conf and local_conf.apisix
           and local_conf.apisix.enable_debug then
            core.log.warn("loaded plugin and sort by priority:",
                          " ", plugin.priority,
                          " name: ", plugin.name)
        end
    end

    _M.load_times = _M.load_times + 1
    core.log.info("load plugin times: ", _M.load_times)
    return true
end
7、初始化router
7.1 默认使用radixtree_uri
7.2 如果路由器不提供其自定义实现,择附加通用方法
-- attach common methods if the router doesn't provide its custom implementation
local function attach_http_router_common_methods(http_router)
    if http_router.routes == nil then
        http_router.routes = function ()
            if not http_router.user_routes then
                return nil, nil
            end

            local user_routes = http_router.user_routes
            return user_routes.values, user_routes.conf_version
        end
    end

    if http_router.init_worker == nil then
        http_router.init_worker = function (filter)
            http_router.user_routes = http_route.init_worker(filter)
        end
    end
end
7.2 初始化router_http、router_ssl

同时etcd创建 /routes、/ssl、/global_rules

7.3 初始化services
7.4 初始化plugin_configs

etcd创建/plugin_configs,即

_M.plugin_config = {
    type = "object",
    properties = {
        id = id_schema,
        desc = desc_def,
        plugins = plugins_schema,
        labels = labels_def,
        create_time = timestamp_def,
        update_time = timestamp_def
    },
    required = {"id", "plugins"},
}
7.5 初始化consumer
7.6 如果使用的时静态yaml方式启动,则开启定时器,每个1s同步数据
7.7 初始化upstream
7.8 初始化 ext-plugin
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
kthread_park和kthread_init_worker都是Linux内核提供的用于创建内核线程的函数,但它们的功能和用法不同。 kthread_park是一个较新的函数,它可以创建一个内核线程,并使其处于休眠状态,直到有任务需要执行时再被唤醒。kthread_park的函数原型如下: ``` struct task_struct *kthread_park(struct kthread_park_info *park); ``` 其中,kthread_park_info结构体定义了休眠等待的条件和超时时间等信息。kthread_park会返回一个指向task_struct结构体的指针,该结构体描述了内核线程的各种属性。 kthread_park适用于需要等待事件发生的场景,比如等待硬件中断、等待网络数据等。使用kthread_park创建的线程会自动进入休眠状态,不会占用CPU资源,等待唤醒后才会再次执行。 kthread_init_worker是一个较早的函数,它创建的内核线程主要用于执行一些后台任务,比如文件系统的后台清理、网络数据包的处理等。kthread_init_worker的函数原型如下: ``` struct task_struct *kthread_init_worker(int (*func)(void *data), void *data, const char *namefmt, ...); ``` 其中,func是内核线程的入口函数,data是传递给线程的参数,namefmt是线程的名称。kthread_init_worker会返回一个指向task_struct结构体的指针,该结构体描述了内核线程的各种属性。 kthread_init_worker创建的线程会立即执行,不会进入休眠状态。它通常用于执行一些需要在后台运行的任务,可以通过多线程编程技术来实现并发执行。 需要注意的是,kthread_park和kthread_init_worker都需要在内核中使用,不能直接在用户空间中调用。使用这两个函数创建内核线程时,需要注意线程同步和互斥的问题,比如使用信号量、互斥锁等机制来保护共享资源,避免竞态条件等问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值