diff --git a/gateway/gateway_test.go b/gateway/gateway_test.go index 9df876c3afc..f76b94ca95b 100644 --- a/gateway/gateway_test.go +++ b/gateway/gateway_test.go @@ -629,6 +629,51 @@ func TestManagementNodeRedisEvents(t *testing.T) { }) } +func TestDistributedRateLimiterDisabledRedisEvents(t *testing.T) { + ts := StartTest(nil) + t.Cleanup(ts.Close) + + drlNotificationCommand := &testMessageAdapter{ + Msg: `{"Command": "NoticeGatewayDRLNotification"}`, + } + + shouldNotHandleNotification := func(NotificationCommand) { + assert.Fail(t, "should skip redis event") + } + + globalConf := ts.Gw.GetConfig() + + t.Run("enabled Sentinel Rate Limiter - should skip DRL updates", func(*testing.T) { + globalConf.EnableSentinelRateLimiter = true + ts.Gw.SetConfig(globalConf) + + ts.Gw.handleRedisEvent(drlNotificationCommand, shouldNotHandleNotification, nil) + + globalConf.EnableSentinelRateLimiter = false + ts.Gw.SetConfig(globalConf) + }) + + t.Run("enabled Redis Rolling Limiter - should skip DRL updates", func(*testing.T) { + globalConf.EnableRedisRollingLimiter = true + ts.Gw.SetConfig(globalConf) + + ts.Gw.handleRedisEvent(drlNotificationCommand, shouldNotHandleNotification, nil) + + globalConf.EnableRedisRollingLimiter = false + ts.Gw.SetConfig(globalConf) + }) + + t.Run("enabled Fixed Window Rate Limiter - should skip DRL updates", func(*testing.T) { + globalConf.EnableFixedWindowRateLimiter = true + ts.Gw.SetConfig(globalConf) + + ts.Gw.handleRedisEvent(drlNotificationCommand, shouldNotHandleNotification, nil) + + globalConf.EnableFixedWindowRateLimiter = false + ts.Gw.SetConfig(globalConf) + }) +} + func TestListenPathTykPrefix(t *testing.T) { ts := StartTest(nil) t.Cleanup(ts.Close) diff --git a/gateway/redis_signals.go b/gateway/redis_signals.go index 361fec64320..9ed7894f3eb 100644 --- a/gateway/redis_signals.go +++ b/gateway/redis_signals.go @@ -140,10 +140,8 @@ func (gw *Gateway) handleRedisEvent(v interface{}, handled func(NotificationComm case NoticeDashboardConfigRequest: gw.handleSendMiniConfig(notif.Payload) case NoticeGatewayDRLNotification: - if gw.GetConfig().ManagementNode { - // DRL is not initialized, going through would - // be mostly harmless but would flood the log - // with warnings since DRLManager.Ready == false + if gw.isDRLDisabled() { + // DRL is disabled - other Rate Limiter is being used or this is a Management Node. return } gw.onServerStatusReceivedHandler(notif.Payload) diff --git a/gateway/server.go b/gateway/server.go index fd7551f3c4a..293a445d8fd 100644 --- a/gateway/server.go +++ b/gateway/server.go @@ -2052,15 +2052,13 @@ func handleDashboardRegistration(gw *Gateway) { func (gw *Gateway) startDRL() { gwConfig := gw.GetConfig() - disabled := gwConfig.ManagementNode || gwConfig.EnableSentinelRateLimiter || gwConfig.EnableRedisRollingLimiter || gwConfig.EnableFixedWindowRateLimiter - gw.drlOnce.Do(func() { drlManager := &drl.DRL{} gw.SessionLimiter = NewSessionLimiter(gw.ctx, &gwConfig, drlManager, &gwConfig.ExternalServices) gw.DRLManager = drlManager - if disabled { + if gw.isDRLDisabled() { return } @@ -2077,6 +2075,12 @@ func (gw *Gateway) startDRL() { }) } +func (gw *Gateway) isDRLDisabled() bool { + gwConfig := gw.GetConfig() + + return gwConfig.ManagementNode || gwConfig.EnableSentinelRateLimiter || gwConfig.EnableRedisRollingLimiter || gwConfig.EnableFixedWindowRateLimiter +} + func (gw *Gateway) setupPortsWhitelist() { // setup listen and control ports as whitelisted gwConf := gw.GetConfig()