Capturing a Connection with connect_server()


When the proxy accepts a connection from a MariaDB client, the connect_server() function is called.

There are no arguments to the function, but you can use and if necessary manipulate the information in the proxy.connection table, which is unique to each client session.

For example, if you have multiple backend servers, you can specify which server that connection should use by setting the value of proxy.connection.backend_ndx to a valid server number. The following code chooses between two servers based on whether the current time in minutes is odd or even:

function connect_server()
 print('--> a client really wants to talk to a server')
 if (tonumber(os.date('%M')) % 2 == 0) then
 proxy.connection.backend_ndx = 2
 print('Choosing backend 2')
 else
 proxy.connection.backend_ndx = 1
 print('Choosing backend 1')
 end
 print('Using ' .. proxy.global.backends[proxy.connection.backend_ndx].dst.name)
end

This example also displays the IP address/port combination by accessing the information from the internal proxy.global.backends table.

Retornar