You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2021/09/30 07:23:58 UTC

[GitHub] [apisix] siaron commented on issue #5159: request help: 自定义负载均衡

siaron commented on issue #5159:
URL: https://github.com/apache/apisix/issues/5159#issuecomment-930916918


   - 简易版的根据header信息进行负载. original_destination.lua
   `
   local core        = require("apisix.core")
   local pairs = pairs
   
   local _M = {}
   
   local function fetch_server_from_header(ctx, upstream)
       local key = upstream.key
       local server=ctx.var["http_" .. key]
       --TODO when server is nil .
       return server
   end
   
   function _M.new(up_nodes, upstream)
   
       local nodes_count = 0
       local servers, nodes = {}, {}
       for serv, weight in pairs(up_nodes) do
           nodes_count = nodes_count + 1
           servers[serv] = serv
       end
   
       return {
           upstream = upstream,
           get = function (ctx)
               local id
               if ctx.balancer_tried_servers then
                   if ctx.balancer_tried_servers_count == nodes_count then
                       return nil, "all upstream servers tried"
                   end
               else
                   id = fetch_server_from_header(ctx,upstream)
               end
               return servers[id]
           end,
           after_balance = function (ctx, before_retry)
               if not before_retry then
                   if ctx.balancer_tried_servers then
                       core.tablepool.release("balancer_tried_servers", ctx.balancer_tried_servers)
                       ctx.balancer_tried_servers = nil
                   end
   
                   return nil
               end
   
               if not ctx.balancer_tried_servers then
                   ctx.balancer_tried_servers = core.tablepool.fetch("balancer_tried_servers", 0, 2)
               end
   
               ctx.balancer_tried_servers[ctx.balancer_server] = true
               ctx.balancer_tried_servers_count = (ctx.balancer_tried_servers_count or 0) + 1
           end,
           before_retry_next_priority = function (ctx)
               if ctx.balancer_tried_servers then
                   core.tablepool.release("balancer_tried_servers", ctx.balancer_tried_servers)
                   ctx.balancer_tried_servers = nil
               end
   
               ctx.balancer_tried_servers_count = 0
           end,
       }
   end
   
   
   return _M
   `


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org