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

     1  package stream
     2  
     3  import (
     4  	"gitee.com/zhaochuninhefei/gmgo/grpc"
     5  
     6  	discovery "gitee.com/zhaochuninhefei/gmgo/go-control-plane/envoy/service/discovery/v3"
     7  )
     8  
     9  // Stream Generic RPC stream.
    10  type Stream interface {
    11  	grpc.ServerStream
    12  
    13  	Send(*discovery.DiscoveryResponse) error
    14  	Recv() (*discovery.DiscoveryRequest, error)
    15  }
    16  
    17  type DeltaStream interface {
    18  	grpc.ServerStream
    19  
    20  	Send(*discovery.DeltaDiscoveryResponse) error
    21  	Recv() (*discovery.DeltaDiscoveryRequest, error)
    22  }
    23  
    24  // StreamState will keep track of resource state per type on a stream.
    25  //goland:noinspection GoNameStartsWithPackageName
    26  type StreamState struct { // nolint:golint,revive
    27  	// Indicates whether the original DeltaRequest was a wildcard LDS/RDS request.
    28  	wildcard bool
    29  
    30  	// ResourceVersions contains a hash of the resource as the value and the resource name as the key.
    31  	// This field stores the last state sent to the client.
    32  	resourceVersions map[string]string
    33  
    34  	// indicates whether the object has beed modified since its creation
    35  	first bool
    36  }
    37  
    38  func (s *StreamState) GetResourceVersions() map[string]string {
    39  	return s.resourceVersions
    40  }
    41  
    42  func (s *StreamState) SetResourceVersions(resourceVersions map[string]string) {
    43  	s.first = false
    44  	s.resourceVersions = resourceVersions
    45  }
    46  
    47  func (s *StreamState) IsFirst() bool {
    48  	return s.first
    49  }
    50  
    51  func (s *StreamState) IsWildcard() bool {
    52  	return s.wildcard
    53  }
    54  
    55  // NewStreamState initializes a stream state.
    56  func NewStreamState(wildcard bool, initialResourceVersions map[string]string) StreamState {
    57  	state := StreamState{
    58  		wildcard:         wildcard,
    59  		resourceVersions: initialResourceVersions,
    60  		first:            true,
    61  	}
    62  
    63  	if initialResourceVersions == nil {
    64  		state.resourceVersions = make(map[string]string)
    65  	}
    66  
    67  	return state
    68  }