Last active
March 21, 2017 16:06
-
-
Save sdboyer/501ed8398dbb15a8c9f779012e71389e to your computer and use it in GitHub Desktop.
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
package gps | |
import ( | |
"context" | |
"sync/atomic" | |
) | |
func (sm *SourceMgr) ListPackages(ctx context.Context, id ProjectIdentifier, v Version) (PackageTree, error) { | |
subctx, cancel := context.WithCancel(ctx) | |
quitchan := make(chan struct{}) | |
go func() { | |
select { | |
case <-ctx.Done(): | |
// cancel initiated by the caller | |
case <-sm.cm.quitchan: | |
// quitchan closed centrally; trigger the cancel func for the subctx | |
cancel() | |
case <-quitchan: | |
// local quitchan was closed | |
} | |
} | |
// do stuff | |
// ... | |
// close the quitchan to kill the goroutine | |
close(quitchan) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment