Endpoint
EndpointContext
EndpointContext(
caller: int,
connection_id: int,
address: str,
group: str,
user_data: dict[str, typing.Any],
timestamp: float,
request: Request,
systems: SystemCaller,
client_limits: list[list[int]] = <factory>,
server_limits: list[list[int]] = <factory>,
max_row_sub: int = 0,
max_index_sub: int = 0,
) -> NoneSource: hetu/endpoint/context.py:18
Endpoint调用时的上下文,由engine创建并作为 ctx 参数传入Endpoint函数;
SystemContext
继承自此类。包含调用方身份、当前连接的用户数据,
以及消息发送和订阅数量限制等。
Attributes
caller(int) — 调用方的user id;未登录为0;执行过elevate()后为传入的user_id。connection_id(int) — 调用方的connection id。address(str) — 调用方的IP地址。group(str) — 所属组名,目前只用于判断是否admin。user_data(dict[str, Any]) — 当前连接的用户数据,可自由设置,在所有System间共享。timestamp(float) — 调用时间戳。request(Request) — framework原始请求对象,unsafe,普通业务代码请避免直接使用。systems(SystemCaller) — 全局System管理器,unsafe,可通过ctx.systems.call(...)调用其他System。client_limits(list[list[int]]) — 客户端消息发送限制(次数)。server_limits(list[list[int]]) — 服务端消息发送限制(次数)。max_row_sub(int) — 行订阅数量限制。max_index_sub(int) — 索引订阅数量限制。
Methods
configure
configure(client_limits, server_limits, max_row_sub, max_index_sub)Source: hetu/endpoint/context.py:68
配置当前连接的限流与订阅配额。
此方法通常在连接建立后调用,用于把 websocket/app 配置中的连接级限制
写入 Context,供后续消息收发和订阅逻辑直接读取。
Parameters
client_limits(Any) — 客户端向服务端发送消息的频率限制。每项格式为[最大消息数, 统计时间(秒)];空列表表示不限制。 一般对应配置项CLIENT_SEND_LIMITS。server_limits(Any) — 服务端向客户端发送消息的频率限制。每项格式同上; 一般对应配置项SERVER_SEND_LIMITS。max_row_sub(Any) — 当前连接允许的最大行订阅数量。一般对应配置项MAX_ROW_SUBSCRIPTION。max_index_sub(Any) — 当前连接允许的最大索引订阅数量。一般对应配置项MAX_INDEX_SUBSCRIPTION。
Notes
本方法只负责保存传入值,不做校验、排序或拷贝。
client_limits 和 server_limits 应按统计时间从小到大排列,
因为后续限流检查会使用最后一项的时间窗口作为计数重置基准。
rls_check
rls_check(
component: type[BaseComponent],
row: numpy.record | numpy.ndarray | numpy.rec.recarray | dict,
) -> boolSource: hetu/endpoint/context.py:102
检查当前用户对某个component的权限
ResponseToClient
ResponseToClient(message: list | dict)Source: hetu/endpoint/response.py:13
Bases: EndpointResponse
回报message给客户端,注意必须是json可以序列化的数据
Attributes
message(Any) — No description.
elevate
elevate(ctx: hetu.endpoint.context.Context, user_id: int, kick_logged_in=True)Source: hetu/endpoint/connection.py:90
提升到User权限。如果该连接已提权,或user_id已在其他连接登录,返回False。 如果成功,则ctx.caller会被设置为user_id,同时事务结束,之后将无法调用ctx[Components]。
kick_logged_in: 如果user_id已在其他连接登录,则标记该连接断开并返回True,该连接将在客户端调用任意Endpoint时被关闭。
Notes
本方法是一个独立的事务,如果你在System中(另一个事务中)调用此方法,父事务回退时不会回退此方法的结果。