Skip to content

Commit

Permalink
fix: reduce deckgen calls from proxy cache server (#1557)
Browse files Browse the repository at this point in the history
  • Loading branch information
shaneutt committed Jul 22, 2021
1 parent dcabd38 commit 9813575
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
8 changes: 8 additions & 0 deletions railgun/internal/proxy/clientgo_cached_proxy_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ func (p *clientgoCachedProxyResolver) ObjectExists(obj client.Object) (bool, err
// While processing objects the cacheServer will (synchronously) convert the objects to kong DSL and
// submit POST updates to the Kong Admin API with the new configuration.
func (p *clientgoCachedProxyResolver) startCacheServer() {
backendNeedsSync := false
p.logger.Info("the proxy cache server has been started")
for {
select {
Expand All @@ -216,18 +217,25 @@ func (p *clientgoCachedProxyResolver) startCacheServer() {
p.logger.Error(err, "object could not be updated in the cache and will be discarded")
break
}
backendNeedsSync = true
case cobj := <-p.del:
if err := p.cacheDelete(cobj); err != nil {
p.logger.Error(err, "object could not be deleted from the cache and will be discarded")
break
}
backendNeedsSync = true
case <-p.syncTicker.C:
if !p.enableReverseSync && !backendNeedsSync {
break
}

updateConfigSHA, err := p.kongUpdater(p.ctx, p.lastConfigSHA, p.cache, p.ingressClassName, p.deprecatedLogger, p.kongConfig, p.enableReverseSync)
if err != nil {
p.logger.Error(err, "could not update kong admin")
break
}
p.lastConfigSHA = updateConfigSHA
backendNeedsSync = false
case <-p.ctx.Done():
p.logger.Info("the proxy cache server's context is done, shutting down")
if err := p.ctx.Err(); err != nil {
Expand Down
23 changes: 21 additions & 2 deletions railgun/internal/proxy/clientgo_cached_proxy_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,27 @@ func TestCaching(t *testing.T) {
t.Log("flushing the cache state to kong admin api")
proxy.syncTicker.Reset(time.Millisecond * 50)

t.Logf("waiting for kong admin api updates to synchronize")
assert.Eventually(t, func() bool { return fakeKongAdminUpdateCount() == len(testObjects) }, time.Second*5, time.Millisecond*50)
t.Logf("ensuring that only a single update to the backend was performed, but that all cache objects are accounted for")
assert.Eventually(t, func() bool {
return fakeKongAdminUpdateCount() == 1 && len(proxy.cache.IngressV1.List()) == len(testObjects)
}, time.Second*5, time.Millisecond*50)

t.Log("freezing updates to the cache again")
fakeKongAdminUpdateCount(0) // reset the test counter
proxy.syncTicker.Reset(time.Minute * 3)

t.Log("deleting all the objects from the cache")
for _, testObj := range testObjects {
require.NoError(t, proxy.DeleteObject(testObj))
}

t.Log("flushing the cache state to kong admin api again")
proxy.syncTicker.Reset(time.Millisecond * 50)

t.Logf("ensuring that only a single update to the backend was performed when the cache is settled and not receiving further updates. verifying that all cache objects were removed")
assert.Eventually(t, func() bool {
return fakeKongAdminUpdateCount() == 1 && len(proxy.cache.IngressV1.List()) == 0
}, time.Second*5, time.Millisecond*50)
}

func TestProxyTimeout(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions railgun/internal/proxy/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ func fakeKongAdminUpdateCount(newcounts ...int) int {
if len(newcounts) < 1 {
return updateCount
}
if len(newcounts) == 1 && newcounts[0] == 0 {
updateCount = 0
return 0
}
for _, count := range newcounts {
updateCount = updateCount + count
}
Expand Down

0 comments on commit 9813575

Please sign in to comment.