Show HN: Is_ready – Wait for many services to become available – 0 Dependencies
https://github.com/Stavrospanakakis/is_readyBy stavepan at
10000truths | 0 comments | 2 weeks ago
You should instead rely on backpressure to signal readiness. A service accepts connections immediately, and only blocks the client when the service has to block on something else to procure a response. Systemd takes this approach with UNIX sockets - it creates all the UNIX sockets for each service, and then starts all the services in parallel. The socket buffers queue the messages so that clients can send messages to services that haven't yet started, and the OS scheduler will saturate the available CPU/IO so that the startup of all the services are maximally parallelized.
Obviously, this is not always feasible, especially in cases where you don't control the service(s) you depend on. But to the extent that you do, relying on backpressure instead of polling can markedly improve end-to-end latency.
matthews2 | 6 comments | 3 weeks ago
A neat project, though. I think that this is best solved in the application itself (e.g. your server starts but returns HTTP errors while the database is unavailable), but being able to retrofit this behaviour into any existing application seems useful. Feels like something very similar should be built into tools like docker-compose.
tommasoamici | 1 comment | 3 weeks ago
In docker compose you can use `depends_on` [0] to define dependencies between containers and by default it will wait until a dependent container is "ready".
But you can also use a more verbose syntax to wait until a container is "healthy", as defined by its own healthcheck.
services:
web:
depends_on:
db:
condition: service_healthy
db:
image: postgres
[0] https://docs.docker.com/compose/compose-file/05-services/#de...stavepan | 0 comments | 3 weeks ago
For example, in the case of PostgreSQL, there is already a tool called pg_isready [0] to do this inside a healthcheck as you described.
services: postgres-db: image: postgres healthcheck: test: ["CMD-SHELL", "pg_isready -U postgres"]
application:
image: image
depends_on:
postgres-db:
condition: service_healthy
However, this is not the case for other databases/services.[0] https://www.postgresql.org/docs/current/app-pg-isready.html
stavepan | 1 comment | 3 weeks ago
stavros | 0 comments | 3 weeks ago
pkulak | 1 comment | 3 weeks ago
blueflow | 1 comment | 3 weeks ago
pkulak | 0 comments | 2 weeks ago
thenonameguy | 3 comments | 3 weeks ago
See: https://f1bonacc1.github.io/process-compose/health/?h=port#r...
cholindo | 0 comments | 3 weeks ago
40183933 | 1 comment | 3 weeks ago
thenonameguy | 0 comments | 2 weeks ago
https://devenv.sh/ is one example that uses it to do just that.
stavepan | 0 comments | 3 weeks ago
SteveNuts | 0 comments | 3 weeks ago
felsokning | 1 comment | 3 weeks ago
Came here to make the same comment. 0 dependencies would mean/infer (at least, to me) a lack of any dependencies but that doesn't seem to be the case[1]. There are 30 matches for the word `dependencies`.
[1] - https://github.com/Stavrospanakakis/is_ready/blob/main/Cargo...
stavros | 1 comment | 3 weeks ago
felsokning | 1 comment | 3 weeks ago
The title claims "0 dependencies", not "0 runtime dependencies" nor "0 compile-time dependencies". In other words, the word dependencies - by itself - is encompassing both runtime and compile-time dependencies.
Think of reading the title at face-value - but as if someone else had written it, with no exposure to your project, its use-case, behaviour, etc. - before it was read.
stavros | 1 comment | 3 weeks ago
felsokning | 1 comment | 3 weeks ago
The "dependencies" point still stands, though, in that the term - generally - encompasses both.
stavepan | 0 comments | 3 weeks ago
fumplethumb | 2 comments | 3 weeks ago
I used wait_for_it.sh for the purpose described in the OP until I found healtchecks could be used instead.
[0] https://github.com/peter-evans/docker-compose-healthcheck
stavepan | 1 comment | 3 weeks ago
As this repository mentions, this is the example using PostgreSQL.
depends_on: postgres-database: condition: service_healthy
healthcheck: test: ["CMD-SHELL", "pg_isready"] interval: 10s timeout: 5s retries: 5
However, PostgreSQL has already a command for this called pg_isready.
How is this going to work for other cases such as MySQL?
hiatus | 0 comments | 3 weeks ago
You could do a query like SHOW DATABASES as a healthcheck for mysql.
rmbyrro | 1 comment | 3 weeks ago
drewbitt | 0 comments | 3 weeks ago
q3k | 2 comments | 3 weeks ago
no_circuit | 0 comments | 3 weeks ago
However it is not realistic to make sure all library dependencies used behave properly for more lambda-like jobs where a library/app like "wait for" is very useful since it reduces the backoff parameters that ops needs to know about, and reduces unnecessary errors in logs.
Take GCP Cloud SQL Auth Proxy [1] for example which simplifies the details of securely connecting to a database without exposing long-term credentials to app code. Typically for the audience that uses the proxy, it is run as a sidecar container in Kubernetes Pod and has a non-deterministic startup time in the range of a few seconds. There is no "depends_on" facility like in Docker (at least when I had to use it). Your batch job could potentially always fail if the proxy cannot establish a connection to the database before your minimal database migration container starts up. Building the backoff inside the migration app makes it more opaque for ops teams. Having this "wait for" run in a startup script before the main app allow ops to tune its parameters, such as through environment variables, so that the job behaves appropriately under health probes [2] for higher level backoffs. All of which can be configured without recompiling the main app code, or embedding the functionality in the first place.
[1] https://cloud.google.com/sql/docs/postgres/connect-auth-prox...
[2] https://kubernetes.io/docs/tasks/configure-pod-container/con...
stavepan | 2 comments | 3 weeks ago
liampulles | 0 comments | 3 weeks ago
ivan_gammel | 1 comment | 3 weeks ago
stavepan | 0 comments | 3 weeks ago
version: '3' services: mysql: image: mysql:8.0
app:
build: .
command: is_ready --timeout 10 --addr mysql:3306 -- <run migrations command>
For cases like this, returning 503 every time the database is not ready, is not very convenient.hhthrowaway1230 | 1 comment | 3 weeks ago
k3vinw | 0 comments | 3 weeks ago
cholindo | 1 comment | 3 weeks ago
stavepan | 1 comment | 3 weeks ago
no_circuit | 0 comments | 3 weeks ago
There could be some useful functionality that could be found in startup and health probe topics [1] in Kubernetes for example. But at least in that world, it may be easier, and more transparent, to plug in a startup bash script to run before the main app. For example the Postgres Docker image [2] will run any scripts that may be mounted in a docker-entrypoint-initdb.d directory before starting the server. So putting a bash script in a ConfigMap that gets used as a disk volume, could be easier that downloading/auditing a binary "wait for it" container image.
[1] https://kubernetes.io/docs/tasks/configure-pod-container/con...
darlingdem | 2 comments | 3 weeks ago
is_ready() {
while ! nc -z $1 $2; do
echo "Waiting for $1:$2..."
sleep 1
done
echo "$1:$2 is accessible."
}
is_ready google.com 443 &
is_ready x.com 443 &
wait
k3vinw | 0 comments | 3 weeks ago
Replace the use of netcat with pure bash to test a tcp (or udp) connection: </dev/tcp/$1/$2
stavepan | 0 comments | 3 weeks ago
I built it for many reasons: - Most docker images do not contain netcat so you would have to download one of them in any case. - In the case of is_ready, you won't have to write this script yourself. - Repositories like this had a lot of traffic so I supposed that engineers need a similar tool but this repository requires wget and netcat as dependencies. For this reason, I built my own without any dependencies. https://github.com/eficode/wait-for