[gatekeeper] implement recommended throttling function

Change-Id: Ic9fff82d4dd43df590081d28efff4742052f272f
diff --git a/gatekeeper.cpp b/gatekeeper.cpp
index 5ea7f7c..998061d 100644
--- a/gatekeeper.cpp
+++ b/gatekeeper.cpp
@@ -18,6 +18,8 @@
 
 #include <endian.h>
 
+#define DAY_IN_MS (1000 * 60 * 60 * 24)
+
 namespace gatekeeper {
 
 void GateKeeper::Enroll(const EnrollRequest &request, EnrollResponse *response) {
@@ -242,6 +244,18 @@
     auth_token->reset(reinterpret_cast<uint8_t *>(token));
 }
 
+/*
+ * Calculates the timeout in milliseconds as a function of the failure
+ * counter 'x' as follows:
+ *
+ * [0. 5) -> 0
+ * 5 -> 30
+ * [6, 10) -> 0
+ * [11, 30) -> 30
+ * [30, 140) -> 30 * (2^((x - 30)/10))
+ * [140, inf) -> 1 day
+ *
+ */
 uint32_t GateKeeper::ComputeRetryTimeout(const failure_record_t *record) {
     static const int failure_timeout_ms = 30000;
     if (record->failure_counter == 0) return 0;
@@ -250,10 +264,13 @@
         if (record->failure_counter % 5 == 0) {
             return failure_timeout_ms;
         }
-    } else {
+    } else if (record->failure_counter < 30) {
         return failure_timeout_ms;
+    } else if (record->failure_counter < 140) {
+        return failure_timeout_ms << ((record->failure_counter - 30) / 10);
     }
-    return 0;
+
+    return DAY_IN_MS;
 }
 
 bool GateKeeper::ThrottleRequest(uint32_t uid, uint64_t timestamp,