Skip to content

Instantly share code, notes, and snippets.

@sdboyer
Last active March 21, 2017 16:06
Show Gist options
  • Save sdboyer/501ed8398dbb15a8c9f779012e71389e to your computer and use it in GitHub Desktop.
Save sdboyer/501ed8398dbb15a8c9f779012e71389e to your computer and use it in GitHub Desktop.
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