1 // Code generated by create_version. DO NOT EDIT. 2 // Copyright 2018 Envoyproxy Authors 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 package cache 17 18 import ( 19 "sync" 20 "time" 21 22 core "github.com/datawire/ambassador/v2/pkg/api/envoy/config/core/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 // GetLastWatchRequestTime returns the timestamp of the last discovery watch request. 54 GetLastWatchRequestTime() time.Time 55 } 56 57 type statusInfo struct { 58 // node is the constant Envoy node metadata. 59 node *core.Node 60 61 // watches are indexed channels for the response watches and the original requests. 62 watches map[int64]ResponseWatch 63 64 // the timestamp of the last watch request 65 lastWatchRequestTime time.Time 66 67 // mutex to protect the status fields. 68 // should not acquire mutex of the parent cache after acquiring this mutex. 69 mu sync.RWMutex 70 } 71 72 // ResponseWatch is a watch record keeping both the request and an open channel for the response. 73 type ResponseWatch struct { 74 // Request is the original request for the watch. 75 Request *Request 76 77 // Response is the channel to push responses to. 78 Response chan Response 79 } 80 81 // newStatusInfo initializes a status info data structure. 82 func newStatusInfo(node *core.Node) *statusInfo { 83 out := statusInfo{ 84 node: node, 85 watches: make(map[int64]ResponseWatch), 86 } 87 return &out 88 } 89 90 func (info *statusInfo) GetNode() *core.Node { 91 info.mu.RLock() 92 defer info.mu.RUnlock() 93 return info.node 94 } 95 96 func (info *statusInfo) GetNumWatches() int { 97 info.mu.RLock() 98 defer info.mu.RUnlock() 99 return len(info.watches) 100 } 101 102 func (info *statusInfo) GetLastWatchRequestTime() time.Time { 103 info.mu.RLock() 104 defer info.mu.RUnlock() 105 return info.lastWatchRequestTime 106 } 107