title: HAProxy execute one of the method in Lua script only once
body: <p>SO I have configured HAProxy to execute global Lua script whenever a request comes in.</p>
  <p><strong>haproxy.cfg</strong></p>
  <pre><code>global
          lua-load /etc/haproxy/route_req.lua
          log 127.0.0.1:514 local0
          chroot /var/lib/haproxy
          stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners
          stats timeout 30s
          user haproxy
          group haproxy
          daemon
  </code></pre>
  <p><strong>route_req.lua</strong></p>
  <pre><code>ConsistentHashing = { num_machines = 0, num_replicas = 0, hash_tuples = {}}
  
  function ConsistentHashing:new(num_machines, num_replicas)
          o = o or {}
          setmetatable(o, self)
  
          self.num_machines = num_machines or 0
          self.num_replicas = num_replicas or 0
  
          for j = 1, self.num_machines, 1
          do
                  for k = 1, self.num_replicas, 1
                  do
                          table.insert(self.hash_tuples, {j,k, getHash(j .. &quot;_&quot; .. k)})
                  end
          end
  end
  
  
  
   function ConsistentHashing:getMachine(hash_value)
          local assigned_machine = 1
          // My code goes here
          return assigned_machine
      
  
   local function getIP(txn)
          local clientIP = txn.f:src()
          cs = ConsistentHashing:new(4, 3) # this value will keep changing
          return cs.getMachine(getHash(clientIP))
          
  end
  
  core.register_fetches('routeIP', getIP)
  
      
  </code></pre>
  <p>Now if you see here, this line <code>cs = ConsistentHashing:new(4, 3) </code> will always return the same result for all the requests I make, I want this to be done only one and for every request I just want to call <code>cs.getMachine(getHash(clientIP))</code>.</p>
  <p>SO in summary, whenever my script is called I need the object to be created just once and for every new request I want the same object to call my getMachine function.
  How can I do this using Lua in HAproxy?</p>