Skip to content

Commit

Permalink
Merge pull request #7 from runreveal/alan/await
Browse files Browse the repository at this point in the history
await: apply options
  • Loading branch information
abraithwaite authored May 14, 2024
2 parents 50c2ad6 + ea891b9 commit 5886615
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions await/await.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,14 @@ func WithStopTimeout(d time.Duration) Option {
}
}

func New(...Option) *runner {
return &runner{
func New(opts ...Option) *runner {
r := &runner{
funcs: make([]RunFunc, 0),
}
for _, opt := range opts {
opt(r)
}
return r
}

func (r *runner) Add(f Runner) {
Expand Down Expand Up @@ -99,10 +103,12 @@ func (r *runner) Run(ctx context.Context) error {
atomic.AddInt32(&waitCount, 1)
go func(fn func(context.Context) error, idx int) {
err := fn(ctx)
if r.funcNames[idx] != "" {
slog.Info(fmt.Sprintf("subroutine %s returned: %+v", r.funcNames[idx], err))
} else {
slog.Info(fmt.Sprintf("subroutine error: %+v", err))
if err != nil && !errors.Is(err, context.Canceled) {
if r.funcNames[idx] != "" {
slog.Info(fmt.Sprintf("subroutine %s error: %+v", r.funcNames[idx], err))
} else {
slog.Info(fmt.Sprintf("subroutine error: %+v", err))
}
}
errc <- err
atomic.AddInt32(&waitCount, -1)
Expand Down Expand Up @@ -136,6 +142,7 @@ func (r *runner) Run(ctx context.Context) error {
waitOrTimeout(r.stopTimeout, &waitCount)

if errors.Is(err, context.Canceled) {
slog.Info("await: stopped on context canceled")
return nil
}

Expand Down

0 comments on commit 5886615

Please sign in to comment.