gitee.com/zhaochuninhefei/gmgo@v0.0.31-0.20240209061119-069254a02979/go-control-plane/pkg/server/delta/v3/watches.go (about)

     1  package delta
     2  
     3  import (
     4  	"gitee.com/zhaochuninhefei/gmgo/go-control-plane/pkg/cache/types"
     5  	"gitee.com/zhaochuninhefei/gmgo/go-control-plane/pkg/cache/v3"
     6  	"gitee.com/zhaochuninhefei/gmgo/go-control-plane/pkg/server/stream/v3"
     7  )
     8  
     9  // watches for all delta xDS resource types
    10  type watches struct {
    11  	deltaWatches map[string]watch
    12  
    13  	// Opaque resources share a muxed channel
    14  	deltaMuxedResponses chan cache.DeltaResponse
    15  }
    16  
    17  // newWatches creates and initializes watches.
    18  func newWatches() watches {
    19  	// deltaMuxedResponses needs a buffer to release go-routines populating it
    20  	return watches{
    21  		deltaWatches:        make(map[string]watch, types.UnknownType),
    22  		deltaMuxedResponses: make(chan cache.DeltaResponse, types.UnknownType),
    23  	}
    24  }
    25  
    26  // Cancel all watches
    27  func (w *watches) Cancel() {
    28  	for _, watch := range w.deltaWatches {
    29  		if watch.cancel != nil {
    30  			watch.cancel()
    31  		}
    32  	}
    33  }
    34  
    35  // watch contains the necessary modifiables for receiving resource responses
    36  type watch struct {
    37  	responses chan cache.DeltaResponse
    38  	cancel    func()
    39  	nonce     string
    40  
    41  	state stream.StreamState
    42  }
    43  
    44  // Cancel calls terminate and cancel
    45  func (w *watch) Cancel() {
    46  	if w.cancel != nil {
    47  		w.cancel()
    48  	}
    49  	close(w.responses)
    50  }