Skip to content

Commit fbaec31

Browse files
committed
feat: extend stopwatch
1 parent 34209d7 commit fbaec31

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

pkg/timeutils/stopwatch.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,25 @@ func (s *Stopwatch) TrackStage(name string, f func()) {
114114
s.stages[name] += time.Since(startedAt)
115115
s.mu.Unlock()
116116
}
117+
118+
func (s *Stopwatch) TrackStageErr(name string, f func() error) error {
119+
startedAt := time.Now()
120+
err := f()
121+
122+
s.mu.Lock()
123+
s.stages[name] += time.Since(startedAt)
124+
s.mu.Unlock()
125+
126+
return err
127+
}
128+
129+
func TrackStage[T any](s *Stopwatch, name string, f func() (T, error)) (T, error) {
130+
var result T
131+
var err error
132+
133+
s.TrackStage(name, func() {
134+
result, err = f()
135+
})
136+
137+
return result, err
138+
}

0 commit comments

Comments
 (0)