Skip to content

Commit

Permalink
Fix Consul key when using URL_PATTERN (#466)
Browse files Browse the repository at this point in the history
* Related to bug no #465 as ConsulRateLimiter not working
as expected when limiting using URL_PATTERN , suggested fix for this is to change Slashes to Underscore

* Matching PR Conventions

Co-authored-by: Mohamed Fawzy <[email protected]>
Co-authored-by: Marcos Barbero <[email protected]>
  • Loading branch information
3 people authored Oct 13, 2021
1 parent 5f5beaf commit 97d7a2f
Showing 1 changed file with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@

import java.io.IOException;

import static org.apache.commons.lang3.StringUtils.isNotBlank;
import static org.springframework.util.StringUtils.hasText;

/**
* Consul rate limiter configuration.
*
* @author Liel Chayoun
* @author Marcos Barbero
* @author Mohamed Fawzy
* @since 2017-08-15
*/
public class ConsulRateLimiter extends AbstractRateLimiter {
Expand All @@ -50,9 +52,9 @@ public ConsulRateLimiter(RateLimiterErrorHandler rateLimiterErrorHandler,
}

@Override
protected Rate getRate(String key) {
protected Rate getRate(final String key) {
Rate rate = null;
GetValue value = this.consulClient.getKVValue(key).getValue();
GetValue value = this.consulClient.getKVValue(buildValidConsulKey(key)).getValue();
if (value != null && value.getDecodedValue() != null) {
try {
rate = this.objectMapper.readValue(value.getDecodedValue(), Rate.class);
Expand All @@ -73,8 +75,16 @@ protected void saveRate(Rate rate) {
}

if (hasText(value)) {
this.consulClient.setKVValue(rate.getKey(), value);
this.consulClient.setKVValue(buildValidConsulKey(rate.getKey()), value);
}
}

// Slash will corrupt Consul Call , to be changed to _
private String buildValidConsulKey(String key){
if(isNotBlank(key)){
return key.replaceAll("/", "_");
}
return key;
}

}

0 comments on commit 97d7a2f

Please sign in to comment.