7
7
8
8
"github.com/solo-io/go-utils/contextutils"
9
9
"k8s.io/apimachinery/pkg/types"
10
- "k8s.io/utils/lru"
11
10
12
11
"github.com/solo-io/gloo/projects/gateway2/proxy_syncer"
13
12
gwplugins "github.com/solo-io/gloo/projects/gateway2/translator/plugins"
@@ -40,22 +39,22 @@ type GatewayStatusSyncer interface {
40
39
type statusSyncerFactory struct {
41
40
// maps a proxy sync action to the plugin registry that produced it
42
41
// sync iteration -> plugin registry
43
- registryPerSyncCache * lru. Cache // map[int]*registry.PluginRegistry
42
+ registryPerSync map [int ]* registry.PluginRegistry
44
43
// maps a proxy to the sync iteration that produced it
45
44
// only the latest sync iteration is stored and used to apply status plugins
46
45
resyncsPerProxy map [types.NamespacedName ]int
47
46
// 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
50
50
}
51
51
52
52
func NewStatusSyncerFactory () GatewayStatusSyncer {
53
53
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 {},
59
58
}
60
59
}
61
60
@@ -71,20 +70,21 @@ func (f *statusSyncerFactory) QueueStatusForProxies(
71
70
72
71
contextutils .LoggerFrom (ctx ).Debugf ("queueing %v proxies for sync %d" , len (proxiesToQueue ), totalSyncCount )
73
72
74
- resyncsPerIteration := make ([]types.NamespacedName , len (proxiesToQueue ))
75
73
// queue each proxy for a given sync iteration
76
74
for _ , proxy := range proxiesToQueue {
77
75
proxyKey := getProxyNameNamespace (proxy )
78
76
// overwrite the sync count for the proxy with the most recent sync count
79
77
f .resyncsPerProxy [proxyKey ] = totalSyncCount
80
78
81
79
// 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 )
83
81
}
84
- f .resyncsPerIterationCache .Add (totalSyncCount , resyncsPerIteration )
85
82
86
83
// 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 )
88
88
}
89
89
90
90
// 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
113
113
continue
114
114
}
115
115
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
124
120
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
126
129
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 )
131
133
}
132
134
}
133
- f .resyncsPerIterationCache .Add (proxySyncCount , updatedList )
134
135
135
136
proxiesToReport [proxySyncCount ] = append (proxiesToReport [proxySyncCount ], proxyWithReport )
136
137
// remove the proxy from the queue
137
138
delete (f .resyncsPerProxy , proxyKey )
138
139
}
139
140
140
141
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 )
143
144
}
144
145
145
146
// 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 )
150
149
}
151
150
}
152
151
}
0 commit comments