attach_context.go 736 B

1234567891011121314151617181920212223242526272829303132333435
  1. package container
  2. import (
  3. "context"
  4. "sync"
  5. )
  6. // attachContext is the context used for for attach calls.
  7. type attachContext struct {
  8. mu sync.Mutex
  9. ctx context.Context
  10. cancelFunc context.CancelFunc
  11. }
  12. // init returns the context for attach calls. It creates a new context
  13. // if no context is created yet.
  14. func (ac *attachContext) init() context.Context {
  15. ac.mu.Lock()
  16. defer ac.mu.Unlock()
  17. if ac.ctx == nil {
  18. ac.ctx, ac.cancelFunc = context.WithCancel(context.Background())
  19. }
  20. return ac.ctx
  21. }
  22. // cancelFunc cancels the attachContext. All attach calls should detach
  23. // after this call.
  24. func (ac *attachContext) cancel() {
  25. ac.mu.Lock()
  26. if ac.ctx != nil {
  27. ac.cancelFunc()
  28. ac.ctx = nil
  29. }
  30. ac.mu.Unlock()
  31. }