Вложенный модуль box.session¶
The box.session
submodule allows querying the session state, writing to a
session-specific temporary Lua table, or setting up triggers which will fire
when a session starts or ends. A session is an object associated with each
client connection.
-
box.session.
id
()¶ Return: the unique identifier (ID) for the current session. The result can be 0 meaning there is no session. Rtype: number
-
box.session.
exists
(id)¶ Return: 1 if the session exists, 0 if the session does not exist. Rtype: number
-
box.session.
peer
(id)¶ This function works only if there is a peer, that is, if a connection has been made to a separate Tarantool instance.
Return: The host address and port of the session peer, for example «127.0.0.1:55457». If the session exists but there is no connection to a separate instance, the return is null. The command is executed on the server instance, so the «local name» is the server instance’s host and port, and the «peer name» is the client’s host and port. Rtype: string Possible errors: „session.peer(): session does not exist“
-
box.session.
sync
()¶ Return: the value of the sync
integer constant used in the binary protocol.Rtype: number
-
box.session.
user
()¶ Return: the name of the current user Rtype: string
-
box.session.
su
(user-name[, function-to-execute])¶ Change Tarantool’s current user – this is analogous to the Unix command
su
.Or, if function-to-execute is specified, change Tarantool’s current user temporarily while executing the function – this is analogous to the Unix command
sudo
.Параметры: - user-name (string) – name of a target user
- function-to-execute – name of a function, or definition of a function.
Additional parameters may be passed to
box.session.su
, they will be interpreted as parameters of function-to-execute.
Example
tarantool> function f(a) return box.session.user() .. a end --- ... tarantool> box.session.su('guest', f, '-xxx') --- - guest-xxx ... tarantool> box.session.su('guest',function(...) return ... end,1,2) --- - 1 - 2 ...
-
box.session.
storage
¶ A Lua table that can hold arbitrary unordered session-specific names and values, which will last until the session ends. For example, this table could be useful to store current tasks when working with a Tarantool queue manager.
Example
tarantool> box.session.peer(box.session.id()) --- - 127.0.0.1:45129 ... tarantool> box.session.storage.random_memorandum = "Don't forget the eggs" --- ... tarantool> box.session.storage.radius_of_mars = 3396 --- ... tarantool> m = '' --- ... tarantool> for k, v in pairs(box.session.storage) do > m = m .. k .. '='.. v .. ' ' > end --- ... tarantool> m --- - 'radius_of_mars=3396 random_memorandum=Don''t forget the eggs. ' ...
-
box.session.
on_connect
(trigger-function[, old-trigger-function])¶ Define a trigger for execution when a new session is created due to an event such as console.connect. The trigger function will be the first thing executed after a new session is created. If the trigger execution fails and raises an error, the error is sent to the client and the connection is closed.
Параметры: - trigger-function (function) – function which will become the trigger function
- old-trigger-function (function) – existing trigger function which will be replaced by trigger-function
Return: nil or function pointer
If the parameters are (nil, old-trigger-function), then the old trigger is deleted.
Details about trigger characteristics are in the triggers section.
Example
tarantool> function f () > x = x + 1 > end tarantool> box.session.on_connect(f)
Предупреждение
If a trigger always results in an error, it may become impossible to connect to a server to reset it.
-
box.session.
on_disconnect
(trigger-function[, old-trigger-function])¶ Define a trigger for execution after a client has disconnected. If the trigger function causes an error, the error is logged but otherwise is ignored. The trigger is invoked while the session associated with the client still exists and can access session properties, such as box.session.id.
Параметры: - trigger-function (function) – function which will become the trigger function
- old-trigger-function (function) – existing trigger function which will be replaced by trigger-function
Return: nil or function pointer
If the parameters are (nil, old-trigger-function), then the old trigger is deleted.
Details about trigger characteristics are in the triggers section.
Example #1
tarantool> function f () > x = x + 1 > end tarantool> box.session.on_disconnect(f)
Example #2
After the following series of requests, a Tarantool instance will write a message using the log module whenever any user connects or disconnects.
function log_connect () local log = require('log') local m = 'Connection. user=' .. box.session.user() .. ' id=' .. box.session.id() log.info(m) end function log_disconnect () local log = require('log') local m = 'Disconnection. user=' .. box.session.user() .. ' id=' .. box.session.id() log.info(m) end box.session.on_connect(log_connect) box.session.on_disconnect(log_disconnect)
Here is what might appear in the log file in a typical installation:
2014-12-15 13:21:34.444 [11360] main/103/iproto I> Connection. user=guest id=3 2014-12-15 13:22:19.289 [11360] main/103/iproto I> Disconnection. user=guest id=3
-
box.session.
on_auth
(trigger-function[, old-trigger-function])¶ Define a trigger for execution during authentication.
The
on_auth
trigger function is invoked in these circumstances:- The console.connect function includes an authentication check
for all users except „guest“.
For this case, the
on_auth
trigger function is invoked after theon_connect
trigger function, if and only if the connection has succeeded so far. - The binary protocol has a separate authentication packet. For this case, connection and authentication are considered to be separate steps.
Unlike other trigger types,
on_auth
trigger functions are invoked before the event. Therefore a trigger function likefunction auth_function () v = box.session.user(); end
will setv
to «guest», the user name before the authentication is done. To get the user name after the authentication is done, use the special syntax:function auth_function (user_name) v = user_name; end
If the trigger fails by raising an error, the error is sent to the client and the connection is closed.
Параметры: - trigger-function (function) – function which will become the trigger function
- old-trigger-function (function) – existing trigger function which will be replaced by trigger-function
Return: nil or function pointer
If the parameters are (nil, old-trigger-function), then the old trigger is deleted.
Details about trigger characteristics are in the triggers section.
Example
tarantool> function f () > x = x + 1 > end tarantool> box.session.on_auth(f)
- The console.connect function includes an authentication check
for all users except „guest“.
For this case, the