Skip to content

Commit 6962804

Browse files
authoredMar 18, 2025··
Remove ContextDeriver and related functions (#430)
This was added as a way to create a detatched context for async schedulers that still had some of the values of the original. Starting in Go 1.21, the standard library now provides the WithoutCancel function to make a copy of the values in a context but with a different lifetime. Custom implementations of ContextDervier are likely very rare (if they exist at all) and their main purpose would be to copy context values other than the logger. Since WithoutCancel now copies all values, I don't see any reason to keep supporting custom functions. While we could deprecate the existing functions, their rare usage combined with the fact that we frequently release breaking changes for go-github upgrades makes me believe this is safe.
1 parent eadd1d2 commit 6962804

File tree

1 file changed

+2
-38
lines changed

1 file changed

+2
-38
lines changed
 

‎githubapp/scheduler.go

+2-38
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,6 @@ func MetricsAsyncErrorCallback(reg metrics.Registry) AsyncErrorCallback {
7777
}
7878
}
7979

80-
// ContextDeriver creates a new independent context from a request's context.
81-
// The new context must be based on context.Background(), not the input.
82-
type ContextDeriver func(context.Context) context.Context
83-
84-
// DefaultContextDeriver copies the logger from the request's context to a new
85-
// context.
86-
func DefaultContextDeriver(ctx context.Context) context.Context {
87-
newCtx := context.Background()
88-
89-
// this value is always unused by async schedulers, but is set for
90-
// compatibility with existing handlers that call SetResponder
91-
newCtx = InitializeResponder(newCtx)
92-
93-
return zerolog.Ctx(ctx).WithContext(newCtx)
94-
}
95-
9680
// Scheduler is a strategy for executing event handlers.
9781
//
9882
// The Schedule method takes a Dispatch and executes it by calling the handler
@@ -120,16 +104,6 @@ func WithAsyncErrorCallback(onError AsyncErrorCallback) SchedulerOption {
120104
}
121105
}
122106

123-
// WithContextDeriver sets the context deriver for an asynchronous scheduler.
124-
// If not set, the scheduler uses DefaultContextDeriver.
125-
func WithContextDeriver(deriver ContextDeriver) SchedulerOption {
126-
return func(s *scheduler) {
127-
if deriver != nil {
128-
s.deriver = deriver
129-
}
130-
}
131-
}
132-
133107
// WithSchedulingMetrics enables metrics reporting for schedulers.
134108
func WithSchedulingMetrics(r metrics.Registry) SchedulerOption {
135109
return func(s *scheduler) {
@@ -155,7 +129,6 @@ type queueDispatch struct {
155129
// core functionality and options for (async) schedulers
156130
type scheduler struct {
157131
onError AsyncErrorCallback
158-
deriver ContextDeriver
159132

160133
activeWorkers int64
161134
queue chan queueDispatch
@@ -183,13 +156,6 @@ func (s *scheduler) safeExecute(ctx context.Context, d Dispatch) {
183156
err = d.Execute(ctx)
184157
}
185158

186-
func (s *scheduler) derive(ctx context.Context) context.Context {
187-
if s.deriver == nil {
188-
return ctx
189-
}
190-
return s.deriver(ctx)
191-
}
192-
193159
// DefaultScheduler returns a scheduler that executes handlers in the go
194160
// routine of the caller and returns any error.
195161
func DefaultScheduler() Scheduler {
@@ -207,7 +173,6 @@ func (s *defaultScheduler) Schedule(ctx context.Context, d Dispatch) error {
207173
func AsyncScheduler(opts ...SchedulerOption) Scheduler {
208174
s := &asyncScheduler{
209175
scheduler: scheduler{
210-
deriver: DefaultContextDeriver,
211176
onError: DefaultAsyncErrorCallback,
212177
},
213178
}
@@ -222,7 +187,7 @@ type asyncScheduler struct {
222187
}
223188

224189
func (s *asyncScheduler) Schedule(ctx context.Context, d Dispatch) error {
225-
go s.safeExecute(s.derive(ctx), d)
190+
go s.safeExecute(context.WithoutCancel(ctx), d)
226191
return nil
227192
}
228193

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

240205
s := &queueScheduler{
241206
scheduler: scheduler{
242-
deriver: DefaultContextDeriver,
243207
onError: DefaultAsyncErrorCallback,
244208
queue: make(chan queueDispatch, queueSize),
245209
},
@@ -268,7 +232,7 @@ type queueScheduler struct {
268232

269233
func (s *queueScheduler) Schedule(ctx context.Context, d Dispatch) error {
270234
select {
271-
case s.queue <- queueDispatch{ctx: s.derive(ctx), t: time.Now(), d: d}:
235+
case s.queue <- queueDispatch{ctx: context.WithoutCancel(ctx), t: time.Now(), d: d}:
272236
default:
273237
if s.dropped != nil {
274238
s.dropped.Inc(1)

0 commit comments

Comments
 (0)
Please sign in to comment.