r/cpp • • Aug 07 '24

Is there a retry / exponential backoff library for cpp?

0 Upvotes

15 comments sorted by

20

u/HKei Aug 07 '24

Retry/exponential backoff is a technique, not something you put into a library, and a correct implementation is like 5 lines of code. You can of course tack on however much you like but the basic idea is not worth putting into some fit-all library.

12

u/SuperV1234 https://romeo.training | C++ Mentoring & Consulting Aug 07 '24

and a correct implementation is like 5 lines of code

Can you share your correct 5LOC implementation?

4

u/blipman17 Aug 07 '24

Have you heared of the turtle retry library?

6

u/pjmlp Aug 07 '24

Libraries like Polly beg to differ.

11

u/HKei Aug 07 '24

I didn't say you can't put it into a library, what I tried to say was that you shouldn't. It's an excellent example of pathological code-reusitis.

8

u/SuperV1234 https://romeo.training | C++ Mentoring & Consulting Aug 07 '24

It's an excellent example of pathological code-reusitis.

Why do you think that? It's a completely reasonable use case for a library.

4

u/segv Aug 09 '24

Especially since the problem may seem simple, but it's actually pretty tricky to do it right.

3

u/SuperV1234 https://romeo.training | C++ Mentoring & Consulting Aug 09 '24

But /u/HKei is confident it can be done in 5LOC, yet he didn't deliver...

1

u/jcelerier ossia score Aug 11 '24

The ability to encode fairly abstract techniques as libraries is exactly what makes c++ so powerful.

2

u/AreaFifty1 Aug 07 '24

Uh no, you just create one yourself how hard can it be?

38

u/regaito Aug 07 '24

"how hard can it be"

  • famous last words

11

u/mcmcc #pragma once Aug 07 '24

Crowdstrike: We have 20 parameters in the config file. We'll just add another to make it 21. How hard can it be?

8

u/-Ros-VR- Aug 07 '24 edited Aug 07 '24

Funny enough it's at least a little more complicated than what you'd think. At least, depending on your use case.

For example, if you're writing retry for network calls which hit a server, you probably want to add random amounts of jitter to the retry schedule. That way, if your server is getting overloaded and failing requests, those clients don't all retry storm the server, hitting it again and again at exactly the same times, on the same exponential retry schedule, making it even harder for the server to recover.

You might even want to make various parameters like retry count and jitter amounts remotely configurable so you in an emergency you can at least remotely let up pressure on your server without needing to slowly deploy a code change to your clients.

Depends a lot on your use case though. I was responsible for writing an SDK which many millions of clients used to make many millions of calls to our servers. I definitely know it's at least a little harder to get this stuff right than what you might imagine.

1

u/KingAggressive1498 Aug 08 '24 edited Aug 08 '24

in what context are you asking about?

for spinlocks or adaptive mutexes, this is usually hard-coded inside the implementation for good reason.

For something like reliability protocols over UDP I can imagine a customizable backoff strategy for congestion control would be useful, but if we look at QUIC as a common target the behavior is actually mandated in the spec to double timeout until it is reset after acknowledgement so it isn't a reasonable feature for an implementation of QUIC specifically anyway.