--- dao/DaoAdresseService/src/dk/daoas/daoadresseservice/GoogleStreetnameHelper.java 2015/02/28 17:08:59 2411 +++ dao/DaoAdresseService/src/main/java/dk/daoas/daoadresseservice/GoogleStreetnameHelper.java 2015/06/07 18:09:16 2567 @@ -10,6 +10,8 @@ import com.google.code.geocoder.model.GeocoderResult; import com.google.code.geocoder.model.GeocoderStatus; +import com.google.common.util.concurrent.RateLimiter; + import dk.daoas.daoadresseservice.admin.ServiceConfig; import dk.daoas.daoadresseservice.beans.SearchRequest; import dk.daoas.daoadresseservice.beans.SearchResult; @@ -20,8 +22,12 @@ public class GoogleStreetnameHelper implements StreetnameHelper { ServiceConfig conf; + RateLimiter limiter; + public GoogleStreetnameHelper(ServiceConfig conf) { this.conf = conf; + + limiter = RateLimiter.create( 10.0 ); //max 10 req /sec } @Override @@ -29,10 +35,11 @@ if (conf.useGoogle == false) return null; + result.google = true; try { - GoogleInvocation wrapper = new GoogleInvocation( conf, request.postnr, request.vejnavn ); + GoogleInvocation wrapper = new GoogleInvocation( limiter, conf, request.postnr, request.vejnavn ); CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("google"); @@ -50,8 +57,10 @@ ServiceConfig conf; int postnr; String vejnavn; + RateLimiter limiter; - public GoogleInvocation(ServiceConfig conf, int postnr, String vejnavn) { + public GoogleInvocation(RateLimiter limiter, ServiceConfig conf, int postnr, String vejnavn) { + this.limiter = limiter; this.conf = conf; this.postnr= postnr; this.vejnavn = vejnavn; @@ -60,6 +69,8 @@ @Override public String proceed() throws Exception { + + limiter.acquire(); final Geocoder geocoder; if ( conf.googleApiKey != null ) {