If the initial get_or_create fails in shorten_url, we enter
an infinite loop of create() attempts. If the initial failure
was due to competition with another instance of trying to
shorten the same URL, then the subsequent create() call will
never succeed and the code will spin until killed.
This commit changes the retries to use get_or_create as well,
so that the retry will not try to create a short url that
already exists.
It might also be worth changing while True to something finite, like for _ in range(20), but that is left for another day.
I think it's fine. Eventually I would say some of the code could be extracted in a function to avoid having completely duplicate code twice in that function.
If the initial get_or_create fails in shorten_url, we enter an infinite loop of create() attempts. If the initial failure was due to competition with another instance of trying to shorten the same URL, then the subsequent create() call will never succeed and the code will spin until killed.
This commit changes the retries to use get_or_create as well, so that the retry will not try to create a short url that already exists.
It might also be worth changing
while True
to something finite, likefor _ in range(20)
, but that is left for another day.