Conversation
…ait til next 15 minute (0,15,30 or 45) to conform the strava rate limits : https://developers.strava.com/docs/
…ity and fail over purpose
|
@orium did you see my pull-request ? |
|
Sorry @b3b00, I didn't yet :/ I will take a look later today. |
orium
left a comment
There was a problem hiding this comment.
Thanks for the PR. I have some suggestions on how to improve things. If you need some extra mentoring let me know, I'm happy to help :)
| import scala.util.control.NonFatal | ||
| import java.util._ | ||
|
|
||
| class StravaRateLimiter(isRateLimitExceeded : Throwable => Boolean) extends RateLimiter(isRateLimitExceeded) { |
There was a problem hiding this comment.
We don't need to replicate so much of this logic.
I think we can have some sort of parameterizeble backup strategy. For instance, we can have something an ADT like:
sealed trait BackoffStrategy
object BackoffStrategy {
case class ExponentialBackoff(startBackoff: Duration, exponent: Double = 2.0) extends BackoffStrategy
case class ConstantBackoff(backoff: Duration) extends BackoffStrategy
}This way we wouldn't need a StravaRateLimiter and RateLimiter would receive a BackoffStrategy. E.g.
object RateLimiter {
...
def byException[T <: Throwable: ClassTag]: RateLimiter =
new RateLimiter(exceptionCausedBy[T], BackoffStrategy.ExponentialBackoff(startBackoff = 1.minute, exponent = 2.0))
def byExceptionForStrava[T <: Throwable: ClassTag]: RateLimiter =
new RateLimiter(exceptionCausedBy[T], BackoffStrategy.ConstantBackoff(startBackoff = 15.minutes))
}
We could still have the nice message saying that we are waiting x time, and will resume at y. Also, to print that message you do some math and manual formatting. There are methods that can help you with that (e.g. take a look at Instant (where you can do math with durations) and Duration).
There was a problem hiding this comment.
the constantbackoff is not optimal as you will always wait for 15 minutes even thiugh you may only need 1 minute if the exception occurs let say at 03:14 and rate limit will be reset at 3:15 as strava API states. So we must have some specific logic here. Strava rate limits are very specific I have never seen such conditions in other API.
so the constantbackoff must be renamed as the baxkoff is not constant at all.
I will try to provide a refactoring using a strategy pattern approach.
I agree for the flrmatting part.
There was a problem hiding this comment.
Yeah, it can be a BackoffStrategy.StravaBackoff or something like that :)
| val datetime = LocalDateTime.parse(activity.start_date, DateTimeFormatter.ISO_DATE_TIME) | ||
|
|
||
| Some(Run(activity.id, datetime, times, distances)) | ||
| val run = Run(activity.id, datetime, times, distances) |
There was a problem hiding this comment.
This is not the best place to do this. The function that populates the cache is Strava.populateRunCache. I think easiest approach might be to just catch exceptions when fetching runs, logging them, and move on.
The fetch is done here:
We can put a try { ... } catch { .. } and log that we failed to fetch a run. To report the error add another parameter to the function like the reportFetch (a reportFetchError I guess). This will it abstracted away from the output.
There was a problem hiding this comment.
you are right. my proposal was a first draft.
No description provided.