close.go 888 B

1234567891011121314151617181920212223242526272829303132333435
  1. package signal
  2. // CancelSignal is a signal passed to goroutine, in order to cancel the goroutine on demand.
  3. type CancelSignal struct {
  4. cancel chan struct{}
  5. done chan struct{}
  6. }
  7. // NewCloseSignal creates a new CancelSignal.
  8. func NewCloseSignal() *CancelSignal {
  9. return &CancelSignal{
  10. cancel: make(chan struct{}),
  11. done: make(chan struct{}),
  12. }
  13. }
  14. // Cancel signals the goroutine to stop.
  15. func (this *CancelSignal) Cancel() {
  16. close(this.cancel)
  17. }
  18. // WaitForCancel should be monitored by the goroutine for when to stop.
  19. func (this *CancelSignal) WaitForCancel() <-chan struct{} {
  20. return this.cancel
  21. }
  22. // Done signals the caller that the goroutine has completely finished.
  23. func (this *CancelSignal) Done() {
  24. close(this.done)
  25. }
  26. // WaitForDone is used by caller to wait for the goroutine finishes.
  27. func (this *CancelSignal) WaitForDone() <-chan struct{} {
  28. return this.done
  29. }