@@ -58,7 +58,7 @@ constructor(
5858
5959 override fun requests (route : Route , user : User , max : Int ): Sequence <RestRequest > {
6060 return if (user.ready()) {
61- return generateRequests(route, user)
61+ return generateRequests(route, user). takeWhile { ! shouldBackoff }
6262 } else {
6363 emptySequence()
6464 }
@@ -75,7 +75,11 @@ constructor(
7575 offset.offset.coerceAtLeast(startDate)
7676 }
7777 val endDate = if (user.endDate >= Instant .now()) Instant .now() else user.endDate
78- if (endDate <= startOffset) return emptySequence()
78+ if (Duration .between(startOffset, endDate).toDays() <= ONE_DAY ) {
79+ logger.info(" Interval between dates is too short. Backing off.." )
80+ userNextRequest[user.versionedId] = Instant .now().plus(USER_BACK_OFF_TIME )
81+ return emptySequence()
82+ }
7983 val endTime = (startOffset + defaultQueryRange).coerceAtMost(endDate)
8084 return route.generateRequests(user, startOffset, endTime, USER_MAX_REQUESTS )
8185 }
@@ -105,8 +109,16 @@ constructor(
105109 ouraOffsetManager.updateOffsets(
106110 request.route,
107111 request.user,
108- Instant .ofEpochSecond(offset),
112+ Instant .ofEpochSecond(offset).plus( Duration .ofDays( 1 )) ,
109113 )
114+ } else {
115+ if (request.startDate.plus(TIME_AFTER_REQUEST ).isBefore(Instant .now())) {
116+ ouraOffsetManager.updateOffsets(
117+ request.route,
118+ request.user,
119+ request.endDate,
120+ )
121+ }
110122 }
111123 return records
112124 }
@@ -144,6 +156,7 @@ constructor(
144156 }
145157 400 -> {
146158 logger.warn(" Client exception.." )
159+ nextRequestTime = Instant .now() + BACK_OFF_TIME
147160 OuraClientException (
148161 " Client unsupported or unauthorized.." ,
149162 IOException (" Invalid client" ),
@@ -179,7 +192,9 @@ constructor(
179192
180193 companion object {
181194 private val logger = LoggerFactory .getLogger(OuraRequestGenerator ::class .java)
182- private val BACK_OFF_TIME = Duration .ofMinutes(5L )
195+ private val BACK_OFF_TIME = Duration .ofMinutes(10L )
196+ private val ONE_DAY = 1L
197+ private val TIME_AFTER_REQUEST = Duration .ofMinutes(1 )
183198 private val USER_BACK_OFF_TIME = Duration .ofDays(1L )
184199 private val USER_MAX_REQUESTS = 20
185200 val JSON_FACTORY = JsonFactory ()
0 commit comments