Signal handler

Runtime.getRuntime().addShutdownHook(new Thread(() -> {
  server.stopAcceptingNewRequests();
  waitForInFlight(Duration.ofSeconds(20));
  runtime.shutdown();
}));
Advertisement

Steps in order

  1. Health check goes red.
  2. Stop accepting new requests.
  3. Wait for in-flight to complete.
  4. Flush metrics/traces.
  5. Close DB connections.
  6. Exit.
Advertisement

Deadline discipline

K8s gives you 30s by default. Budget accordingly. Slow shutdown = forced kill.