Skip to content

Commit

Permalink
Adds RouteDeletedEvent used in WeightCalculatorWebFilter
Browse files Browse the repository at this point in the history
Fixes gh-922
  • Loading branch information
spencergibb committed Mar 20, 2024
1 parent f0ad75a commit c79e9ad
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
import org.springframework.cloud.gateway.event.RefreshRoutesEvent;
import org.springframework.cloud.gateway.event.RouteDeletedEvent;
import org.springframework.cloud.gateway.filter.FilterDefinition;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.cloud.gateway.filter.factory.GatewayFilterFactory;
Expand Down Expand Up @@ -307,9 +308,10 @@ private boolean isAvailable(PredicateDefinition predicateDefinition) {

@DeleteMapping("/routes/{id}")
public Mono<ResponseEntity<Object>> delete(@PathVariable String id) {
return this.routeDefinitionWriter.delete(Mono.just(id))
.then(Mono.defer(() -> Mono.just(ResponseEntity.ok().build())))
.onErrorResume(t -> t instanceof NotFoundException, t -> Mono.just(ResponseEntity.notFound().build()));
return this.routeDefinitionWriter.delete(Mono.just(id)).then(Mono.defer(() -> {
publisher.publishEvent(new RouteDeletedEvent(this, id));
return Mono.just(ResponseEntity.ok().build());
})).onErrorResume(t -> t instanceof NotFoundException, t -> Mono.just(ResponseEntity.notFound().build()));
}

@GetMapping("/routes/{id}/combinedfilters")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2013-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.cloud.gateway.event;

import org.springframework.context.ApplicationEvent;

public class RouteDeletedEvent extends ApplicationEvent {

private final String routeId;

public RouteDeletedEvent(Object source, String routeId) {
super(source);
this.routeId = routeId;
}

public String getRouteId() {
return routeId;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.cloud.gateway.event.PredicateArgsEvent;
import org.springframework.cloud.gateway.event.RefreshRoutesEvent;
import org.springframework.cloud.gateway.event.RouteDeletedEvent;
import org.springframework.cloud.gateway.event.WeightDefinedEvent;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.support.ConfigurationService;
import org.springframework.cloud.gateway.support.WeightConfig;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.SmartApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.core.log.LogMessage;
import org.springframework.core.style.ToStringCreator;
import org.springframework.util.Assert;
import org.springframework.web.server.ServerWebExchange;
Expand Down Expand Up @@ -123,6 +125,8 @@ public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
return PredicateArgsEvent.class.isAssignableFrom(eventType) ||
// from java dsl
WeightDefinedEvent.class.isAssignableFrom(eventType) ||
// from actuator or custom call
RouteDeletedEvent.class.isAssignableFrom(eventType) ||
// force initialization
RefreshRoutesEvent.class.isAssignableFrom(eventType);
}
Expand All @@ -140,6 +144,9 @@ public void onApplicationEvent(ApplicationEvent event) {
else if (event instanceof WeightDefinedEvent) {
addWeightConfig(((WeightDefinedEvent) event).getWeightConfig());
}
else if (event instanceof RouteDeletedEvent) {
removeWeightConfig(((RouteDeletedEvent) event).getRouteId());
}
else if (event instanceof RefreshRoutesEvent && routeLocator != null) {
// forces initialization
if (routeLocatorInitialized.compareAndSet(false, true)) {
Expand Down Expand Up @@ -229,6 +236,16 @@ private boolean hasRelevantKey(Map<String, Object> args) {
groupWeights.put(group, config);
}

private void removeWeightConfig(String routeId) {
log.trace(LogMessage.format("Removing weight config for route %s", routeId));
groupWeights.forEach((group, weightConfig) -> {
if (weightConfig.normalizedWeights.containsKey(routeId)) {
weightConfig.normalizedWeights.remove(routeId);
weightConfig.weights.remove(routeId);
}
});
}

/* for testing */ Map<String, GroupWeightConfig> getGroupWeights() {
return groupWeights;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.junit.jupiter.api.Test;

import org.springframework.cloud.gateway.event.PredicateArgsEvent;
import org.springframework.cloud.gateway.event.RouteDeletedEvent;
import org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter.GroupWeightConfig;
import org.springframework.cloud.gateway.support.ConfigurationService;
import org.springframework.cloud.gateway.support.WeightConfig;
Expand Down Expand Up @@ -121,6 +122,12 @@ public void testChooseRouteWithRandom() {
filter.filter(exchange, filterChain);
weights = WeightCalculatorWebFilter.getWeights(exchange);
assertThat(weights).containsEntry("groupa", "route3");

filter.onApplicationEvent(new RouteDeletedEvent(this, "route3"));
assertThat(filter.getGroupWeights()).containsKey("groupa");
GroupWeightConfig groupa = filter.getGroupWeights().get("groupa");
assertThat(groupa.normalizedWeights).doesNotContainKey("route3");
assertThat(groupa.weights).doesNotContainKey("route3");
}

@Test
Expand Down

0 comments on commit c79e9ad

Please sign in to comment.