For similar reasons, we similarly can't keep making sleep-sort faster by scaling the sleeps down.
For similar reasons, we similarly can't keep making sleep-sort faster by scaling the sleeps down.
I've been endlessly frustrated by how prevalent "just add a <x> second sleep" is in the Kubernetes world.
Examples:
If you're using IRSA to provide pods an AWS IAM Role, it can take some time on first-run for the role to become usable. So your app has to just wait until it can successfully talk to AWS.
If you're using the AWS Elastic Load Balancer controller, you need to add sleeps during pre-stop for a pod. The controller has to see the shutdown event, then start target deregistration on the ELB Target Group. That's a minimum of 30 seconds, during which you can be getting new connections.
Service meshes on Kubernetes add some fun on both startup AND shutdown.
Linkerd, for instance, will only inject it's linkerd-proxy container into your pod after startup. So it's entirely possible for your main application container to start, begin talking to the world and then a moment later all your connections get reset when Linkerd-proxy takes over the pod's networking configuration.
You can fix this by injecting into all of your containers a linkerd-await utility that replaces the container entrypoint. It will wait until linkerd starts before starting your regular entrypoint. It's basically a more advanced version of sleep.
Shutdown is just as fun - linkerd-proxy will immediately terminate, dropping all network connectivity for all the containers in your pod. There's a fix for this, too, which involves the above linkerd-await and another option.
Actually it's a generalisation of what OP is doing here. Move the listener _out_ of the program and you never have to know if a service is up or not. Connecting to a service just blocks until it's up.
Is then the waiting for the availability/existence of anything a "more advanced version of sleep"?
Is a readiness probe a sleep?
I don't follow at all.
The problem is often relying too much on cold-starts; these polls amortize well in a real system, where you can afford (and want to afford!)
Agreed. Just note that it can sometimes (often?) be a good idea to add a small stochastic element to the sleep time, and/or do something similar to the exponential back-off[1] approach that Ethernet (among other things) uses.
If anything, people underestimating the ramp up of exponential back off(or not capping it) has left me waiting much longer than necessary
Technically, we can, at least to a certain limit - as long as the total time needed to schedule the sleeps is less than the shortest sleep duration, sleepsort will still work. Though we're just offloading the sorting to the scheduler, which has to implement the sorting algorithm anyway...
The biggest headache we had was to figure out when everything was up. There simply was no way to detect this programmatically, so the only way would have been to alter the code to give some kind of signal on the console like "System is up." similar to a linux box showing the login prompt. So I resorted to wait loops too.
We ended up canceling the project.
``` interval = interval*1.6 # fiboroughly ```
The alternatives have a ton more complexity for not much benefit.