orlabs / orange

OpenResty/Nginx Gateway for API Monitoring and Management.
http://orange.sumory.com
MIT License
2.31k stars 573 forks source link

some doubts about the init operation of orange #277

Open liulei18 opened 5 years ago

liulei18 commented 5 years ago
Version

v0.7.0

Question one
-- orange/orange/orange.lua
function Orange.init(options)
    options = options or {}
    local store, config
    local status, err = pcall(function()
        local conf_file_path = options.config
        config = config_loader.load(conf_file_path)
        store = require("orange.store.mysql_store")(config.store_mysql)

        loaded_plugins = load_node_plugins(config, store)
        ngx.update_time()
        config.orange_start_at = ngx.now()
    end)

    if not status or err then
        ngx.log(ngx.ERR, "Startup error: " .. err)
        os.exit(1)
    end

    local consul = require("orange.plugins.consul_balancer.consul_balancer")
    consul.set_shared_dict_name("consul_upstream", "consul_upstream_watch")
    Orange.data = {
        store = store,
        config = config,
        consul = consul
    }

    -- init dns_client
    assert(dns_client.init())

    return config, store
end
Question two
-- orange/orange/orange.lua
function Orange.init_worker()
    -- 仅在 init_worker 阶段调用,初始化随机因子,仅允许调用一次
    math.randomseed()
    -- 初始化定时器,清理计数器等
    if Orange.data and Orange.data.store and Orange.data.config.store == "mysql" then
            local ok, err = ngx.timer.at(0, function(premature, store, config)
                local available_plugins = config.plugins
                for _, v in ipairs(available_plugins) do
                    local load_success = dao.load_data_by_mysql(store, v)
                    if not load_success then
                        os.exit(1)
                    end

                    if v == "consul_balancer" then
                        for ii,p in ipairs(loaded_plugins) do
                            if v == p.name then
                                p.handler.db_ready()
                            end
                        end
                    end
                end
            end, Orange.data.store, Orange.data.config)

            if not ok then
                ngx.log(ngx.ERR, "failed to create the timer: ", err)
                return os.exit(1)
            end
    end

    for _, plugin in ipairs(loaded_plugins) do
        plugin.handler:init_worker()
    end
end
Question three
-- orange/orange/orange.lua
local function load_node_plugins(config, store)
    ngx.log(ngx.DEBUG, "Discovering used plugins")

    local sorted_plugins = {}
    local plugins = config.plugins

    for _, v in ipairs(plugins) do
        local loaded, plugin_handler = utils.load_module_if_exists("orange.plugins." .. v .. ".handler")
        if not loaded then
            -- ngx.log(ngx.WARN, "The following plugin is not installed or has no handler: " .. v)
            ngx.log(ngx.ERR, "The following plugin is not installed or has no handler: " .. v)
        else
            ngx.log(ngx.DEBUG, "Loading plugin: " .. v)
            table_insert(sorted_plugins, {
                name = v,
                handler = plugin_handler(store),
            })
        end
    end

    table_sort(sorted_plugins, function(a, b)
        local priority_a = a.handler.PRIORITY or 0
        local priority_b = b.handler.PRIORITY or 0
        return priority_a > priority_b
    end)

    return sorted_plugins
end
EasonFeng5870 commented 5 years ago

For first question, I think consul_balancer is a plugin for replacing current balancer plugin. And I think the consul_balancer authot isn't the very first author who design these framework. I think you don't need to put your attention on this~