重构:GoroutineTracker 不必要地使用反射
今天,我在公司代码库中遇到了这段代码(代码和注释是为了演示目的而重写的,不包含任何专有代码):
type GoroutineTracker struct { wg sync.WaitGroup // ... some other fields } // Go starts a new goroutine and tracks it with some metrics. func (g *GoroutineTracker) Go(ctx context.Context, name string, f any, args ...any) { fn := reflect.TypeOf(f) if fn.Kind() != reflect.Func { panic("must be function") } if fn.NumIn() != len(args) { panic("args does not match fn signature") } if fn.NumOut() > 0 { panic("output from fn is ignored") } g.wg.Add(1) id := g.startCaptureTime() go func() { defer func() { r := recover() // ... some panic handling code g.wg.Done() g.endCaptureTime(id) }() input := typez.MapFunc(args, func(arg any) reflect.Value { return reflect.ValueOf(arg) }) _ = reflect.ValueOf(f).Call(input) }() } // Wait for all goroutines to finished. func (g *GoroutineTracker) Wait() { g.wg.Wait() }
GoroutineTracker 用于跟踪代码库中 Goroutine 的使用情况,例如 Goroutine 的数量、每个 Goroutine 所花费的时间等。Go 方法用于启动一个新的 Goroutine 并跟踪它。 Wait 方法用于等待所有 goroutine 完成。
使用示例:
g := NewGoroutineTracker() g.Go(ctx, "task1", doTask1, arg1, arg2) g.Go(ctx, "task2", doTask2, arg3) g.Wait()
问题:reflect的使用是不必要的,可以避免
嗯,该代码可以工作,但它使用反射包来检查函数签名然后调用该函数。这是完全没有必要的,我们可以通过将用法更改为来避免它:
g := NewGoroutineTracker() g.Go(ctx, "task1", func() error { return doTask1(arg1, arg2) }) g.Go(ctx, "task2", func() error { return doTask2(arg3) })
新代码将更加简单,并且有很多好处:
- 类型安全:无需使用反射检查函数签名。编译器会为我们做这件事。如果函数签名与参数不匹配,原始代码可能会出现运行时错误。
- 错误处理:我们可以从函数返回错误并在调用者中处理它。原始代码忽略了函数的输出。
- 可读性:新代码更具可读性,更容易理解。我们可以直接在代码中看到函数签名和参数。
GoroutineTracker 的更好实现
这是重构后的代码:
func (g *GoroutineTracker) Go(ctx context.Context, fn func() error) { g.wg.Add(1) id := g.startCaptureTime() go func() (err error) { defer func() { r := recover() // capture returned error and panic g.endCaptureTime(id, r, err) g.wg.Done() }() // just call the function, no reflect needed return fn() }() }
等待所有 goroutine 完成后再关闭
GoroutineTracker 的另一个用例是在关闭应用程序之前等待所有 goroutine 完成。所以我们可以有两种等待方式:
- 在函数中:等待所有本地 goroutine 完成。
- 当应用程序关闭时:等待由任何 GoroutineTracker 启动的所有 goroutine 完成。
我们可以通过添加全局跟踪器并让任何跟踪器将其功能注册到全局跟踪器来实现:
type GlobalTracker struct { wg sync.WaitGroup // ... some other fields } type GoroutineTracker struct { parent *GlobalTracker wg sync.WaitGroup // ... some other fields } func (g *GlobalTracker) New() *GoroutineTracker { return &GoroutineTracker{parent: g} } func (g *GoroutineTracker) Go(ctx context.Context, fn func() error) { g.wg.Add(1) // use both parent and local wg g.parent.wg.Add(1) // to track the new goroutine id := g.startCaptureTime() go func() (err error) { defer func() { // ... g.endCaptureTime(id, r, err) g.wg.Done() g.parent.wg.Done() }() return fn() }() } func (g *GlobalTracker) WaitForAll() { g.wg.Wait() } func (g *GoroutineTracker) Wait() { g.wg.Wait() }
我们可以使用 WaitForAll() 等待所有 goroutine 完成后再关闭应用程序:
type FooService { tracker *GlobalTracker // ... some other fields } func (s *FooService) DoSomething(ctx context.Context) { g := s.tracker.New() g.Go(ctx, func() error { return s.doTask1(arg1, arg2) }) g.Go(ctx, func() error { return s.doTask2(arg3) }) g.Wait() // wait for local goroutines, this is optional } func main() { // some initialization, then start the application globalTracker := &GlobalTracker{} fooService := FooService{tracker: globalTracker, /*...*/} application.Start() // wait for all goroutines to finish before shutting down <-application.Done() globalTracker.Wait() }
结论
总之,虽然 GoroutineTracker 的原始实现可以工作并且可以跟踪 goroutine,但它使用 Reflect 包来动态检查和调用函数会带来不必要的复杂性和潜在的运行时错误。通过重构代码以直接接受函数文字,我们提高了类型安全性、简化了错误处理并增强了可读性。这种方法利用 Go 的编译器检查类型系统来确保函数签名和参数之间的兼容性,从而产生更健壮和可维护的代码。通过采用这些更改,我们优化了 GoroutineTracker 的清晰度和可靠性,与 Go 编程的最佳实践保持一致。
作者
我是奥利弗·阮。一家主要使用 Go 和 JavaScript 工作的软件制造商。我喜欢每天学习并看到更好的自己。偶尔会衍生出新的开源项目。在我的旅程中分享知识和想法。
以上是重构:GoroutineTracker 不必要地使用反射的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Golang在性能和可扩展性方面优于Python。1)Golang的编译型特性和高效并发模型使其在高并发场景下表现出色。2)Python作为解释型语言,执行速度较慢,但通过工具如Cython可优化性能。

Golang在并发性上优于C ,而C 在原始速度上优于Golang。1)Golang通过goroutine和channel实现高效并发,适合处理大量并发任务。2)C 通过编译器优化和标准库,提供接近硬件的高性能,适合需要极致优化的应用。

goisidealforbeginnersandsubableforforcloudnetworkservicesduetoitssimplicity,效率和concurrencyFeatures.1)installgromtheofficialwebsitealwebsiteandverifywith'.2)

Golang适合快速开发和并发场景,C 适用于需要极致性能和低级控制的场景。1)Golang通过垃圾回收和并发机制提升性能,适合高并发Web服务开发。2)C 通过手动内存管理和编译器优化达到极致性能,适用于嵌入式系统开发。

GoimpactsdevelopmentPositationalityThroughSpeed,效率和模拟性。1)速度:gocompilesquicklyandrunseff,ifealforlargeprojects.2)效率:效率:ITScomprehenSevestAndArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdEcceSteral Depentencies,增强开发的简单性:3)SimpleflovelmentIcties:3)简单性。

Golang和Python各有优势:Golang适合高性能和并发编程,Python适用于数据科学和Web开发。 Golang以其并发模型和高效性能着称,Python则以简洁语法和丰富库生态系统着称。

Golang和C 在性能上的差异主要体现在内存管理、编译优化和运行时效率等方面。1)Golang的垃圾回收机制方便但可能影响性能,2)C 的手动内存管理和编译器优化在递归计算中表现更为高效。

Golang和C 在性能竞赛中的表现各有优势:1)Golang适合高并发和快速开发,2)C 提供更高性能和细粒度控制。选择应基于项目需求和团队技术栈。
