Is there an option to generate a random string of a specific length?
If you want something to uniquely identify requests, you should use uuid.
Otherwise you can generate random numbers with rand.
If you want a random string, you probably need to work with LUA.
Hi @lukastribus, you wrote work with Lua when need a random string.
I tried do that but without success. What I wanted to get is is 8-char length string returned into unique-id format HaProxy property.
unique-id-format %[lua.randomString(16)]
This is an example function that returns a string with the specified value:
math.randomseed(os.time())
local character_set = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
local string_sub = string.sub
local math_random = math.random
local table_concat = table.concat
local character_set_amount = #character_set
local number_one = 1
local default_length = 10
local function generate_key(length)
local random_string = {}
for int = number_one, length or default_length do
local random_number = math_random(number_one, character_set_amount)
local character = string_sub(character_set, random_number, random_number)
random_string[#random_string + number_one] = character
end
return table_concat(random_string)
end
core.register_action('randomString', generate_key)
This function is working in local ula environment and returning a proper string.
The issue is when I load this lua file into HaProxy:
lua-load /etc/haproxy/hello.lua
then this statement
unique-id-format %[lua.randomString(16)]
is return nothing.
I have tried to register lua function for all contexts (task, action, fetch, converter) but without the expected result.
I will be very grateful if you leave any comment helping me achieve my goal.
Or maybe @f1outsourcing , you have resolved your case and want to share working solution?
Thanks!