github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/go-control-plane/pkg/cache/v3/status.go (about)

     1  // Copyright 2018 Envoyproxy Authors
     2  //
     3  //   Licensed under the Apache License, Version 2.0 (the "License");
     4  //   you may not use this file except in compliance with the License.
     5  //   You may obtain a copy of the License at
     6  //
     7  //       http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  //   Unless required by applicable law or agreed to in writing, software
    10  //   distributed under the License is distributed on an "AS IS" BASIS,
    11  //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  //   See the License for the specific language governing permissions and
    13  //   limitations under the License.
    14  
    15  package cache
    16  
    17  import (
    18  	"sync"
    19  	"time"
    20  
    21  	core "github.com/hxx258456/ccgo/go-control-plane/envoy/config/core/v3"
    22  	"github.com/hxx258456/ccgo/go-control-plane/pkg/server/stream/v3"
    23  )
    24  
    25  // NodeHash computes string identifiers for Envoy nodes.
    26  type NodeHash interface {
    27  	// ID function defines a unique string identifier for the remote Envoy node.
    28  	ID(node *core.Node) string
    29  }
    30  
    31  // IDHash uses ID field as the node hash.
    32  type IDHash struct{}
    33  
    34  // ID uses the node ID field
    35  func (IDHash) ID(node *core.Node) string {
    36  	if node == nil {
    37  		return ""
    38  	}
    39  	return node.Id
    40  }
    41  
    42  var _ NodeHash = IDHash{}
    43  
    44  // StatusInfo tracks the server state for the remote Envoy node.
    45  // Not all fields are used by all cache implementations.
    46  type StatusInfo interface {
    47  	// GetNode returns the node metadata.
    48  	GetNode() *core.Node
    49  
    50  	// GetNumWatches returns the number of open watches.
    51  	GetNumWatches() int
    52  
    53  	// GetNumDeltaWatches returns the number of open delta watches.
    54  	GetNumDeltaWatches() int
    55  
    56  	// GetLastWatchRequestTime returns the timestamp of the last discovery watch request.
    57  	GetLastWatchRequestTime() time.Time
    58  
    59  	// GetLastDeltaWatchRequestTime returns the timestamp of the last delta discovery watch request.
    60  	GetLastDeltaWatchRequestTime() time.Time
    61  
    62  	// SetLastDeltaWatchRequestTime will set the current time of the last delta discovery watch request
    63  	SetLastDeltaWatchRequestTime(time.Time)
    64  
    65  	// SetDeltaResponseWatch will set the provided delta response watch to the associate watch ID
    66  	SetDeltaResponseWatch(int64, DeltaResponseWatch)
    67  }
    68  
    69  type statusInfo struct {
    70  	// node is the constant Envoy node metadata.
    71  	node *core.Node
    72  
    73  	// watches are indexed channels for the response watches and the original requests.
    74  	watches map[int64]ResponseWatch
    75  
    76  	// deltaWatches are indexed channels for the delta response watches and the original requests
    77  	deltaWatches map[int64]DeltaResponseWatch
    78  
    79  	// the timestamp of the last watch request
    80  	lastWatchRequestTime time.Time
    81  
    82  	// the timestamp of the last delta watch request
    83  	lastDeltaWatchRequestTime time.Time
    84  
    85  	// mutex to protect the status fields.
    86  	// should not acquire mutex of the parent cache after acquiring this mutex.
    87  	mu sync.RWMutex
    88  }
    89  
    90  // ResponseWatch is a watch record keeping both the request and an open channel for the response.
    91  type ResponseWatch struct {
    92  	// Request is the original request for the watch.
    93  	Request *Request
    94  
    95  	// Response is the channel to push responses to.
    96  	Response chan Response
    97  }
    98  
    99  // DeltaResponseWatch is a watch record keeping both the delta request and an open channel for the delta response.
   100  type DeltaResponseWatch struct {
   101  	// Request is the most recent delta request for the watch
   102  	Request *DeltaRequest
   103  
   104  	// Response is the channel to push the delta responses to
   105  	Response chan DeltaResponse
   106  
   107  	// VersionMap for the stream
   108  	StreamState stream.StreamState
   109  }
   110  
   111  // newStatusInfo initializes a status info data structure.
   112  func newStatusInfo(node *core.Node) *statusInfo {
   113  	out := statusInfo{
   114  		node:         node,
   115  		watches:      make(map[int64]ResponseWatch),
   116  		deltaWatches: make(map[int64]DeltaResponseWatch),
   117  	}
   118  	return &out
   119  }
   120  
   121  func (info *statusInfo) GetNode() *core.Node {
   122  	info.mu.RLock()
   123  	defer info.mu.RUnlock()
   124  	return info.node
   125  }
   126  
   127  func (info *statusInfo) GetNumWatches() int {
   128  	info.mu.RLock()
   129  	defer info.mu.RUnlock()
   130  	return len(info.watches)
   131  }
   132  
   133  func (info *statusInfo) GetNumDeltaWatches() int {
   134  	info.mu.RLock()
   135  	defer info.mu.RUnlock()
   136  	return len(info.deltaWatches)
   137  }
   138  
   139  func (info *statusInfo) GetLastWatchRequestTime() time.Time {
   140  	info.mu.RLock()
   141  	defer info.mu.RUnlock()
   142  	return info.lastWatchRequestTime
   143  }
   144  
   145  func (info *statusInfo) GetLastDeltaWatchRequestTime() time.Time {
   146  	info.mu.RLock()
   147  	defer info.mu.RUnlock()
   148  	return info.lastDeltaWatchRequestTime
   149  }
   150  
   151  // GetDeltaStreamState will pull the stream state with the version map out of a specific watch
   152  func (info *statusInfo) GetDeltaStreamState(watchID int64) stream.StreamState {
   153  	info.mu.RLock()
   154  	defer info.mu.RUnlock()
   155  	return info.deltaWatches[watchID].StreamState
   156  }
   157  
   158  func (info *statusInfo) SetLastDeltaWatchRequestTime(t time.Time) {
   159  	info.mu.Lock()
   160  	defer info.mu.Unlock()
   161  	info.lastDeltaWatchRequestTime = t
   162  }
   163  
   164  func (info *statusInfo) SetDeltaResponseWatch(id int64, drw DeltaResponseWatch) {
   165  	info.mu.Lock()
   166  	defer info.mu.Unlock()
   167  	info.deltaWatches[id] = drw
   168  }