HTTP:persist(t)
Manages persistence/stickiness table operations, including reading, writing, dumping table contents, and retrieving server information.
Syntax
HTTP:persist(t);
Arguments
| Name | Description |
|---|---|
|
t |
A table that specifies the operation and its parameters. |
Events
Applicable in the following events:
- PERSISTENCE
- POST_PERSIST
- HTTP_REQUEST (supported since version 7.2.x)
- HTTP_DATA_REQUEST, HTTP_DATA_RESPONSE (supported since version 8.0.0)
Operations
The following sections detail all possible structures for the argument table t.
-
save_tbl — Saves a server assignment for a hash value in the persistence table.
-
read_tbl — Saves a server assignment for a hash value in the persistence table.
-
dump_tbl — Dumps a range of entries from the persistence table.
-
get_valid_server — Saves a server assignment for a hash value in the persistence table.
-
cal_server_from_hash — Saves a server assignment for a hash value in the persistence table.
-
get_current_assigned_server — Saves a server assignment for a hash value in the persistence table.
save_tbl
Saves a server assignment for a hash value in the persistence table.
|
Field |
Type |
Required |
Description |
|---|---|---|---|
|
operation |
String |
Yes |
Must be "save_tbl". |
|
hash_value |
String |
Yes |
The hash key to store. |
|
srv_name |
Integer |
String |
The server name to associate with the hash. |
Example
when PERSISTENCE {
cip = HTTP:client_addr()
hash_str_cip = sha512_hex(cip)
debug("-----save_tbl-----\n")
t={}
t["operation"] = "save_tbl"
t["hash_value"] = hash_str_cip
t["srv_name"] = "pool1-2"
ret = HTTP:persist(t)
if ret then
debug("hash save table success\n");
else
debug("save table failed\n");
end
t={};
t["operation"] = "save_tbl";
t["hash_value"] = "246810";
t["srv_name"] = "pool1-3";
ret = HTTP: persist(t);
if ret then
debug("===server add success\n");
else
debug("===server add fail\n");
end
}
Output:
true: success, false: failed
Note: Due to limitations in the stick table, it only supports 16 characters in the hash value. Otherwise, we will hash it to obtain 16 bytes as index.
Supported Version
FortiADC version 5.4.x and later. In 7.2.x, function extended to HTTP_REQUEST events.
read_tbl
Saves a server assignment for a hash value in the persistence table.
|
Field |
Type |
Required |
Description |
|---|---|---|---|
|
operation |
String |
Yes |
Must be "read_tbl". |
|
hash_value |
String |
Yes |
The hash key to lookup |
Example
when PERSISTENCE {
t={};
t["operation"] = "save_tbl";
t["hash_value"] = "246810";
t["srv_name"] = "pool1-3";
ret = HTTP: persist(t);
if ret then
debug("===server add success\n");
else
debug("===server add fail\n");
end
t={}
t["operation"] = "read_tbl"
t["hash_value"] = "246810"
ret_tbl = HTTP:persist(t)
if ret_tbl then
debug("246810-server: %s\n",ret_tbl)
else
debug("246810-server read fail\n")
end
}
Output:
Return server name of the entry, or false if no entry found
Supported Version
FortiADC version 5.4.x and later. In 7.2.x, function extended to HTTP_REQUEST events.
dump_tbl
Dumps a range of entries from the persistence table.
|
Field |
Type |
Required |
Description |
|---|---|---|---|
|
operation |
String |
Yes |
Must be "dump_tbl". |
|
index |
Integer |
No |
Starting index for dump (default: 1). |
|
count |
Integer |
No |
Number of entries to return (default: all). |
Example
when PERSISTENCE {
t={};
t["operation"] = "save_tbl";
t["hash_value"] = "246810";
t["srv_name"] = "pool1-3";
ret = HTTP: persist(t);
if ret then
debug("===server add success\n");
else
debug("===server add fail\n");
end
t={}
t["operation"] = "dump_tbl"
t[“index”] = 1
t[“count”] = 15
ret_tbl = HTTP:persist(t)
if ret_tbl then
for hash, srv in pairs(ret_tbl) do
debug("tbl hash %s srv %s\n",hash,srv)
end
end
}
“index”:
“count”:
Output:
Return A table include hash and server name
Supported Version
FortiADC version 5.4.x and later. In 7.2.x, function extended to HTTP_REQUEST events.
get_valid_server
Saves a server assignment for a hash value in the persistence table.
|
Field |
Type |
Required |
Description |
|---|---|---|---|
|
operation |
String |
Yes |
Must be "get_valid_server". |
Example
when PERSISTENCE {
debug("-----get valid server-----\n")
t={}
t["operation"] = "get_valid_server"
ret = HTTP:persist(t)
if ret then
for srv,stat in pairs(ret) do
debug("server %s, status %s\n",srv,stat)
end
end
}
Output:
Return the table of usable real server and server state(enable, backup)
Supported Version
FortiADC version 5.4.x and later. In 7.2.x, function extended to HTTP_REQUEST events.
cal_server_from_hash
Saves a server assignment for a hash value in the persistence table.
|
Field |
Type |
Required |
Description |
|---|---|---|---|
|
operation |
String |
Yes |
Must be "cal_server_from_hash". |
|
hash_value |
String |
Yes |
The hash value to calculate from. |
Example
when PERSISTENCE {
debug("-----cal_server_from_hash-----\n")
t={}
t["operation"] = "cal_server_from_hash"
t["hash_value"] = "246810"
ret = HTTP:persist(t)
if ret then
debug("hash 246810, server %s\n",ret)
end
}
Output:
Return the real server name according to the hash value using our algorithm or False if failed
Supported Version
FortiADC version 5.4.x and later. In 7.2.x, function extended to HTTP_REQUEST events.
get_current_assigned_server
Saves a server assignment for a hash value in the persistence table.
|
Field |
Type |
Required |
Description |
|---|---|---|---|
|
operation |
String |
Yes |
Must be "get_current_assigned_server". |
Example
when PERSISTENCE {
cip = HTTP:client_addr()
hash_str_cip = sha512_hex(cip)
t={}
t["operation"] = "save_tbl"
t["hash_value"] = hash_str_cip
t["srv_name"] = "pool1-3"
ret = HTTP:persist(t)
if ret then
debug("hash save table success\n");
else
debug("save table failed\n");
end
t={}
t["hash_value"]=hash_str_cip
ret = HTTP:lookup_tbl(t)
if ret then
debug("hash LOOKUP success\n")
else
debug("hash lookup failed\n")
end
}
when POST_PERSIST {
debug("-----event POST_PERSIST-----\n")
debug("-----get current assigned server-----\n")
t={}
t["operation"]="get_current_assigned_server"
ret=HTTP:persist(t)
debug("current assigned server: %s\n",ret)
}
Output:
Return the real server name which is assigned to current session or False if no server is assigned right now
Supported Version
FortiADC version 5.4.x and later. In 7.2.x, function extended to HTTP_REQUEST events.