pub struct Core<'lua> { /* private fields */ }
Expand description
The “Core” class contains all the HAProxy core functions.
Implementations§
Source§impl<'lua> Core<'lua>
impl<'lua> Core<'lua>
Sourcepub fn proxies(&self) -> Result<HashMap<String, Proxy<'lua>>>
pub fn proxies(&self) -> Result<HashMap<String, Proxy<'lua>>>
Returns a map of declared proxies (frontends and backends), indexed by proxy name.
Sourcepub fn backends(&self) -> Result<HashMap<String, Proxy<'lua>>>
pub fn backends(&self) -> Result<HashMap<String, Proxy<'lua>>>
Returns a map of declared proxies with backend capability, indexed by the backend name.
Sourcepub fn frontends(&self) -> Result<HashMap<String, Proxy<'lua>>>
pub fn frontends(&self) -> Result<HashMap<String, Proxy<'lua>>>
Returns a map of declared proxies with frontend capability, indexed by the frontend name.
Sourcepub fn thread(&self) -> Result<u16>
pub fn thread(&self) -> Result<u16>
Returns the executing thread number starting at 0. If thread is 0, Lua scope is shared by all threads, otherwise the scope is dedicated to a single thread. This is HAProxy >=2.4 feature.
Sourcepub fn log(&self, level: LogLevel, msg: impl AsRef<str>) -> Result<()>
pub fn log(&self, level: LogLevel, msg: impl AsRef<str>) -> Result<()>
Sends a log on the default syslog server if it is configured and on the stderr if it is allowed.
Sourcepub fn add_acl(&self, filename: &str, key: &str) -> Result<()>
pub fn add_acl(&self, filename: &str, key: &str) -> Result<()>
Adds the ACL key
in the ACLs list referenced by filename
.
Sourcepub fn del_acl(&self, filename: &str, key: &str) -> Result<()>
pub fn del_acl(&self, filename: &str, key: &str) -> Result<()>
Deletes the ACL entry by key
in the ACLs list referenced by filename
.
Sourcepub fn del_map(&self, filename: &str, key: &str) -> Result<()>
pub fn del_map(&self, filename: &str, key: &str) -> Result<()>
Deletes the map entry indexed with the specified key
in the list of maps
referenced by his filename
.
Sourcepub fn set_map(&self, filename: &str, key: &str, value: &str) -> Result<()>
pub fn set_map(&self, filename: &str, key: &str, value: &str) -> Result<()>
Sets the value
associated to the key
in the map referenced by filename
.
Sourcepub fn get_info(&self) -> Result<Vec<String>>
pub fn get_info(&self) -> Result<Vec<String>>
Returns HAProxy core information (uptime, pid, memory pool usage, tasks number, …).
Sourcepub fn now(&self) -> Result<Time>
pub fn now(&self) -> Result<Time>
Returns the current time.
The time returned is fixed by the HAProxy core and assures than the hour will be monotonic
and that the system call gettimeofday
will not be called too.
Sourcepub fn register_action<A, F>(
&self,
name: &str,
actions: &[Action],
nb_args: usize,
func: F,
) -> Result<()>
pub fn register_action<A, F>( &self, name: &str, actions: &[Action], nb_args: usize, func: F, ) -> Result<()>
Registers a function executed as an action.
The expected actions are tcp-req
, tcp-res
, http-req
, http-res
or http-after-res
.
All the registered actions can be used in HAProxy with the prefix lua.
.
Sourcepub fn register_async_action<F, A, FR>(
&self,
name: &str,
actions: &[Action],
nb_args: usize,
func: F,
) -> Result<()>
pub fn register_async_action<F, A, FR>( &self, name: &str, actions: &[Action], nb_args: usize, func: F, ) -> Result<()>
Registers an asynchronous function executed as an action.
See Core::register_action
for more details.
Sourcepub fn register_lua_action<'a, S>(
&self,
name: &str,
actions: &[&str],
nb_args: usize,
code: S,
) -> Result<()>where
S: AsChunk<'lua, 'a>,
pub fn register_lua_action<'a, S>(
&self,
name: &str,
actions: &[&str],
nb_args: usize,
code: S,
) -> Result<()>where
S: AsChunk<'lua, 'a>,
Same as register_action
but using Lua function.
Sourcepub fn register_converters<A, R, F>(&self, name: &str, func: F) -> Result<()>
pub fn register_converters<A, R, F>(&self, name: &str, func: F) -> Result<()>
Registers a function executed as a converter.
All the registered converters can be used in HAProxy with the prefix lua.
.
Sourcepub fn register_lua_converters<'a, S>(&self, name: &str, code: S) -> Result<()>where
S: AsChunk<'lua, 'a>,
pub fn register_lua_converters<'a, S>(&self, name: &str, code: S) -> Result<()>where
S: AsChunk<'lua, 'a>,
Same as register_converters
but using Lua function.
Sourcepub fn register_fetches<A, R, F>(&self, name: &str, func: F) -> Result<()>
pub fn register_fetches<A, R, F>(&self, name: &str, func: F) -> Result<()>
Registers a function executed as sample fetch.
All the registered sample fetch can be used in HAProxy with the prefix lua.
.
Sourcepub fn register_lua_fetches<'a, S>(&self, name: &str, code: S) -> Result<()>where
S: AsChunk<'lua, 'a>,
pub fn register_lua_fetches<'a, S>(&self, name: &str, code: S) -> Result<()>where
S: AsChunk<'lua, 'a>,
Same as register_fetches
but using Lua function.
Sourcepub fn register_filter<T: UserFilter + 'static>(&self, name: &str) -> Result<()>
pub fn register_filter<T: UserFilter + 'static>(&self, name: &str) -> Result<()>
Registers a custom filter that implements UserFilter
trait.
Sourcepub fn register_lua_service<'a, S>(
&self,
name: &str,
mode: ServiceMode,
code: S,
) -> Result<()>where
S: AsChunk<'lua, 'a>,
pub fn register_lua_service<'a, S>(
&self,
name: &str,
mode: ServiceMode,
code: S,
) -> Result<()>where
S: AsChunk<'lua, 'a>,
Registers a Lua function executed as a service.
All the registered service can be used in HAProxy with the prefix lua.
.
Sourcepub fn register_init<F>(&self, func: F) -> Result<()>
pub fn register_init<F>(&self, func: F) -> Result<()>
Registers a function executed after the configuration parsing. This is useful to check any parameters.
Sourcepub fn register_task<F>(&self, func: F) -> Result<()>
pub fn register_task<F>(&self, func: F) -> Result<()>
Registers and start an independent task. The task is started when the HAProxy main scheduler starts.
Sourcepub fn register_async_task<F, FR>(&self, func: F) -> Result<()>
pub fn register_async_task<F, FR>(&self, func: F) -> Result<()>
Registers and start an independent asynchronous task.
Sourcepub fn register_lua_task<'a, S>(&self, code: S) -> Result<()>where
S: AsChunk<'lua, 'a>,
pub fn register_lua_task<'a, S>(&self, code: S) -> Result<()>where
S: AsChunk<'lua, 'a>,
Same as register_task
but using Lua function.
Sourcepub fn register_lua_cli<'a, S>(
&self,
path: &[&str],
usage: &str,
code: S,
) -> Result<()>where
S: AsChunk<'lua, 'a>,
pub fn register_lua_cli<'a, S>(
&self,
path: &[&str],
usage: &str,
code: S,
) -> Result<()>where
S: AsChunk<'lua, 'a>,
Registers a Lua function executed as a cli command.
Sourcepub fn set_nice(&self, nice: i32) -> Result<()>
pub fn set_nice(&self, nice: i32) -> Result<()>
Changes the nice of the current task or current session.
Sourcepub fn parse_addr(&self, addr: &str) -> Result<AnyUserData<'lua>>
pub fn parse_addr(&self, addr: &str) -> Result<AnyUserData<'lua>>
Parses ipv4 or ipv6 addresses and its facultative associated network.
Sourcepub fn match_addr(
&self,
addr1: AnyUserData<'_>,
addr2: AnyUserData<'_>,
) -> Result<bool>
pub fn match_addr( &self, addr1: AnyUserData<'_>, addr2: AnyUserData<'_>, ) -> Result<bool>
Matches two networks. For example “127.0.0.1/32” matches “127.0.0.0/8”. The order of network is not important.
pub fn event_sub<'a, S>(&self, event_types: &[&str], code: S) -> Result<()>where
S: AsChunk<'lua, 'a>,
Methods from Deref<Target = Table<'lua>>§
Sourcepub fn set<K, V>(&self, key: K, value: V) -> Result<(), Error>
pub fn set<K, V>(&self, key: K, value: V) -> Result<(), Error>
Sets a key-value pair in the table.
If the value is nil
, this will effectively remove the pair.
This might invoke the __newindex
metamethod. Use the raw_set
method if that is not
desired.
§Examples
Export a value as a global to make it usable from Lua:
let globals = lua.globals();
globals.set("assertions", cfg!(debug_assertions))?;
lua.load(r#"
if assertions == true then
-- ...
elseif assertions == false then
-- ...
else
error("assertions neither on nor off?")
end
"#).exec()?;
Sourcepub fn get<K, V>(&self, key: K) -> Result<V, Error>
pub fn get<K, V>(&self, key: K) -> Result<V, Error>
Gets the value associated to key
from the table.
If no value is associated to key
, returns the nil
value.
This might invoke the __index
metamethod. Use the raw_get
method if that is not
desired.
§Examples
Query the version of the Lua interpreter:
let globals = lua.globals();
let version: String = globals.get("_VERSION")?;
println!("Lua version: {}", version);
Sourcepub fn contains_key<K>(&self, key: K) -> Result<bool, Error>where
K: IntoLua<'lua>,
pub fn contains_key<K>(&self, key: K) -> Result<bool, Error>where
K: IntoLua<'lua>,
Checks whether the table contains a non-nil value for key
.
This might invoke the __index
metamethod.
Sourcepub fn push<V>(&self, value: V) -> Result<(), Error>where
V: IntoLua<'lua>,
pub fn push<V>(&self, value: V) -> Result<(), Error>where
V: IntoLua<'lua>,
Appends a value to the back of the table.
This might invoke the __len
and __newindex
metamethods.
Sourcepub fn pop<V>(&self) -> Result<V, Error>where
V: FromLua<'lua>,
pub fn pop<V>(&self) -> Result<V, Error>where
V: FromLua<'lua>,
Removes the last element from the table and returns it.
This might invoke the __len
and __newindex
metamethods.
Sourcepub fn equals<T>(&self, other: T) -> Result<bool, Error>
pub fn equals<T>(&self, other: T) -> Result<bool, Error>
Compares two tables for equality.
Tables are compared by reference first.
If they are not primitively equals, then mlua will try to invoke the __eq
metamethod.
mlua will check self
first for the metamethod, then other
if not found.
§Examples
Compare two tables using __eq
metamethod:
let table1 = lua.create_table()?;
table1.set(1, "value")?;
let table2 = lua.create_table()?;
table2.set(2, "value")?;
let always_equals_mt = lua.create_table()?;
always_equals_mt.set("__eq", lua.create_function(|_, (_t1, _t2): (Table, Table)| Ok(true))?)?;
table2.set_metatable(Some(always_equals_mt));
assert!(table1.equals(&table1.clone())?);
assert!(table1.equals(&table2)?);
Sourcepub fn raw_set<K, V>(&self, key: K, value: V) -> Result<(), Error>
pub fn raw_set<K, V>(&self, key: K, value: V) -> Result<(), Error>
Sets a key-value pair without invoking metamethods.
Sourcepub fn raw_get<K, V>(&self, key: K) -> Result<V, Error>
pub fn raw_get<K, V>(&self, key: K) -> Result<V, Error>
Gets the value associated to key
without invoking metamethods.
Sourcepub fn raw_insert<V>(&self, idx: i64, value: V) -> Result<(), Error>where
V: IntoLua<'lua>,
pub fn raw_insert<V>(&self, idx: i64, value: V) -> Result<(), Error>where
V: IntoLua<'lua>,
Inserts element value at position idx
to the table, shifting up the elements from table[idx]
.
The worst case complexity is O(n), where n is the table length.
Sourcepub fn raw_push<V>(&self, value: V) -> Result<(), Error>where
V: IntoLua<'lua>,
pub fn raw_push<V>(&self, value: V) -> Result<(), Error>where
V: IntoLua<'lua>,
Appends a value to the back of the table without invoking metamethods.
Sourcepub fn raw_pop<V>(&self) -> Result<V, Error>where
V: FromLua<'lua>,
pub fn raw_pop<V>(&self) -> Result<V, Error>where
V: FromLua<'lua>,
Removes the last element from the table and returns it, without invoking metamethods.
Sourcepub fn raw_remove<K>(&self, key: K) -> Result<(), Error>where
K: IntoLua<'lua>,
pub fn raw_remove<K>(&self, key: K) -> Result<(), Error>where
K: IntoLua<'lua>,
Removes a key from the table.
If key
is an integer, mlua shifts down the elements from table[key+1]
,
and erases element table[key]
. The complexity is O(n) in the worst case,
where n is the table length.
For other key types this is equivalent to setting table[key] = nil
.
Sourcepub fn clear(&self) -> Result<(), Error>
pub fn clear(&self) -> Result<(), Error>
Clears the table, removing all keys and values from array and hash parts, without invoking metamethods.
This method is useful to clear the table while keeping its capacity.
Sourcepub fn len(&self) -> Result<i64, Error>
pub fn len(&self) -> Result<i64, Error>
Returns the result of the Lua #
operator.
This might invoke the __len
metamethod. Use the raw_len
method if that is not desired.
Sourcepub fn raw_len(&self) -> usize
pub fn raw_len(&self) -> usize
Returns the result of the Lua #
operator, without invoking the __len
metamethod.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true
if the table is empty, without invoking metamethods.
It checks both the array part and the hash part.
Sourcepub fn get_metatable(&self) -> Option<Table<'lua>>
pub fn get_metatable(&self) -> Option<Table<'lua>>
Returns a reference to the metatable of this table, or None
if no metatable is set.
Unlike the getmetatable
Lua function, this method ignores the __metatable
field.
Sourcepub fn set_metatable(&self, metatable: Option<Table<'lua>>)
pub fn set_metatable(&self, metatable: Option<Table<'lua>>)
Sets or removes the metatable of this table.
If metatable
is None
, the metatable is removed (if no metatable is set, this does
nothing).
Sourcepub fn to_pointer(&self) -> *const c_void
pub fn to_pointer(&self) -> *const c_void
Converts this table to a generic C pointer.
Different tables will give different pointers. There is no way to convert the pointer back to its original value.
Typically this function is used only for hashing and debug information.