Skip to content

Commit

Permalink
pkg/services: add StopChan & StopRChan from core (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmank88 authored and samsondav committed Jan 11, 2024
1 parent 2b4f852 commit 94027eb
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 9 deletions.
50 changes: 50 additions & 0 deletions pkg/services/stop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package services

import "context"

// A StopChan signals when some work should stop.
// Use StopChanR if you already have a read only <-chan.
type StopChan chan struct{}

// NewCtx returns a background [context.Context] that is cancelled when StopChan is closed.
func (s StopChan) NewCtx() (context.Context, context.CancelFunc) {
return StopRChan((<-chan struct{})(s)).NewCtx()
}

// Ctx cancels a [context.Context] when StopChan is closed.
func (s StopChan) Ctx(ctx context.Context) (context.Context, context.CancelFunc) {
return StopRChan((<-chan struct{})(s)).Ctx(ctx)
}

// 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) {
return StopRChan((<-chan struct{})(s)).CtxCancel(ctx, cancel)
}

// A StopRChan signals when some work should stop.
// This is a receive-only version of StopChan, for casting an existing <-chan.
type StopRChan <-chan struct{}

// NewCtx returns a background [context.Context] that is cancelled when StopChan is closed.
func (s StopRChan) NewCtx() (context.Context, context.CancelFunc) {
return s.Ctx(context.Background())
}

// Ctx cancels a [context.Context] when StopChan is closed.
func (s StopRChan) Ctx(ctx context.Context) (context.Context, context.CancelFunc) {
return s.CtxCancel(context.WithCancel(ctx))
}

// 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) {
go func() {
select {
case <-s:
cancel()
case <-ctx.Done():
}
}()
return ctx, cancel
}
12 changes: 3 additions & 9 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"math"
mrand "math/rand"
"time"

"github.com/smartcontractkit/chainlink-relay/pkg/services"
)

// WithJitter adds +/- 10% to a duration
Expand All @@ -27,15 +29,7 @@ func WithJitter(d time.Duration) time.Duration {
// NOTE: Spins up a goroutine that exits on cancellation.
// REMEMBER TO CALL CANCEL OTHERWISE IT CAN LEAD TO MEMORY LEAKS
func ContextFromChan(chStop <-chan struct{}) (context.Context, context.CancelFunc) {
ctx, cancel := context.WithCancel(context.Background())
go func() {
select {
case <-chStop:
cancel()
case <-ctx.Done():
}
}()
return ctx, cancel
return services.StopRChan(chStop).NewCtx()
}

// ContextWithDeadlineFn returns a copy of the parent context with the deadline modified by deadlineFn.
Expand Down

0 comments on commit 94027eb

Please sign in to comment.