// Copyright 2020 Kentaro Hibino. All rights reserved.// Use of this source code is governed by a MIT license// that can be found in the LICENSE file.package contextimport ()// A taskMetadata holds task scoped data to put in context.type taskMetadata struct { id string maxRetry int retryCount int qname string}// ctxKey type is unexported to prevent collisions with context keys defined in// other packages.type ctxKey int// metadataCtxKey is the context key for the task metadata.// Its value of zero is arbitrary.const metadataCtxKey ctxKey = 0// New returns a context and cancel function for a given task message.func ( context.Context, *base.TaskMessage, time.Time) (context.Context, context.CancelFunc) { := taskMetadata{id: .ID,maxRetry: .Retry,retryCount: .Retried,qname: .Queue, } := context.WithValue(, metadataCtxKey, )returncontext.WithDeadline(, )}// GetTaskID extracts a task ID from a context, if any.//// ID of a task is guaranteed to be unique.// ID of a task doesn't change if the task is being retried.func ( context.Context) ( string, bool) { , := .Value(metadataCtxKey).(taskMetadata)if ! {return"", false }return .id, true}// GetRetryCount extracts retry count from a context, if any.//// Return value n indicates the number of times associated task has been// retried so far.func ( context.Context) ( int, bool) { , := .Value(metadataCtxKey).(taskMetadata)if ! {return0, false }return .retryCount, true}// GetMaxRetry extracts maximum retry from a context, if any.//// Return value n indicates the maximum number of times the associated task// can be retried if ProcessTask returns a non-nil error.func ( context.Context) ( int, bool) { , := .Value(metadataCtxKey).(taskMetadata)if ! {return0, false }return .maxRetry, true}// GetQueueName extracts queue name from a context, if any.//// Return value qname indicates which queue the task was pulled from.func ( context.Context) ( string, bool) { , := .Value(metadataCtxKey).(taskMetadata)if ! {return"", false }return .qname, true}
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.