Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove ContextDeriver and related functions #430

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 2 additions & 38 deletions githubapp/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,6 @@ func MetricsAsyncErrorCallback(reg metrics.Registry) AsyncErrorCallback {
}
}

// ContextDeriver creates a new independent context from a request's context.
// The new context must be based on context.Background(), not the input.
type ContextDeriver func(context.Context) context.Context

// DefaultContextDeriver copies the logger from the request's context to a new
// context.
func DefaultContextDeriver(ctx context.Context) context.Context {
newCtx := context.Background()

// this value is always unused by async schedulers, but is set for
// compatibility with existing handlers that call SetResponder
newCtx = InitializeResponder(newCtx)

return zerolog.Ctx(ctx).WithContext(newCtx)
}

// Scheduler is a strategy for executing event handlers.
//
// The Schedule method takes a Dispatch and executes it by calling the handler
Expand Down Expand Up @@ -120,16 +104,6 @@ func WithAsyncErrorCallback(onError AsyncErrorCallback) SchedulerOption {
}
}

// WithContextDeriver sets the context deriver for an asynchronous scheduler.
// If not set, the scheduler uses DefaultContextDeriver.
func WithContextDeriver(deriver ContextDeriver) SchedulerOption {
return func(s *scheduler) {
if deriver != nil {
s.deriver = deriver
}
}
}

// WithSchedulingMetrics enables metrics reporting for schedulers.
func WithSchedulingMetrics(r metrics.Registry) SchedulerOption {
return func(s *scheduler) {
Expand All @@ -155,7 +129,6 @@ type queueDispatch struct {
// core functionality and options for (async) schedulers
type scheduler struct {
onError AsyncErrorCallback
deriver ContextDeriver

activeWorkers int64
queue chan queueDispatch
Expand Down Expand Up @@ -183,13 +156,6 @@ func (s *scheduler) safeExecute(ctx context.Context, d Dispatch) {
err = d.Execute(ctx)
}

func (s *scheduler) derive(ctx context.Context) context.Context {
if s.deriver == nil {
return ctx
}
return s.deriver(ctx)
}

// DefaultScheduler returns a scheduler that executes handlers in the go
// routine of the caller and returns any error.
func DefaultScheduler() Scheduler {
Expand All @@ -207,7 +173,6 @@ func (s *defaultScheduler) Schedule(ctx context.Context, d Dispatch) error {
func AsyncScheduler(opts ...SchedulerOption) Scheduler {
s := &asyncScheduler{
scheduler: scheduler{
deriver: DefaultContextDeriver,
onError: DefaultAsyncErrorCallback,
},
}
Expand All @@ -222,7 +187,7 @@ type asyncScheduler struct {
}

func (s *asyncScheduler) Schedule(ctx context.Context, d Dispatch) error {
go s.safeExecute(s.derive(ctx), d)
go s.safeExecute(context.WithoutCancel(ctx), d)
return nil
}

Expand All @@ -239,7 +204,6 @@ func QueueAsyncScheduler(queueSize int, workers int, opts ...SchedulerOption) Sc

s := &queueScheduler{
scheduler: scheduler{
deriver: DefaultContextDeriver,
onError: DefaultAsyncErrorCallback,
queue: make(chan queueDispatch, queueSize),
},
Expand Down Expand Up @@ -268,7 +232,7 @@ type queueScheduler struct {

func (s *queueScheduler) Schedule(ctx context.Context, d Dispatch) error {
select {
case s.queue <- queueDispatch{ctx: s.derive(ctx), t: time.Now(), d: d}:
case s.queue <- queueDispatch{ctx: context.WithoutCancel(ctx), t: time.Now(), d: d}:
default:
if s.dropped != nil {
s.dropped.Inc(1)
Expand Down