Skip to content

Commit

Permalink
pkg/services: add StopChan.CtxWithTimeout() (#879)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmank88 authored Oct 23, 2024
1 parent 86c89e2 commit 2218392
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion pkg/services/stop.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package services

import "context"
import (
"context"
"time"
)

// A StopChan signals when some work should stop.
// Use StopChanR if you already have a read only <-chan.
Expand All @@ -16,6 +19,11 @@ func (s StopChan) Ctx(ctx context.Context) (context.Context, context.CancelFunc)
return StopRChan((<-chan struct{})(s)).Ctx(ctx)
}

// CtxWithTimeout cancels a [context.Context] when StopChan is closed.
func (s StopChan) CtxWithTimeout(timeout time.Duration) (context.Context, context.CancelFunc) {
return s.CtxCancel(context.WithTimeout(context.Background(), timeout))
}

// CtxCancel cancels a [context.Context] when StopChan is closed.
// Returns ctx and cancel unmodified, for convenience.
func (s StopChan) CtxCancel(ctx context.Context, cancel context.CancelFunc) (context.Context, context.CancelFunc) {
Expand All @@ -36,6 +44,11 @@ func (s StopRChan) Ctx(ctx context.Context) (context.Context, context.CancelFunc
return s.CtxCancel(context.WithCancel(ctx))
}

// CtxWithTimeout cancels a [context.Context] when StopChan is closed.
func (s StopRChan) CtxWithTimeout(timeout time.Duration) (context.Context, context.CancelFunc) {
return s.CtxCancel(context.WithTimeout(context.Background(), timeout))
}

// CtxCancel cancels a [context.Context] when StopChan is closed.
// Returns ctx and cancel unmodified, for convenience.
func (s StopRChan) CtxCancel(ctx context.Context, cancel context.CancelFunc) (context.Context, context.CancelFunc) {
Expand Down

0 comments on commit 2218392

Please sign in to comment.