altosui: rate limit map downloads
authorAnthony Towns <aj@erisian.com.au>
Mon, 20 Jan 2014 09:38:21 +0000 (19:38 +1000)
committerAnthony Towns <aj@erisian.com.au>
Mon, 20 Jan 2014 09:38:21 +0000 (19:38 +1000)
altosui/AltosSiteMapCache.java
altosui/AltosSiteMapPreload.java

index 40c8ff6bf0dc078beff6f8b5016f8910ffe2f4c4..03dc3cf5d67ebc8a3170d863d8557c75258452c1 100644 (file)
@@ -25,8 +25,13 @@ import java.net.URL;
 import java.net.URLConnection;
 
 public class AltosSiteMapCache extends JLabel {
+       static final long google_maps_ratelimit_ms = 1200;
+       // Google limits static map queries to 50 per minute per IP, so
+       // each query should take at least 1.2 seconds.
+
        public static boolean fetchMap(File file, String url) {
                URL u;
+               long startTime = System.nanoTime();
 
                try {
                        u = new URL(url);
@@ -70,6 +75,16 @@ public class AltosSiteMapCache extends JLabel {
                        }
                        return false;
                }
+
+               long duration_ms = (System.nanoTime() - startTime) / 1000000;
+               if (duration_ms < google_maps_ratelimit_ms) {
+                       try {
+                               Thread.sleep(google_maps_ratelimit_ms - duration_ms);
+                       } catch (InterruptedException e) {
+                               Thread.currentThread().interrupt();
+                       }
+               }
+
                return true;
        }
 
index 7112fed680b36c83c2a2894d02a4a329631e9cd8..66399557e6813289da168fd511bcc711a84e442f 100644 (file)
@@ -213,7 +213,7 @@ public class AltosSiteMapPreload extends AltosUIDialog implements ActionListener
        AltosMapPos     lat;
        AltosMapPos     lon;
 
-       final static int        radius = 3;
+       final static int        radius = 5;
        final static int        width = (radius * 2 + 1);
        final static int        height = (radius * 2 + 1);