feat(os/gcfg): Add file watcher with custom callback support#4446
feat(os/gcfg): Add file watcher with custom callback support#4446hailaz merged 38 commits intogogf:masterfrom
Conversation
- 新增 WatcherAdapter 接口,支持添加和移除监听函数- 在 AdapterContent 和 AdapterFile 中实现监听器机制 - 添加 notifyWatchers 方法,用于通知所有监听器配置变更 - 实现文件变化监听,自动触发监听器回调 - 增加单元测试,验证监听器的添加、移除和通知功能 - 支持内容变更时触发监听器回调
|
@LanceAdd 看实现的功能,只能知道文件变化了,但不知道文件是由哪个事件引起的变化,是不是应该将变化的具体情况也透传一下? |
另外可能要考虑一下是否预置一个ctx context.Context参数 |
赞同,是否也应该增加一个file参数? |
我在内部魔改的版本是带个ctx参数,把这些信息都放到ctx里面,需要什么就去ctx里拿,不同实现的Adapter结构体参数可能不一样。 |
There was a problem hiding this comment.
Pull Request Overview
This PR adds file watcher functionality with custom callback support to the gcfg package. It implements the WatcherAdapter interface for both AdapterFile and AdapterContent types, allowing users to register callbacks that are triggered when configuration changes occur.
Key changes:
- Adds
WatcherAdapterinterface withAddWatcherandRemoveWatchermethods - Implements watcher functionality in both
AdapterFileandAdapterContentwith callback notification - Adds comprehensive test coverage for watcher functionality including file changes, content updates, and watcher removal
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 10 comments.
Show a summary per file
| File | Description |
|---|---|
| os/gcfg/gcfg_adaper.go | Defines the new WatcherAdapter interface |
| os/gcfg/gcfg_adapter_file.go | Implements watcher functionality for file-based configurations |
| os/gcfg/gcfg_adapter_content.go | Implements watcher functionality for content-based configurations |
| os/gcfg/gcfg_adapter_file_content.go | Adds watcher notifications to content manipulation methods |
| os/gcfg/gcfg_z_unit_watcher_test.go | Comprehensive test suite for watcher functionality |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
- 修改 WatcherAdapter 接口,使 AddWatcher 方法接受 context.Context 参数 - 新增 GetWatcherNames 方法用于获取所有监听器名称 - 为 AdapterContent 和 AdapterFile 实现新的上下文支持方法 - 创建 AdapterContentCtx 和 AdapterFileCtx 结构体及其实现- 添加配置操作相关的上下文键值常量定义文件 - 更新 notifyWatchers 方法以传递上下文信息 - 扩展单元测试以验证新添加的上下文功能 - 将 defaultFileNameOrPath 类型从字符串改为 *gtype.String 以提高并发安全性- 在文件系统事件监听中增加对更多事件类型的支持并传递相应上下文- 优化代码结构和变量命名以增强可读性和一致性
|
@wln32 cc |
fix(contrib/config/nacos): 在 Nacos 客户端中新增监听器管理机制
fix(os/gcfg): 修复上下文获取方法,增加类型安全检查
fix(contrib/config/nacos): - 修复nacos中ctx的错误key,补充缺少的注释
- 添加对 gcfg.Adapter 接口的编译时检查 - 添加对 gcfg.WatcherAdapter 接口的编译时检查- 确保 Client 结构体实现必要的配置管理接口
There was a problem hiding this comment.
Pull Request Overview
Copilot reviewed 20 out of 20 changed files in this pull request and generated 8 comments.
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
为
gcfg添加配置文件变更自定义回调,实现了WatcherAdapter接口,以下是AdapterFile的用法test.yaml
使用
g和默认配置文件注意:由于
gf的AdapterFile使用的监听到文件变化删除缓存下一次重新初始化的懒加载方案,所有除了默认加载的config.xxx文件外,自定义的配置文件像test.yaml之类的都需要在AddWatcher前主动读取一次数据进行初始化监听(g.Cfg("test").Data(ctx))