Skip to content

Commit

Permalink
Update about-connection-context.md (#1688)
Browse files Browse the repository at this point in the history
  • Loading branch information
Barenboim authored Jan 14, 2025
1 parent 1c483da commit 58ea077
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
8 changes: 7 additions & 1 deletion docs/about-connection-context.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ http协议可以说是一种完全无连接状态的协议,http会话,是通
事务型mysql,可以固定连接,这部分内容请参考mysql相关文档。
但是,如果我们实现一个redis协议的server,那我们需要知道当前连接上的状态了。

此外,我们还可以通过连接上下文件被释放的事件来感知连接被远端关闭。

# 使用连接上下文的方法

我们需要强调的是,一般情况下只有server任务需要使用连接上下文,并且只需要在process函数内部使用,这也是最安全最简单的用法。
Expand All @@ -40,12 +42,16 @@ class WFConnection : public CommConnection
public:
void *get_context() const;
void set_context(void *context, std::function<void (void *)> deleter);
void set_context(void *context);
void *test_set_context(void *test_context, void *new_context,
std::function<void (void *)> deleter);
void *test_set_context(void *test_context, void *new_context);
};
~~~
get_connection()只可在process或callback里调用,而且如果callback里调用,需要检查返回值是否为NULL。
如果成功取得WFConnection对象,就可以操作连接上下文了。连接上下文是一个void *指针,在连接被关闭时,deleter被自动调用。
如果成功取得WFConnection对象,就可以操作连接上下文了。连接上下文是一个void *指针。
设置连接上下文可以同时传入deleter函数,在连接被关闭时,deleter被自动调用。
如果调用无deleter参数的接口,可以只设置新的上下文,保持原有的deleter不变。

# 访问连接上下文的时机和并发问题

Expand Down
6 changes: 5 additions & 1 deletion docs/en/about-connection-context.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Due to this limitation, in the framework, you cannot use the SELECT command of R
Transactional MySQL tasks can use fixed connections. Please see MySQL documentations for relevant details.
However, if you implement a server based on Redis protocol, you need to know the current connection status.

By using the deleter function of connection context, users can also get notified when the connection was closed by the peer.

# How to use connection context

Note: generally, only the server tasks need to use the connection context, and the connection context is used only inside the process function, which is also the safest and simplest.
Expand All @@ -44,13 +46,15 @@ class WFConnection : public CommConnection
public:
void *get_context() const;
void set_context(void *context, std::function<void (void *)> deleter);
void set_context(void *context);
void *test_set_context(void *test_context, void *new_context,
std::function<void (void *)> deleter);
void *test_set_context(void *test_context, void *new_context);
};
~~~

**get\_connection()** can only be called in a process or a callback. If you call it in the callback, please check whether the return value is NULL.
If you get the WFConnection object successfully, you can perform operations on the connection context. A connection context is a void \* pointer. When the connection is closed, the deleter is automatically called.
If you get the WFConnection object successfully, you can perform operations on the connection context. A connection context is a void \* pointer. When the connection is closed, the deleter is automatically called. When using the setting context functions without ``deleter`` argument, the original deleter will be kept unchanged.

# Timing and concurrency for accessing connection context

Expand Down

0 comments on commit 58ea077

Please sign in to comment.