Skip to content

Commit d6ec34b

Browse files
committed
pkg/services: add Engine.GoTickUntil & Engine.WaitFor
1 parent b1fd6cb commit d6ec34b

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

pkg/services/service.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,35 @@ func (e *Engine) GoTick(ticker *timeutil.Ticker, fn func(context.Context)) {
107107
})
108108
}
109109

110+
// GoTickUntil is like GoTick but halts when fn returns true, and closes the returned chan.
111+
func (e *Engine) GoTickUntil(ticker *timeutil.Ticker, fn func(context.Context) bool) <-chan struct{} {
112+
ch := make(chan struct{})
113+
e.Go(func(ctx context.Context) {
114+
defer ticker.Stop()
115+
for {
116+
select {
117+
case <-ctx.Done():
118+
return
119+
case <-ticker.C:
120+
if fn(ctx) {
121+
close(ch)
122+
return
123+
}
124+
}
125+
}
126+
})
127+
return ch
128+
}
129+
130+
func (e *Engine) WaitFor(ch <-chan struct{}) bool {
131+
select {
132+
case <-e.StopChan:
133+
return false
134+
case <-ch:
135+
}
136+
return true
137+
}
138+
110139
// Tracer returns the otel tracer with service attributes included.
111140
func (e *Engine) Tracer() trace.Tracer {
112141
return e.tracer

0 commit comments

Comments
 (0)