liquid_feedback_frontend
view env/ldap/get_hosts.lua @ 1715:5ada43fc3f9b
Nicer wording
| author | bsw | 
|---|---|
| date | Mon Sep 27 12:41:07 2021 +0200 (2021-09-27) | 
| parents | 58f48a8a202a | 
| children | 
 line source
     1 -- generate a ready-to-use list of the configures LDAP servers
     2 -- --------------------------------------------------------------------------
     3 -- result is in order of preference, including a round robin style random
     4 -- selection of the preference of hosts with the same preference
     5 --
     6 -- returns:
     7 --   hostlist: flattened list of LDAP servers in order of preference
     9 function ldap.get_hosts()
    11   math.randomseed(os.time())
    13   local hostlist = {}
    15   -- iterate through all entries on base level
    16   for i, host in ipairs (config.ldap.hosts) do
    18     -- a single host entry
    19     if host.uri then
    20       hostlist[#hostlist+1] = host
    22     -- or a list of host entries
    23     else
    24       local subhostlist = {}
    26       -- sort list of host entries by random
    27       for j, subhost in ipairs(host) do
    28         subhost.priority = math.random()
    29         subhostlist[#subhostlist+1] = subhost
    30       end
    31       table.sort(subhostlist, function (a,b)
    32         return a.priority < b.priority
    33       end)
    35       -- and add them to the main list
    36       for i, subhost in ipairs(subhostlist) do
    37         hostlist[#hostlist+1] = subhost
    38       end
    40     end
    42   end
    44   return hostlist
    46 end
