首页  >  文章  >  后端开发  >  重构:GoroutineTracker 不必要地使用反射

重构:GoroutineTracker 不必要地使用反射

PHPz
PHPz原创
2024-07-17 04:26:29634浏览

Refactor: GoroutineTracker with unnecessary usage of reflect

今天,我在公司代码库中遇到了这段代码(代码和注释是为了演示目的而重写的,不包含任何专有代码)

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中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn