重构: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)

OpenSSL,作为广泛应用于安全通信的开源库,提供了加密算法、密钥和证书管理等功能。然而,其历史版本中存在一些已知安全漏洞,其中一些危害极大。本文将重点介绍Debian系统中OpenSSL的常见漏洞及应对措施。DebianOpenSSL已知漏洞:OpenSSL曾出现过多个严重漏洞,例如:心脏出血漏洞(CVE-2014-0160):该漏洞影响OpenSSL1.0.1至1.0.1f以及1.0.2至1.0.2beta版本。攻击者可利用此漏洞未经授权读取服务器上的敏感信息,包括加密密钥等。

后端学习路径:从前端转型到后端的探索之旅作为一名从前端开发转型的后端初学者,你已经有了nodejs的基础,...

在BeegoORM框架下,如何指定模型关联的数据库?许多Beego项目需要同时操作多个数据库。当使用Beego...

Go语言中用于浮点数运算的库介绍在Go语言(也称为Golang)中,进行浮点数的加减乘除运算时,如何确保精度是�...

Go爬虫Colly中的Queue线程问题探讨在使用Go语言的Colly爬虫库时,开发者常常会遇到关于线程和请求队列的问题。�...

GoLand中自定义结构体标签不显示怎么办?在使用GoLand进行Go语言开发时,很多开发者会遇到自定义结构体标签在�...

Go语言中使用RedisStream实现消息队列时类型转换问题在使用Go语言与Redis...

本文介绍如何在Debian系统上配置MongoDB实现自动扩容,主要步骤包括MongoDB副本集的设置和磁盘空间监控。一、MongoDB安装首先,确保已在Debian系统上安装MongoDB。使用以下命令安装:sudoaptupdatesudoaptinstall-ymongodb-org二、配置MongoDB副本集MongoDB副本集确保高可用性和数据冗余,是实现自动扩容的基础。启动MongoDB服务:sudosystemctlstartmongodsudosys
