SHARE
TWEET

Untitled

a guest Mar 20th, 2017 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package gps
  2.  
  3. import (
  4.     "context"
  5.     "sync/atomic"
  6. )
  7.  
  8. func (sm *SourceMgr) ListPackages(ctx context.Context, id ProjectIdentifier, v Version) (PackageTree, error) {
  9.     subctx, cancel := context.WithCancel(ctx)
  10.     quitchan := make(chan struct{})
  11.     go func() {
  12.         select {
  13.         case <-ctx.Done():
  14.             // cancel initiated by the caller
  15.         case <-sm.cm.quitchan:
  16.             // quitchan closed centrally; trigger the cancel func for the subctx
  17.             cancel()
  18.         case <-quitchan:
  19.             // local quitchan was closed
  20.         }
  21.     }
  22.  
  23.     // do stuff
  24.     // ...
  25.  
  26.     // close the quitchan to kill the goroutine
  27.     close(quitchan)
  28. }
RAW Paste Data
Top