-
Notifications
You must be signed in to change notification settings - Fork 191
Fix the links to the examples project in the Chinese version of the README; add a close callback mechanism for sessions. #91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
9b65fd2
修复中文版readme的examples工程跳转链接
6bbc2b5
为session增加closecallback机制用于断线时做一些回收操作
af7c6e6
整合test文件
f7fe83d
change desc to english
558c626
翻译注释,增加许可证
0d94634
Merge branch 'master' into mazheng
a51bf83
调整格式
ecef7cc
test file add License Header
a1fa804
invokeCloseCallbacks panic add stack info
c27bc00
采纳ai comment
f585a70
remove ai commets recover design
6391338
格式整理
5c5b156
funcname count change to len,change recover logic
63ab8bf
update readme
d1359ce
update readme
8a74d19
文档里说明handler key的参数类型要求
27e1af0
ai comment内容修改
7fb5dbb
unlock在defer中执行,增加cbnum来记录链表元素数量
aedd29e
增加iscompare方法对不可比较类型进行判断
8aeb952
修改文档描述
7585325
修改代码格式
a951232
gofmt
979da7b
调整文档
59ecd59
isclose放在unlock后
8200644
修复callback.go格式
c0ee5b7
doc描述修改
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| package getty | ||
AlexStocks marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // callbackCommon 表示回调链表中的一个节点 | ||
AlexStocks marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // 每个节点包含处理器标识、键值、回调函数和指向下一个节点的指针 | ||
| type callbackCommon struct { | ||
AlexStocks marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| handler interface{} // 处理器标识,用于标识回调的来源或类型 | ||
| key interface{} // 回调的唯一标识键,与 handler 组合使用 | ||
| call func() // 实际要执行的回调函数 | ||
| next *callbackCommon // 指向下一个节点的指针,形成链表结构 | ||
| } | ||
|
|
||
| // callbacks 是一个单向链表结构,用于管理多个回调函数 | ||
| // 支持动态添加、移除和执行回调 | ||
| type callbacks struct { | ||
| first *callbackCommon // 指向链表第一个节点的指针 | ||
| last *callbackCommon // 指向链表最后一个节点的指针,用于快速添加新节点 | ||
| } | ||
|
|
||
| // Add 向回调链表中添加一个新的回调函数 | ||
| // 参数说明: | ||
| // - handler: 处理器标识,可以是任意类型 | ||
| // - key: 回调的唯一标识键,与 handler 组合使用 | ||
| // - callback: 要执行的回调函数,如果为 nil 则忽略 | ||
| func (t *callbacks) Add(handler, key interface{}, callback func()) { | ||
| // 防止添加空回调函数 | ||
| if callback == nil { | ||
| return | ||
| } | ||
|
|
||
| // 创建新的回调节点 | ||
| newItem := &callbackCommon{handler, key, callback, nil} | ||
|
|
||
| if t.first == nil { | ||
| // 如果链表为空,新节点成为第一个节点 | ||
| t.first = newItem | ||
| } else { | ||
| // 否则将新节点添加到链表末尾 | ||
| t.last.next = newItem | ||
| } | ||
| // 更新最后一个节点的指针 | ||
| t.last = newItem | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Remove 从回调链表中移除指定的回调函数 | ||
| // 参数说明: | ||
| // - handler: 要移除的回调的处理器标识 | ||
| // - key: 要移除的回调的唯一标识键 | ||
| // 注意: 如果找不到匹配的回调,此方法不会产生任何效果 | ||
| func (t *callbacks) Remove(handler, key interface{}) { | ||
| var prev *callbackCommon | ||
|
|
||
| // 遍历链表查找要移除的节点 | ||
| for callback := t.first; callback != nil; prev, callback = callback, callback.next { | ||
ayamzh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // 找到匹配的节点 | ||
| if callback.handler == handler && callback.key == key { | ||
| if t.first == callback { | ||
| // 如果是第一个节点,更新 first 指针 | ||
| t.first = callback.next | ||
| } else if prev != nil { | ||
| // 如果是中间节点,更新前一个节点的 next 指针 | ||
| prev.next = callback.next | ||
| } | ||
|
|
||
| if t.last == callback { | ||
| // 如果是最后一个节点,更新 last 指针 | ||
| t.last = prev | ||
| } | ||
|
|
||
| // 找到并移除后立即返回 | ||
| return | ||
| } | ||
| } | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Invoke 执行链表中所有注册的回调函数 | ||
| // 按照添加的顺序依次执行每个回调 | ||
| // 注意: 如果某个回调函数为 nil,会被跳过 | ||
| func (t *callbacks) Invoke() { | ||
| // 从头节点开始遍历整个链表 | ||
| for callback := t.first; callback != nil; callback = callback.next { | ||
| // 确保回调函数不为 nil 再执行 | ||
| if callback.call != nil { | ||
| callback.call() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Count 返回链表中回调函数的数量 | ||
| // 返回值: 当前注册的回调函数总数 | ||
| func (t *callbacks) Count() int { | ||
AlexStocks marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| var count int | ||
|
|
||
| // 遍历链表计数 | ||
| for callback := t.first; callback != nil; callback = callback.next { | ||
| count++ | ||
| } | ||
|
|
||
| return count | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| package getty | ||
|
|
||
| import ( | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestCallback(t *testing.T) { | ||
| // Test empty list | ||
| cb := &callbacks{} | ||
| if cb.Count() != 0 { | ||
| t.Errorf("Expected count for empty list is 0, but got %d", cb.Count()) | ||
| } | ||
|
|
||
| // Test adding callback functions | ||
| var count, expected, remove, totalCount int | ||
| totalCount = 10 | ||
| remove = 5 | ||
|
|
||
| // Add multiple callback functions | ||
| for i := 1; i < totalCount; i++ { | ||
| expected = expected + i | ||
| func(ii int) { | ||
| cb.Add(ii, ii, func() { count = count + ii }) | ||
| }(i) | ||
| } | ||
|
|
||
| // Verify count after adding | ||
| expectedCallbacks := totalCount - 1 | ||
| if cb.Count() != expectedCallbacks { | ||
| t.Errorf("Expected callback count is %d, but got %d", expectedCallbacks, cb.Count()) | ||
| } | ||
|
|
||
| // Test adding nil callback | ||
| cb.Add(remove, remove, nil) | ||
| if cb.Count() != expectedCallbacks { | ||
| t.Errorf("Expected count after adding nil callback is %d, but got %d", expectedCallbacks, cb.Count()) | ||
| } | ||
|
|
||
| // Remove specified callback | ||
| cb.Remove(remove, remove) | ||
|
|
||
| // Try to remove non-existent callback | ||
| cb.Remove(remove+1, remove+2) | ||
|
|
||
| // Execute all callbacks | ||
| cb.Invoke() | ||
|
|
||
| // Verify execution result | ||
| expectedCount := expected - remove | ||
| if count != expectedCount { | ||
| t.Errorf("Expected execution result is %d, but got %d", expectedCount, count) | ||
| } | ||
|
|
||
| // Test string type handler and key | ||
| cb2 := &callbacks{} | ||
|
|
||
| // Add callbacks | ||
| cb2.Add("handler1", "key1", func() {}) | ||
| cb2.Add("handler2", "key2", func() {}) | ||
| cb2.Add("handler3", "key3", func() {}) | ||
|
|
||
| if cb2.Count() != 3 { | ||
| t.Errorf("Expected callback count is 3, but got %d", cb2.Count()) | ||
| } | ||
|
|
||
| // Remove middle callback | ||
| cb2.Remove("handler2", "key2") | ||
| if cb2.Count() != 2 { | ||
| t.Errorf("Expected count after removing middle callback is 2, but got %d", cb2.Count()) | ||
| } | ||
|
|
||
| // Remove first callback | ||
| cb2.Remove("handler1", "key1") | ||
| if cb2.Count() != 1 { | ||
| t.Errorf("Expected count after removing first callback is 1, but got %d", cb2.Count()) | ||
| } | ||
|
|
||
| // Remove last callback | ||
| cb2.Remove("handler3", "key3") | ||
| if cb2.Count() != 0 { | ||
| t.Errorf("Expected count after removing last callback is 0, but got %d", cb2.Count()) | ||
| } | ||
|
|
||
| // Test removing non-existent callback | ||
| cb2.Add("handler1", "key1", func() {}) | ||
| cb2.Remove("handler2", "key2") // Try to remove non-existent callback | ||
|
|
||
| // Should still have 1 callback | ||
| if cb2.Count() != 1 { | ||
| t.Errorf("Expected callback count is 1, but got %d", cb2.Count()) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package getty | ||
|
|
||
| // AddCloseCallback adds a close callback function to the Session | ||
| // | ||
| // Parameters: | ||
| // - handler: handler identifier, used to identify the source or type of the callback | ||
| // - key: unique identifier key for the callback, used in combination with handler | ||
| // - f: callback function to be executed when the session is closed | ||
| // | ||
| // Notes: | ||
| // - If the session is already closed, this addition will be ignored | ||
| // - The combination of handler and key must be unique, otherwise it will override previous callbacks | ||
| // - Callback functions will be executed in the order they were added when the session closes | ||
| func (s *session) AddCloseCallback(handler, key any, f CallBackFunc) { | ||
| if s.IsClosed() { | ||
| return | ||
| } | ||
| s.closeCallbackMutex.Lock() | ||
| s.closeCallback.Add(handler, key, f) | ||
| s.closeCallbackMutex.Unlock() | ||
| } | ||
ayamzh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // RemoveCloseCallback removes the specified Session close callback function | ||
| // | ||
| // Parameters: | ||
| // - handler: handler identifier of the callback to be removed | ||
| // - key: unique identifier key of the callback to be removed | ||
| // | ||
| // Return value: none | ||
| // | ||
| // Notes: | ||
| // - If the session is already closed, this removal operation will be ignored | ||
| // - If no matching callback is found, this operation will have no effect | ||
| // - The removal operation is thread-safe | ||
| func (s *session) RemoveCloseCallback(handler, key any) { | ||
| if s.IsClosed() { | ||
| return | ||
| } | ||
| s.closeCallbackMutex.Lock() | ||
| s.closeCallback.Remove(handler, key) | ||
| s.closeCallbackMutex.Unlock() | ||
ayamzh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
ayamzh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // invokeCloseCallbacks executes all registered close callback functions | ||
| // | ||
| // Function description: | ||
| // - Executes all registered close callbacks in the order they were added | ||
| // - Uses read lock to protect the callback list, ensuring concurrency safety | ||
| // - This method is typically called automatically when the session closes | ||
| // | ||
| // Notes: | ||
| // - This is an internal method, not recommended for external direct calls | ||
| // - If panic occurs during callback execution, it will be caught and logged | ||
| // - Callback functions should avoid long blocking operations, async processing is recommended for time-consuming tasks | ||
| func (s *session) invokeCloseCallbacks() { | ||
| s.closeCallbackMutex.RLock() | ||
| s.closeCallback.Invoke() | ||
| s.closeCallbackMutex.RUnlock() | ||
ayamzh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // CallBackFunc defines the callback function type when Session closes | ||
| // | ||
| // Usage notes: | ||
| // - Callback function accepts no parameters | ||
| // - Callback function returns no values | ||
| // - Callback function should handle resource cleanup, state updates, etc. | ||
| // - It's recommended to avoid accessing closed session state in callback functions | ||
| type CallBackFunc func() | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.