77
88 "github.com/solo-io/go-utils/contextutils"
99 "k8s.io/apimachinery/pkg/types"
10- "k8s.io/utils/lru"
1110
1211 "github.com/solo-io/gloo/projects/gateway2/proxy_syncer"
1312 gwplugins "github.com/solo-io/gloo/projects/gateway2/translator/plugins"
@@ -40,22 +39,22 @@ type GatewayStatusSyncer interface {
4039type statusSyncerFactory struct {
4140 // maps a proxy sync action to the plugin registry that produced it
4241 // sync iteration -> plugin registry
43- registryPerSyncCache * lru. Cache // map[int]*registry.PluginRegistry
42+ registryPerSync map [int ]* registry.PluginRegistry
4443 // maps a proxy to the sync iteration that produced it
4544 // only the latest sync iteration is stored and used to apply status plugins
4645 resyncsPerProxy map [types.NamespacedName ]int
4746 // proxies left to sync
48- resyncsPerIterationCache * lru.Cache // map[int][]types.NamespacedName
49- lock * sync.Mutex
47+ resyncsPerIteration map [int ][]types.NamespacedName
48+
49+ lock * sync.Mutex
5050}
5151
5252func NewStatusSyncerFactory () GatewayStatusSyncer {
5353 return & statusSyncerFactory {
54- // Set a max value of 3 for n-2 iterations. Ideally we should only care about n-1 but playing it safe
55- registryPerSyncCache : lru .New (3 ),
56- resyncsPerIterationCache : lru .New (3 ),
57- resyncsPerProxy : make (map [types.NamespacedName ]int ),
58- lock : & sync.Mutex {},
54+ registryPerSync : make (map [int ]* registry.PluginRegistry ),
55+ resyncsPerProxy : make (map [types.NamespacedName ]int ),
56+ resyncsPerIteration : make (map [int ][]types.NamespacedName ),
57+ lock : & sync.Mutex {},
5958 }
6059}
6160
@@ -71,20 +70,21 @@ func (f *statusSyncerFactory) QueueStatusForProxies(
7170
7271 contextutils .LoggerFrom (ctx ).Debugf ("queueing %v proxies for sync %d" , len (proxiesToQueue ), totalSyncCount )
7372
74- resyncsPerIteration := make ([]types.NamespacedName , len (proxiesToQueue ))
7573 // queue each proxy for a given sync iteration
7674 for _ , proxy := range proxiesToQueue {
7775 proxyKey := getProxyNameNamespace (proxy )
7876 // overwrite the sync count for the proxy with the most recent sync count
7977 f .resyncsPerProxy [proxyKey ] = totalSyncCount
8078
8179 // keep track of proxies to check all proxies are handled in debugger
82- resyncsPerIteration = append (resyncsPerIteration , proxyKey )
80+ f . resyncsPerIteration [ totalSyncCount ] = append (f . resyncsPerIteration [ totalSyncCount ] , proxyKey )
8381 }
84- f .resyncsPerIterationCache .Add (totalSyncCount , resyncsPerIteration )
8582
8683 // the plugin registry that produced the proxies is the same for all proxies in a given sync
87- f .registryPerSyncCache .Add (totalSyncCount , pluginRegistry )
84+ f .registryPerSync [totalSyncCount ] = pluginRegistry
85+
86+ delete (f .resyncsPerIteration , totalSyncCount - 2 )
87+ delete (f .registryPerSync , totalSyncCount - 2 )
8888}
8989
9090// HandleProxyReports is a callback that applies status plugins to the proxies that have been queued
@@ -113,40 +113,39 @@ func (f *statusSyncerFactory) HandleProxyReports(ctx context.Context, proxiesWit
113113 continue
114114 }
115115
116- // If the proxy does not exist in the cache, or the cache is empty, the re-sync already happened, nothing to do
117- resyncsPerIterationIface , ok := f .resyncsPerIterationCache .Get (proxySyncCount )
118- if ! ok {
119- continue
120- }
121- resyncsPerIteration := resyncsPerIterationIface .([]types.NamespacedName )
122- if len (resyncsPerIteration ) == 0 {
123- f .resyncsPerIterationCache .Remove (proxySyncCount )
116+ if len (f .resyncsPerIteration [proxySyncCount ]) == 0 {
117+ // remove the key so the map does not indefinitely grow
118+ delete (f .resyncsPerIteration , proxySyncCount )
119+ // re-sync already happened, nothing to do
124120 continue
125- }
121+ } else {
122+ updatedList := make ([]types.NamespacedName , 0 )
123+ for _ , proxyNameNs := range f .resyncsPerIteration [proxySyncCount ] {
124+ if proxyNameNs != proxyKey {
125+ updatedList = append (updatedList , proxyNameNs )
126+ }
127+ }
128+ f .resyncsPerIteration [proxySyncCount ] = updatedList
126129
127- updatedList := make ([]types.NamespacedName , 0 )
128- for _ , proxyNameNs := range resyncsPerIteration {
129- if proxyNameNs != proxyKey {
130- updatedList = append (updatedList , proxyNameNs )
130+ if len (f .resyncsPerIteration [proxySyncCount ]) == 0 {
131+ // remove the key so the map does not indefinitely grow
132+ delete (f .resyncsPerIteration , proxySyncCount )
131133 }
132134 }
133- f .resyncsPerIterationCache .Add (proxySyncCount , updatedList )
134135
135136 proxiesToReport [proxySyncCount ] = append (proxiesToReport [proxySyncCount ], proxyWithReport )
136137 // remove the proxy from the queue
137138 delete (f .resyncsPerProxy , proxyKey )
138139 }
139140
140141 for syncCount , proxies := range proxiesToReport {
141- if plugins , ok := f .registryPerSyncCache . Get ( syncCount ) ; ok {
142- newStatusSyncer (plugins .( * registry. PluginRegistry ) ).applyStatusPlugins (ctx , proxies )
142+ if plugins , ok := f .registryPerSync [ syncCount ] ; ok {
143+ newStatusSyncer (plugins ).applyStatusPlugins (ctx , proxies )
143144 }
144145
145146 // If there are no more proxies for the sync iteration, delete the sync count
146- resyncsPerIterationIface , _ := f .resyncsPerIterationCache .Get (syncCount )
147- resyncsPerIteration := resyncsPerIterationIface .([]types.NamespacedName )
148- if len (resyncsPerIteration ) == 0 {
149- f .registryPerSyncCache .Remove (syncCount )
147+ if len (f .resyncsPerIteration [syncCount ]) == 0 {
148+ delete (f .registryPerSync , syncCount )
150149 }
151150 }
152151}
0 commit comments