Remove context from task struct as it violates the pattern #31
Labels
blocked
Blocked until more information is added.
bug
Something isn't working
Difficulty: Intermediate
Describe the bug
Context should not be stored in structs, but instead be passed explicitly to the function. The current implementation violates the pattern and needs to be resolved.
To Reproduce
N/A
Expected behavior
N/A
Screenshots
N/A
Additional context
https://golang.org/pkg/context/ specifications explicitly state, not storing the context in the struct. In
task.go
we store the context and cancelFn within the struct. This was needed to provide a way to cancel running tasks, when thetask.Cancel()
function is invoked by the caller (i.e. the dispatcher, on handling aPOST /v1/api/attack/<attackID/cancel
request.Potential Solution
The dispatcher is responsible for creating a new context for every task, prior to calling the tasks
Run()
method. The ctx should be passed to theRun()
method explicitly, which can then be passed around among the task methods. The dispatcher instead can keep a reference to the context and invoke the cancel function by passing the stored context to the task'sCancel()
method.The text was updated successfully, but these errors were encountered: