Skip to content

feat(os/gcfg): Add file watcher with custom callback support#4446

Merged
hailaz merged 38 commits intogogf:masterfrom
LanceAdd:fix/gcfg-watcher
Oct 15, 2025
Merged

feat(os/gcfg): Add file watcher with custom callback support#4446
hailaz merged 38 commits intogogf:masterfrom
LanceAdd:fix/gcfg-watcher

Conversation

@LanceAdd
Copy link
Member

gcfg添加配置文件变更自定义回调,实现了WatcherAdapter接口,以下是AdapterFile的用法
test.yaml

b: "b"

package main

import (
	"fmt"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gcfg"
	"github.com/gogf/gf/v2/os/gctx"
)

func main() {
	ctx := gctx.New()
	file, _ := gcfg.NewAdapterFile("test.yaml")
	file.Data(ctx)
	file.AddWatcher("test", func() {
		value := file.MustGet(ctx, "b")
		fmt.Println(value.String())
	})
	server := g.Server()
	server.Run()
}

使用g和默认配置文件

	file := g.Cfg().GetAdapter().(*gcfg.AdapterFile)
	file.AddWatcher("test", func() {

	})
	file := g.Cfg().GetAdapter().(*gcfg.AdapterFile)
	file.RemoveWatcher("test")

注意:由于gfAdapterFile使用的监听到文件变化删除缓存下一次重新初始化的懒加载方案,所有除了默认加载的config.xxx文件外,自定义的配置文件像test.yaml之类的都需要在AddWatcher前主动读取一次数据进行初始化监听(
g.Cfg("test").Data(ctx))

- 新增 WatcherAdapter 接口,支持添加和移除监听函数- 在 AdapterContent 和 AdapterFile 中实现监听器机制
- 添加 notifyWatchers 方法,用于通知所有监听器配置变更
- 实现文件变化监听,自动触发监听器回调
- 增加单元测试,验证监听器的添加、移除和通知功能
- 支持内容变更时触发监听器回调
@hailaz
Copy link
Contributor

hailaz commented Sep 23, 2025

@LanceAdd 看实现的功能,只能知道文件变化了,但不知道文件是由哪个事件引起的变化,是不是应该将变化的具体情况也透传一下?

@hailaz
Copy link
Contributor

hailaz commented Sep 23, 2025

@LanceAdd 看实现的功能,只能知道文件变化了,但不知道文件是由哪个事件引起的变化,是不是应该将变化的具体情况也透传一下?

另外可能要考虑一下是否预置一个ctx context.Context参数

@wln32
Copy link
Member

wln32 commented Sep 23, 2025

@LanceAdd 看实现的功能,只能知道文件变化了,但不知道文件是由哪个事件引起的变化,是不是应该将变化的具体情况也透传一下?

另外可能要考虑一下是否预置一个ctx context.Context参数

赞同,是否也应该增加一个file参数?

@LanceAdd
Copy link
Member Author

@LanceAdd 看实现的功能,只能知道文件变化了,但不知道文件是由哪个事件引起的变化,是不是应该将变化的具体情况也透传一下?

我在内部魔改的版本是带个ctx参数,把这些信息都放到ctx里面,需要什么就去ctx里拿,不同实现的Adapter结构体参数可能不一样。
这个不加的原因是gcfg的具体实现像AdapterFileAdapterContent还有AdapterNacos这些的配置缓存的更新方式和配置来源元数据都不相同,有的懒加载有的主动刷新,然后是只要个path的本地文件,有的是需要namespace/groupId之类的,没法统一,不知道大家能不能接受,所以才希望大家不关注具体细节,只关注事件发生以及触发后自己去读自己需要的东西进行具体操作。如果大家能接受ctx的方案,我明天加一下,或者有其他方案也可以让我学习一下

@houseme houseme requested a review from Copilot September 26, 2025 08:48
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 WatcherAdapter interface with AddWatcher and RemoveWatcher methods
  • Implements watcher functionality in both AdapterFile and AdapterContent with 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.

LanceAdd and others added 2 commits September 27, 2025 22:31
- 修改 WatcherAdapter 接口,使 AddWatcher 方法接受 context.Context 参数
- 新增 GetWatcherNames 方法用于获取所有监听器名称
- 为 AdapterContent 和 AdapterFile 实现新的上下文支持方法
- 创建 AdapterContentCtx 和 AdapterFileCtx 结构体及其实现- 添加配置操作相关的上下文键值常量定义文件
- 更新 notifyWatchers 方法以传递上下文信息
- 扩展单元测试以验证新添加的上下文功能
- 将 defaultFileNameOrPath 类型从字符串改为 *gtype.String 以提高并发安全性- 在文件系统事件监听中增加对更多事件类型的支持并传递相应上下文- 优化代码结构和变量命名以增强可读性和一致性
@hailaz
Copy link
Contributor

hailaz commented Sep 28, 2025

@wln32 cc

@hailaz hailaz requested a review from wln32 September 28, 2025 09:40
@hailaz hailaz changed the title fix(gcfg): Add file watcher with custom callback support fix(os/gcfg): Add file watcher with custom callback support Sep 29, 2025
@hailaz hailaz changed the title fix(os/gcfg): Add file watcher with custom callback support feat(os/gcfg): Add file watcher with custom callback support Sep 29, 2025
@hailaz hailaz requested a review from joy999 September 29, 2025 08:45
@hailaz hailaz requested a review from Copilot October 15, 2025 07:13
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants