github.com/khulnasoft-lab/khulnasoft@v26.0.1-0.20240328202558-330a6f959fe0+incompatible/container/attach_context.go (about)

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