-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(scorch): ensure run is canceled if experiment is stopped
- Loading branch information
1 parent
3641854
commit 5659978
Showing
5 changed files
with
140 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package scorchexe | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
"sync" | ||
) | ||
|
||
var ( | ||
cancelers = make(map[string]context.CancelFunc) | ||
cancelersMu sync.Mutex | ||
) | ||
|
||
func AddCanceler(ctx context.Context, exp string, run int) context.Context { | ||
key := fmt.Sprintf("%s/%d", exp, run) | ||
|
||
cancelersMu.Lock() | ||
defer cancelersMu.Unlock() | ||
|
||
ctx, cancel := context.WithCancel(ctx) | ||
cancelers[key] = cancel | ||
|
||
return ctx | ||
} | ||
|
||
func HasCanceler(exp string, run int) bool { | ||
key := fmt.Sprintf("%s/%d", exp, run) | ||
|
||
cancelersMu.Lock() | ||
defer cancelersMu.Unlock() | ||
|
||
_, ok := cancelers[key] | ||
|
||
return ok | ||
} | ||
|
||
func GetExperimentCancelers(exp string) []context.CancelFunc { | ||
var expCancelers []context.CancelFunc | ||
|
||
cancelersMu.Lock() | ||
defer cancelersMu.Unlock() | ||
|
||
for run := range cancelers { | ||
// run keys are prefixed with the name of the experiment | ||
if strings.HasPrefix(run, exp+"/") { | ||
expCancelers = append(expCancelers, cancelers[run]) | ||
delete(cancelers, run) | ||
} | ||
} | ||
|
||
return expCancelers | ||
} | ||
|
||
func GetCanceler(exp string, run int) context.CancelFunc { | ||
key := fmt.Sprintf("%s/%d", exp, run) | ||
|
||
cancelersMu.Lock() | ||
defer cancelersMu.Unlock() | ||
|
||
cancel := cancelers[key] | ||
delete(cancelers, key) | ||
|
||
return cancel | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters